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#
Optionskey | 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 Objectvalidate with an object will run on the first argument
#
Custom Optionscustom 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 FailIf the params are not valid, BadRequestError
will return a 400 Bad Request
response with detailed validation errors.
#
Validation Modelyou can use the validations as method decorators
then in the controller
#
Custom options#
Inherit Model#
Nested Modelnested model can be an object or array
#
Custom message#
Groupswhen groups defined on a schema or constrain it will only run when validation called with matched group
#
Custom Validatorit 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