Validator
You can add validations to any class or method using the validate decorator. The method will be called if the validation is valid.
Validations are using appolo-validator.
Installation#
Options#
| key | Description | Type | Default |
|---|---|---|---|
convert | when true, attempts to cast values to the required types (e.g. a string to a number) | boolean | true |
stripUnknown | remove unknown elements from objects and arrays | boolean | true |
in config/modules/all.ts
Usage#
Validate Object#
validate with an object will run on the first argument
Custom Options#
custom validate with options
| key | Description | Type | Default |
|---|---|---|---|
convert | when true, attempts to cast values to the required types (e.g. a string to a number) | boolean | true |
stripUnknown | remove unknown elements from objects and arrays | boolean | true |
groups | groups of the validation | string[] | [] |
Validation Fail#
If the params are not valid, BadRequestError will return a 400 Bad Request response with detailed validation errors.
Validation Model#
you can use the validations as method decorators
then in the controller
Custom options#
Inherit Model#
Nested Model#
nested model can be an object or array
Custom message#
Groups#
when groups defined on a schema or constrain it will only run when validation called with matched group
Custom Validator#
it is possible to define custom validators as standalone or with inject.
custom validator must implement IConstraint then register the validator using registerConstraint
register the validator to the base schema
add the validator types to base schema
now we can use the validator
string constrain will run but not max constrain