# new Validator()
Abstract class for defining synchronous validators.
Abstract class for defining synchronous validators.
This class extends the base BaseValidator
and enforces that any implementation of hasErrors
must be synchronous.
Use this when the validation process is immediate and does not require asynchronous operations.
- See:
-
BaseValidator
For the base validator.ValidatorOptions
For the base validator options.
Example
```typescript
// Example of a synchronous validator that checks if a number is greater than
class GreaterThanValidator extends Validator<{ gt?: number }> {
constructor(message: string = "Value must be greater than {0}") {
super(message);
}
hasErrors(value: number, options?: { gt?: number }) {
const minValue = options?.gt ?? 0;
if (value <= minValue) {
return this.getMessage();
}
return undefined;
}
}
// Example usage:
const validator = new GreaterThanValidator();
const error = validator.hasErrors(10, { gt: 15 });
if (error) {
console.log('Value must be greater than 15')
} else {
console.log('Value is valid');
}
```
- If `value` is less than or equal to `gt`, returns the error message.
- Otherwise, returns `undefined` indicating validation success.
Classes
- Validator
- Abstract class for defining synchronous validators.
Methods
# 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
Error message if validation fails, undefined if validation passes
string
|
undefined