The SchemaBase
class has been removed. This class was overriding the constructor and may cause problems in the future.
The user is now responsible to handle property assignment to the class manually.
Here's a simple function replacing the SchemaBase's behavior.
function validateFromPayload(validationClass: any, payload: Record<string, any>) {
const validationClassInstance = new validationClass();
Object.assign(validationClassInstance,payload);
return validate(validationClassInstance);
}
@Schema(true)
export class User extends SchemaBase {
@String()
username: string;
}
const user = new User({
username: 'AmauryD'
});
const validated = user.validate();
@Schema({
strict: true
})
export class User {
@String()
username: string;
}
const validated = validateFromPayload(User, {
username: 'AmauryD'
});
fastest-validator
has been moved to peerDependencies. So it needs to be installed in your project using your favorite package manager.
The old decorators were applying some properties by default (like converting numbers). This behavior was confusing and was removed. User needs to explicitly tell fastest validator to apply a specific validation option.
@Number()
- applies
{ type: "number", convert: true }
@Number()
- applies
{ type: "number" }
For better readability the first option to the @Schema decorator is an object.
// applies strict
@Schema(true)
class X {}
// applies strict
@Schema({
strict: true
})
class X {}
Before, second option was used to create only custom validation messages. It has been replaced by a more flexible option ValidatorConstructorOptions
. You need to wrap your old message object in a message key in order to migrate.
// applies strict
@Schema(true, {
evenNumber: "The '{field}' field must be an even number! Actual: {actual}"
})
class X {}
// applies strict
@Schema({
strict: true
}, {
messages: {
// Register our new error message text
evenNumber: "The '{field}' field must be an even number! Actual: {actual}"
},
// other fastest validator options ...
plugins: [],
})
class X {}
All decorators now provide typescript auto-completion. The types are directly from fastest-validator package.