:zap: The fastest JS validator library for NodeJS
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.17.0...v1.18.0
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.16.0...v1.17.0
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.15.0...v1.16.0
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.14.0...v1.15.0
Full Changelog: https://github.com/icebob/fastest-validator/compare/v1.13.0...v1.14.0
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
age: (schema, field, parent, context) => { ... }
normalize
method. #275 E.g.: validator.normalize({ a: "string[]|optional" })
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" }
});