Built-in Validators
All validators are imported from reformer/validators.
requiredβ
Field must have a non-empty value.
import { required } from '@reformer/core/validators';
required(path.name);
// Error: { code: 'required', message: '...' }
Empty values: '', null, undefined, []
emailβ
Valid email format.
import { email } from '@reformer/core/validators';
email(path.email);
// Error: { code: 'email', message: '...' }
minLength / maxLengthβ
String length constraints.
import { minLength, maxLength } from '@reformer/core/validators';
minLength(path.name, 2);
// Error: { code: 'minLength', params: { required: 2, actual: 1 } }
maxLength(path.bio, 500);
// Error: { code: 'maxLength', params: { required: 500, actual: 501 } }
min / maxβ
Number value constraints.
import { min, max } from '@reformer/core/validators';
min(path.age, 18);
// Error: { code: 'min', params: { min: 18, actual: 16 } }
max(path.quantity, 100);
// Error: { code: 'max', params: { max: 100, actual: 150 } }
patternβ
Match regex pattern.
import { pattern } from '@reformer/core/validators';
// Only letters
pattern(path.code, /^[A-Z]+$/);
// Error: { code: 'pattern', params: { pattern: '/^[A-Z]+$/' } }
// Custom error key
pattern(path.code, /^[A-Z]+$/, 'uppercase');
// Error: { code: 'uppercase' }
urlβ
Valid URL format.
import { url } from '@reformer/core/validators';
url(path.website);
// Error: { code: 'url', message: '...' }
phoneβ
Valid phone number format.
import { phone } from '@reformer/core/validators';
phone(path.phone);
// Error: { code: 'phone', message: '...' }
numberβ
Must be a valid number.
import { number } from '@reformer/core/validators';
number(path.amount);
// Error: { code: 'number', message: '...' }
dateβ
Valid date value.
import { date } from '@reformer/core/validators';
date(path.birthDate);
// Error: { code: 'date', message: '...' }
Combining Validatorsβ
Apply multiple validators to one field:
validation: (path) => {
required(path.password);
minLength(path.password, 8);
pattern(path.password, /[A-Z]/, 'uppercase');
pattern(path.password, /[0-9]/, 'hasNumber');
};
All validators run, errors are collected:
// If password is "abc"
errors: [
{ code: 'minLength', params: { required: 8, actual: 3 } },
{ code: 'uppercase' },
{ code: 'hasNumber' },
];
Next Stepsβ
- Async Validation β Server-side validation
- Custom Validators β Create your own