fastest-validator
Advanced tools
Changelog
1.7.0 (2020-08-30)
currency
rule by @ishan-srivastava in #178any
rule #185<a name="1.6.1"></a>
Changelog
1.6.0 (2020-08-06)
objectID
ruleYou can validate BSON/MongoDB ObjectID's
Example
const { ObjectID } = require("mongodb") // or anywhere else
const schema = {
id: {
type: "objectID",
ObjectID // passing the ObjectID class
}
}
const check = v.compile(schema);
check({ id: "5f082780b00cc7401fb8e8fc" }) // ok
check({ id: new ObjectID() }) // ok
check({ id: "5f082780b00cc7401fb8e8" }) // Error
You can use dynamic default value by defining a function that returns a value.
Example
In the following code, if createdAt
field not defined in object`, the validator sets the current time into the property:
const schema = {
createdAt: {
type: "date",
default: () => new Date()
}
};
const obj = {}
v.validate(obj, schema); // Valid
console.log(obj);
/*
{
createdAt: Date(2020-07-25T13:17:41.052Z)
}
*/
addMessage
method for using in plugins #166singleLine
property to string
rule. #180Many thanks to @intech and @erfanium for contributing.
<a name="1.5.1"></a>
Changelog
1.5.0 (2020-06-18)
tuple
validation ruleThanks 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 }
] }
}