Skip to content

Robust ValidatorRule-based data validation in JS

πŸ’‘ Extendable. Function-oriented. i18n-ready.

Robust Validator
Projects using Robust Validator

Validate your data in a declarative way

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.

ts
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);
ts
const data = {
  email: "not-a-valid-email",
};

const definition = {
  email: [required(), email(), max(255)],
  name: [required(), max(100)],
};

const result = await validate(data, definition);

Use composable functions for rule definitions

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.

Structured output for clean error handling

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.

json
{
  "isValid": false,
  "isInvalid": true,
  "fields": {
    "email": false,
    "name": true,
    "surname": true
  },
  "errors": {
    "email": [
      {
        "rule": "required",
        "message": "The field is required."
      }
    ]
  }
}
ts
const data = {
  users: {
    addresses: [
      { city: "Copenhagen" }
    ]
  }
};

const definition = {
  "users.addresses.*.city": "required",
};

const result = await validate(data, definition);

Validate deeply nested data with dot notation

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.

Built-in support for multiple languages

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.

ts
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" });
ts
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}",
});

Create your own validation rules

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.

Supported Languages

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.

Arabic
Azerbaijani
Belarusian
Bulgarian
Bosnian
Catalan
Czech
Welsh
Danish
German
Greek
English
Spanish
Estonian
Basque
Persian
Finnish
French
Croatian
Hungarian
Indonesian
Italian
Japanese
Georgian
Korean
Limburgish
Lithuanian
Latvian
Macedonian
Mongolian
Malay
Norwegian
Dutch
Polish
Portuguese
Romanian
Russian
Northern Sami
Slovenian
Albanian
Serbian
Swedish
Turkish
Ukrainian
Vietnamese
Chinese