A common gotcha of the current implementation of the @body decorator is that the method signature may define a type, but at runtime the actual object passed may really be of any type, or have other properties as well as those defined by the type. Even if it matches the properties, it is not an actual instance of the class.
This pull request adds the @bodyTyped decorator, for automatic coercion to a class using class-transformer, as well as optionally performing validation using class-validator. This allows a user to simply add the decorator and receive a runtime type-safe object that has already been validated and had any unwanted properties removed.
The signature is @bodyType(TypeConstructor, options?). Options are as per class-validator, with the addition of a validate option that enables the validation (by default the annotation creates the class but does not perform validation). A validation failure will return an HTTP 400 Bad Request with the relevant validation error messages.
Caveats: the type must be a class, not an interface, and it must have a default no-args constructor.
A common gotcha of the current implementation of the
@body
decorator is that the method signature may define a type, but at runtime the actual object passed may really be of any type, or have other properties as well as those defined by the type. Even if it matches the properties, it is not an actual instance of the class.This pull request adds the @bodyTyped decorator, for automatic coercion to a class using
class-transformer
, as well as optionally performing validation usingclass-validator
. This allows a user to simply add the decorator and receive a runtime type-safe object that has already been validated and had any unwanted properties removed.The signature is
@bodyType(TypeConstructor, options?)
. Options are as per class-validator, with the addition of avalidate
option that enables the validation (by default the annotation creates the class but does not perform validation). A validation failure will return an HTTP 400 Bad Request with the relevant validation error messages.Caveats: the type must be a class, not an interface, and it must have a default no-args constructor.