Member Junction
    Preparing search index...

    Class SemanticValidatorAbstract

    Abstract base class for semantic validators

    All semantic validators must extend this class and implement the validate() method.

    Hierarchy (View Summary)

    Index

    Constructors

    Accessors

    • get EvaluatesAbsentProp(): boolean

      Whether this validator also runs when the parent does not pass the property at all.

      By default a constraint is checked only against a prop the element actually passes, because most constraints describe a value ("these are field names on the entity") and have nothing to say about a missing one. A validator whose rule is about absence — "this prop must be set", "this prop must be set when another one is" — overrides this to return true. It is then also called for a missing prop, with context.PropertyPresent === false and context.propertyValue === undefined, and must handle that case.

      It is still not called for a missing prop when the element spreads props (<Grid {...props} />), since the spread may supply it.

      Returns boolean

    Methods

    • Apply custom error template

      If the constraint has an errorTemplate, applies it by replacing template variables with actual values.

      Template variables:

      • {property} - Property name from context
      • {value} - Property value from context
      • {constraint} - Constraint type
      • Custom variables from templateVars parameter

      Parameters

      • constraint: PropertyConstraint

        Constraint definition

      • context: ValidationContext

        Validation context

      • defaultMessage: string

        Default message to use if no template

      • templateVars: Record<string, any> = {}

        Additional template variables

      Returns string

      Formatted message

      // Constraint: { errorTemplate: "Field '{field}' not found on {entityName}" }
      const message = this.applyErrorTemplate(
      constraint,
      context,
      `Field '${field}' does not exist`,
      { field: 'FullName', entityName: 'Members' }
      );
      // => "Field 'FullName' not found on Members"
    • Create a violation with consistent formatting

      Helper method to create ConstraintViolation objects with common fields populated.

      Parameters

      • type: string

        Violation type (e.g., 'invalid-field', 'type-mismatch')

      • message: string

        Error message

      • severity: "critical" | "high" | "medium" | "low" = 'high'

        Severity level (default: 'high')

      • Optionalsuggestion: string

        Optional fix suggestion

      • Optionalmetadata: Record<string, any>

        Optional additional metadata

      Returns ConstraintViolation

      ConstraintViolation object

      return [
      this.createViolation(
      'invalid-field',
      `Field '${field}' does not exist on entity '${entityName}'`,
      'critical',
      `Use one of: ${validFields.join(', ')}`,
      { field, entityName, validFields }
      )
      ];
    • Find similar strings using Levenshtein distance

      Returns strings sorted by similarity (most similar first).

      Parameters

      • target: string

        The target string to find matches for

      • candidates: string[]

        Array of candidate strings

      • maxResults: number = 3

        Maximum number of results (default: 3)

      • maxDistance: number = 3

        Maximum edit distance to consider (default: 3)

      Returns string[]

      Array of similar strings, sorted by distance

      const fields = ['FirstName', 'LastName', 'Email', 'Status'];
      const similar = this.findSimilar('FristName', fields, 2);
      // => ['FirstName'] (edit distance 1)
    • Format a list of values for error messages

      Truncates long lists with "..." and formats values nicely.

      Parameters

      • values: any[]

        Array of values

      • maxItems: number = 10

        Maximum items to show (default: 10)

      Returns string

      Formatted string

      const fields = ['FirstName', 'LastName', 'Email', ...]; // 50 fields
      const formatted = this.formatValueList(fields, 5);
      // => "FirstName, LastName, Email, Status, JoinDate... (45 more)"
    • Get the dependent prop value

      If the constraint has a dependsOn field, this method retrieves the value of that dependent prop from sibling props.

      Returns null if:

      • No dependency is specified
      • The dependent prop is not found
      • The dependent prop has a dynamic value

      Parameters

      Returns any

      The dependent prop value, or null if not available

      // Constraint: { type: 'subset-of-entity-fields', dependsOn: 'entityName' }
      const entityName = this.getDependentPropValue(context, constraint);
      if (!entityName) {
      // Can't validate - entityName not provided or is dynamic
      return [];
      }
    • Get a description of what this validator checks

      Used for documentation and error messages. Should be a brief, user-friendly description.

      Returns string

      Validator description

      getDescription() {
      return "Validates that array elements are valid field names for the specified entity";
      }
    • Check if a value is dynamic (can't be validated statically)

      Dynamic values include identifiers (variables) and expressions (function calls, etc.) that can't be resolved at lint-time.

      Parameters

      • value: any

        The extracted value to check

      Returns value is DynamicValue

      True if the value is dynamic

      if (this.isDynamicValue(context.propertyValue)) {
      return []; // Skip validation
      }
    • Validate a prop value against a constraint

      This is the main method that subclasses must implement. It receives the validation context and constraint definition, and returns an array of violations (empty if valid).

      Note: Changed from async to sync since validators perform no actual I/O. This allows seamless integration with the synchronous linter architecture.

      Parameters

      Returns ConstraintViolation[]

      Array of violations (empty if valid)

      class MyValidator extends SemanticValidator {
      validate(context: ValidationContext, constraint: PropertyConstraint): ConstraintViolation[] {
      const violations: ConstraintViolation[] = [];

      // Check if value can be validated statically
      if (PropValueExtractor.isDynamicValue(context.propertyValue)) {
      return violations; // Skip - can't validate dynamic values
      }

      // Perform validation logic
      // ...

      return violations;
      }
      }