v1.5.0
New tuple
validation rule
Thanks for @Gamote, in this version there is a new tuple
. This rule checks if a value is an Array
with the elements order as described by the schema.
Example
const schema = {
grade: { type: "tuple", items: ["string", "number", "string"] }
};
const schema = {
location: { type: "tuple", empty: false, items: [
{ type: "number", min: 35, max: 45 },
{ type: "number", min: -75, max: -65 }
] }
}
Define aliases & custom rules in constructor options #162
You can define aliases & custom rules in constructor options instead of using v.alias
and v.add
.
Example
const v = new Validator({
aliases: {
username: {
type: 'string',
min: 4,
max: 30
}
},
customRules: {
even: function({ schema, messages }, path, context) {
return {
source: `
if (value % 2 != 0)
${this.makeError({ type: "evenNumber", actual: "value", messages })}
return value;
`
};
})
}
});
Support plugins
Thanks for @erfanium, you can create plugin for fastest-validator
.
Example
// Plugin Side
function myPlugin(validator){
// you can modify validator here
// e.g.: validator.add(...)
// or : validator.alias(...)
}
// Validator Side
const v = new Validator();
v.plugin(myPlugin)