# 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.
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 |
acceptedTypes |
Array.<string>
|
Type names that this validator accepts (used for runtime type checking) |
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 |
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 |
boolean