Rule-based data validation in JS. Extendable, function-oriented, i18n-supported
Most validation libraries are either hard to use, missing key features, or no longer maintained. It’s tough to find one that lets you reuse rules, supports multiple languages, and still feels easy to work with.
Robust Validator was built to solve that. It makes validation simple, reusable, and flexible. You can define your rules once and use them anywhere, and it works great with multiple languages out of the box.
If you want a validation library that just works, is easy to read, and stays up to date, Robust Validator is a solid choice.
I decided on some fundamental rules while building this library:
- ✅︎ Every validation rule should be an independent function.
- ✅︎ Every validation rule should be able to be used separately
- ✅︎ All validation definition should be able to be stored anywhere (database, memory, configuration files, 3rd party API, etc) to be used later.
- ✅︎ All validation rules should be able to be used in different languages.
- ✅︎ Contribution to the rule set should be easy.
- ✅︎ Should be well-documented.
The library can be installed into an existing project:
$ npm install --save robust-validator
Using robust-validator is very simple.
You should just call the validate()
function with data and the definition.
import { validate, setLocales, en } from "robust-validator";
setLocales(en);
const data = {
email: "not-a-valid-email",
name: "John",
surname: "Doe",
};
const definition = {
email: "required|email",
name: "required|min:1|max:50",
surname: "required|min:1|max:50",
};
const result = await validate(data, definition);
console.log(result);
This feature allows dynamic traversal of nested data structures, supporting complex validation rules for paths like users.*.addresses.*.city
.
It is inspired by Laravel's validation system and works seamlessly with arrays and objects, including deeply nested data.
import { validate, setLocales, en } from "robust-validator";
setLocales(en);
const data = {
secret: "some secret",
users: [
{ addresses: [{ city: "New York" }, { city: "Istanbul" }] },
{ addresses: [{ city: "New York" }, { street: "Wall Street" }] },
],
permissons: { read: true, write: true },
};
const definition = {
secret: "required|min:100",
"users.*.addresses.*.city": "required",
"permissons.read": "required|boolean",
"permissons.delete": "required|boolean",
};
const result = await validate(data, definition);
console.log(result);
And this is the content of the result
variable:
{
"isValid": false,
"isInvalid": true,
"fields": {
"secret": false,
"users.*.addresses.*.city": false,
"permissons.read": true,
"permissons.delete": false
},
"errors": {
"secret": [{ "rule": "min", "message": "The field must be at least 100." }],
"users.1.addresses.1.city": [
{ "rule": "required", "message": "The field is required." }
],
"permissons.delete": [
{ "rule": "required", "message": "The field is required." }
]
}
}
Made with contrib.rocks.