The fastest JS validator library for NodeJS
MIT License
Bot releases are visible (Hide)
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.17.0...v1.18.0
Published by icebob over 1 year ago
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.16.0...v1.17.0
Published by icebob almost 2 years ago
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.15.0...v1.16.0
Published by icebob about 2 years ago
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.14.0...v1.15.0
Published by icebob about 2 years ago
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.13.0...v1.14.0
Published by icebob about 2 years ago
empty: true
in string rule. Fixes #283 by @icebob in https://github.com/icebob/fastest-validator/pull/284
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.12.0...v1.13.0
Published by icebob about 3 years ago
age: (schema, field, parent, context) => { ... }
normalize
method. #275 E.g.: validator.normalize({ a: "string[]|optional" })
Published by icebob over 3 years ago
const schema = {
// Turn on async mode for this schema
$$async: true,
name: {
type: "string",
min: 4,
max: 25,
custom: async (v) => {
await new Promise(resolve => setTimeout(resolve, 1000));
return v.toUpperCase();
}
},
username: {
type: "custom",
custom: async (v) => {
// E.g. checking in the DB that whether is unique.
await new Promise(resolve => setTimeout(resolve, 1000));
return v.trim();
}
},
}
The compiled check
function has an async
property to detect this mode. If true
it returns a Promise
.
const check = v.compile(schema);
console.log("Is async?", check.async);
You can pass any extra meta information for the custom validators which is available via context.meta
.
const schema = {
name: { type: "string", custom: (value, errors, schema, name, parent, context) => {
// Access to the meta
return context.meta.a;
} },
};
const check = v.compile(schema);
const res = check(obj, {
// Passes meta information
meta: { a: "from-meta" }
});
Published by icebob almost 4 years ago
nullable
rule attribute in #185
const schema = {
age: { type: "number", nullable: true }
}
v.validate({ age: 42 }, schema); // Valid
v.validate({ age: null }, schema); // Valid
v.validate({ age: undefined }, schema); // Fail because undefined is disallowed
v.validate({}, schema); // Fail because undefined is disallowed
Published by icebob about 4 years ago
currency
rule by @ishan-srivastava in #178
any
rule #185
Published by icebob about 4 years ago
ObjectID
rulePublished by icebob about 4 years ago
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 #166
singleLine
property to string
rule. #180
Many thanks to @intech and @erfanium for contributing.
Published by icebob over 4 years ago
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 }
] }
}
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;
`
};
})
}
});
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)