Declarative βπ½
Write rules once, reuse anywhere: configs, DB, UI, etc.
π‘ Extendable. Function-oriented. i18n-ready.
The validate()
function checks your data against rule definitions using simple, readable syntax like "required|email"
.
It works directly with plain JavaScript objects, so you stay in control of when and how validation is performed.
The library is function oriented, extendable, and built to fit cleanly into any part of your application logic.
import { validate } from "robust-validator";
const rules = {
email: "required|email",
age: "integer|min:18"
};
const data = {
email: "test@example.com",
age: 21
};
const result = validate(data, rules);
const data = {
email: "not-a-valid-email",
};
const definition = {
email: [required(), email(), max(255)],
name: [required(), max(100)],
};
const result = await validate(data, definition);
Instead of writing rules as strings, you can define them as function calls. This gives you full control, better type safety, and cleaner auto-completion in editors.
Every rule becomes a reusable function, making your validation logic easier to test and extend when your app grows.
The result from the validate()
function includes everything you need to respond or react. It separates global state from field-level results.
You get isValid
, isInvalid
, per-field status, and a detailed list of errors with rule names and messages.
{
"isValid": false,
"isInvalid": true,
"fields": {
"email": false,
"name": true,
"surname": true
},
"errors": {
"email": [
{
"rule": "required",
"message": "The field is required."
}
]
}
}
const data = {
users: {
addresses: [
{ city: "Copenhagen" }
]
}
};
const definition = {
"users.addresses.*.city": "required",
};
const result = await validate(data, definition);
The validator supports nested objects and arrays out of the box. You can use dot notation to target any depth in the data structure.
Wildcards like *
make it easy to validate each item in an array without writing repetitive rules.
This syntax works seamlessly for complex payloads, including user profiles, form builders, or nested APIs, without requiring any extra setup.
Robust Validator includes native i18n support. You can register multiple locales and switch the language with a single option.
This makes it easy to return localized error messages for forms, APIs, or any user-facing validation without extra libraries.
import { setLocales, setOptions, en, fr, de } from "robust-validator";
// Setting the supported locales
setLocales(en, fr, de);
// Setting the user's locale
setOptions({ language: "en" });
import { register } from "robust-validator";
const ruleFunction = (value: any) => {
// TODO: add your custom rule logic here
return true;
};
register("exists", ruleFunction, {
en: "The record doesn't exists on database: {0}",
});
Robust Validator lets you register custom rules using the register()
function. This is ideal when you need to add application-specific logic, like checking if a record exists in the database.
You define the rule name, a validation function, and localized error messages. Once registered, your custom rule behaves just like any built-in rule.
Robust Validator includes built-in error messages for a wide range of languages. You can use them as-is or extend them with your own translations.