Validators
Registration
Section titled “Registration”import { registerDefaultValidators, registerValidator } from 'formlayer';
// Register all 12 built-in validators at onceregisterDefaultValidators();
// Register a single custom validatorregisterValidator({ type: 'MyValidator', validate(value, options) { return { valid: true, message: '' }; },});Validator Interface
Section titled “Validator Interface”interface Validator { type: string; validate( value: string, options: Record<string, unknown> ): ValidatorResult | Promise<ValidatorResult>;}
interface ValidatorResult { valid: boolean; message: string;}The type must match the type field in data-validate JSON rules.
Built-in Validators
Section titled “Built-in Validators”| Type | Options | Passes on empty? |
|---|---|---|
NotEmpty | message | No |
StringLength | minimum, maximum, message | Yes |
EmailAddress | message | Yes |
RegularExpression | regularExpression (PCRE delimited), message | Yes |
NumberRange | minimum, maximum, message | Yes |
Integer | message | Yes |
Float | message | Yes |
Number | message | Yes |
Alphanumeric | message | Yes |
DateTime | message | Yes |
DateRange | minimum, maximum, message | Yes |
FileSize | minimum, maximum (e.g. "5M"), message | Yes |
Most validators pass on empty values by design. Combine with NotEmpty for required fields.
ValidatorRule (HTML format)
Section titled “ValidatorRule (HTML format)”interface ValidatorRule { type: string; options?: Record<string, unknown>;}Used in HTML as a JSON array:
<div data-validate='[ {"type":"NotEmpty","options":{"message":"Required"}}, {"type":"StringLength","options":{"minimum":3}}]'>Running Validators Programmatically
Section titled “Running Validators Programmatically”import { runValidators, runValidatorsAsync } from 'formlayer/validators';
// Synchronous (skips async validators with a warning)const results = runValidators(rules, value);
// Async (awaits each validator)const results = await runValidatorsAsync(rules, value);Both return an array of failed ValidatorResult objects.