Class

BaseValidator

(abstract) BaseValidator(async, message, acceptedTypes)

Constructor

# abstract new BaseValidator(async, message, acceptedTypes)

The BaseValidator class provides the foundation for all synchronous and asynchronous validator implementations. It handles type checking, error message formatting, and defines the interface that all validators must implement. This class is designed to be extended by specific validator classes that define their own validation logic.

Abstract base class for all validators in the validation framework.

sequenceDiagram participant C as Client participant V as Validator Subclass participant B as BaseValidator C->>V: new CustomValidator(async, message) V->>B: super(async, message, acceptedTypes) B->>B: Store message, async flag, and accepted types B->>B: Optionally wrap hasErrors with type checking C->>V: hasErrors(value, options) alt value type not in acceptedTypes B-->>C: Type error message else value type is accepted V->>V: Custom validation logic V-->>C: Validation result end
Parameters:
Name Type Description
async boolean

Defines if the validator is async (must match the subclass signature)

message string

Default error message to display when validation fails (defaults to DEFAULT_ERROR_MESSAGES#DEFAULT)

acceptedTypes Array.<string>

Type names that this validator accepts (used for runtime type checking)

View Source validation/Validators/BaseValidator.ts, line 5

Examples
// Example of a synchronous validator
class SyncValidator extends BaseValidator<SomeOptions, false> {
  constructor() {
    super(false, "Sync validation failed", String.name);
  }

  public hasErrors(value: any, options?: SomeOptions): string | undefined {
    if (typeof value !== "string") return this.getMessage(this.message);
    return undefined;
  }
}
// Example of an asynchronous custom validator
class AsyncValidator extends BaseValidator<SomeOptions, true> {
  constructor() {
    super(true, "Async validation failed", String.name);
  }

  public async hasErrors(value: any, options?: SomeOptions): Promise<string | undefined> {
    const result = await someAsyncCheck(value);
    if (!result) return this.getMessage(this.message);
    return undefined;
  }
}

Methods

# protected getMessage(message, …args) → {string}

Creates a formatted error message by replacing placeholders with provided arguments. This method uses the string formatting utility to generate consistent error messages across all validators.

Formats an error message with optional arguments

Parameters:
Name Type Attributes Description
message string

The message template with placeholders

args any <repeatable>

Values to insert into the message template

View Source validation/Validators/BaseValidator.ts, line 142

The formatted error message

string

# abstract hasErrors(value, optionsopt, proxy) → {string|undefined}

Abstract method that must be implemented by all validator subclasses. This method contains the core validation logic that determines whether a value is valid according to the specific rules of the validator. If the value is valid, the method returns undefined; otherwise, it returns an error message.

Validates a value against specific validation rules

Parameters:
Name Type Attributes Description
value any

The value to validate

options V <optional>

Optional configuration options for customizing validation behavior

proxy PathProxy.<any>
See:
  • Model#validate

View Source validation/Validators/BaseValidator.ts, line 176

Error message if validation fails, undefined if validation passes

string | undefined

# static isValidator(val) → {boolean}

Duck typing for Validators

Parameters:
Name Type Description
val

View Source validation/Validators/BaseValidator.ts, line 183

boolean