Validation
Validation is configured per field using the field config.
A model instance is validated before
Model.prototype.insert
and Model.prototype.update
. Before inserts, all
fields will be validated (except the primary field if it's undefined
)
whereas before updates, only the fields that have values set will be validated.
You can trigger validation any time on a model instance via
Model.prototype.validate
.
info
Validation can be configured for each field via the field configs.
Regex validation
You can validate fields against regular expressions by either
- configuring a regular expression that all values must match
- configuring a regular expression that all values must not match
- both matching and non-matching regular expressions
The regular expressions are tested against the field's value.
Matching regex
Can be configured directly via the regex
field config:
Or explicitly via an object with a matching
regex:
Both these configs will ensure that values for username
will be lowercase a-z
letters:
Non-matching regex
Can be configured via an object with a notMatching
regex:
This ensures that values for username
will not contain a dot (.):
Both matching and non-matching regex
Can be configured via an object with both matching
and notMatching
regex's:
This ensures that values for username
will be lowercase a-z letters and will
not contain a dot (.):
Custom validation
To add custom validation, supply a validate
function to the field config. The
validator is called with the field's value and the Model
instance as
parameters, so you're able to access other values on the instance. This is handy
for validating fields based on the input of other fields.
Async validators (validators that return a Promise
) are also supported.
Validation for the field fails if the function:
- throws an error
- returns
false
- returns a
Promise
that is rejected with an error - returns a
Promise
that is resolved withfalse
If the validator throws an error or returns a rejected Promise
, validation for
that field fails with that error (or the rejection error). However, if it returns
false
or resolves the Promise
with false
, then validation fails with a
Field.ValidationError
.
You can also continue validating by returning an object with the regular
validators (or resolving the Promise
with an
object with validators), including another custom validator function!
info
Custom validators will not be called if the value is undefined
, but will
be called if the value is null
.
JSON validation
For json (and
jsonb) fields, you can have the JSON values
validated by adding a shape
object to the field definition object.
With this config, these are valid:
while these are invalid:
info
JSON shape
validators support all the
validators, including nested shape
validators for nested objects.
info
You may also define the shape with the fieldName: fieldType
shorthand:
JSON arrays
For JSON arrays, use the array
field type. You can also define the shape of a
single array item by passing a shape
validation object with the regular
validators.
Nested objects
For nested objects, use the object
field type. You can also define the nested
object's shape with a nested shape
validator.
note
Nested object fields should contain a type
, just like regular Field
instances.
Root-level JSON fields
For JSON fields whose values are not nested in an object, define their
validators with a shape
validator:
Note that you may also define the shape with the fieldName: fieldType
shorthand:
Overriding validators
Instead of repeating the same validation config for multiple related fieds, you could instead overload a validator.
For example, to enforce a max-length of 500
for all string
field types,
instead of adding a maxLength
validator for every field of type string
, you
could override the string
validator to add max-length validation for every
string
field.
This can be easily done with a plugin: