Validates objects, optionally coercing values into other types.
Validators and types are all exported from the root:
import { any, text } from '@fmtk/validation';
Validators are functions which return a function which actually does the validation.
// require a string
const validator = text();
const result = validator({ value: 'foo' });
Most likely, you'll want to validate whole objects:
const personValidator = properties({
name: text(),
age: optional(integer()),
dateOfBirth: dateFormat('YYYY-MM-DD'),
});
personValidator({
value: { name: 'Bob', age: '5', dateOfBirth: '2013-12-05' },
mode: ValidationMode.String,
});
// ===> {
// value: {
// name: 'Bob',
// age: 5,
// dateOfBirth: new Date('2013-12-05T00:00:00.000')
// },
// ok: true
// }
personValidator({ value: { name: 'Bob' } });
// ===> {
// value: { name: 'Bob' },
// errors: [
// {
// id: 'EXPECTED_DATE_FORMAT',
// text: 'expected a date',
// field: 'dateOfBirth'
// }
// ]
// }
The library defines validators for various data types and formats.
All validators return an implementation of ValueValidator
, i.e., a function
that takes a ValidationContext
as its only parameter, and returns a ValidationResult
.
Properties:
field | description |
---|---|
value | the value to validate |
field | the name of the field under validation (for properties or modelValidator ) |
mode | the validation mode |
enum ValidationMode {
Strict = 'strict',
JSON = 'json',
Form = 'form',
String = 'string',
}
field | description |
---|---|
value | the value after validation - allows values to be coerced, e.g., integer() turns '1' into 1 |
errors | an array of ValidationError if there are any |
field | description |
---|---|
id |
the error ID |
text |
the error message |
field |
the field the error relates to, if applicable |
extra |
any extra info, if applicable |
Don't perform validation. This will accept any input and never return an error.
Expect the value to be an array.
Params:
field | description |
---|---|
elem | a validator to validate each element in the array |
arraySplit | the character to split a string on (default ',' ) |
If ValidationOptions.parse
is true
, the validator will first create an array from the input by splitting by arraySplit
.
Require a boolean value.
If ValidationOptions.parse
is true
, the string values true
and false
are accepted.
Creates a validator that requires at least one of the supplied validators to be valid.
Creates a validator that requires all supplied validators to be valid.
Validates a date/time against the given format.
The format
argument is only relevant if If ValidationOptions.parse
is true
, and the input is a string. In this case, it will parse the string with moment
and the given format.
If the input is a Date
it is always valid, regardless of the format.
Require an email address.
Expects a string to match the following regex: /^[^@]+@[^@]+$/
Require an integer value. The options minValue
and maxValue
are inclusive.
Require one of a list of values.
Require a value not to be present.
Make a value optional.
Validate a password against a complexity score.
minScore
should be between 0 and 4 inclusive.
Require a real value (floating point).
If ValidationOptions.parse
is true
, the number is parsed from a string if applicable.
Require a text value.
Validates a string as a UK mobile number, coercing it to E.164 format.
Validates an object, taking a hash which describes the fields. E.g.:
properties({
name: text(),
age: optional(integer()),
});