Skip to content

Rules

In this section, you can find the truth tables for all validation rules.

TIP

You can learn more about the Rule Terminology.

WARNING

Each rule function should validate only one thing. For example, the email validation should NOT check if the data is provided. Otherwise, a rule function can not check the optional data.

That's why null and undefined values are acceptable for all rules except the required.

If you want to check if the data is provided and is a valid email, you should use two rules (required, email) at the same time.

accepted

The field under validation must be yes, on, 1 or true. This is useful for validating "Terms of Service" acceptance.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { terms: "accepted" });
ts
import { validate, accepted } from "robust-validator";
// ...
await validate(data, { terms: [accepted()] });
ts
import { isAccepted } from "robust-validator";
// ...
isAccepted("your-value");
RuleValueIs valid?
acceptednull🔴
acceptedundefined🔴
accepted'yes'🟢
accepted'on'🟢
accepted1🟢
acceptedtrue🟢
acceptedjohn🔴
accepted3.14🔴

after:date

The field under validation must be after the given date.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "after:2023-01-01" });
ts
import { validate, after } from "robust-validator";
// ...
await validate(data, { startAt: [after("2023-01-01")] });
ts
import { isAfter } from "robust-validator";
// ...
isAfter("your-value", "2023-01-01");

TIP

robust-validator library uses the date-fns for the date validations.

You can check the possible date formats here.

RuleValuestartAtIs valid?
after:2024-01-01null🔴
after:2024-01-01undefined🔴
after:2024-01-012025-01-01🟢
after:startAt2025-01-012024-01-01🟢
after:startAt2024-01-012024-01-01🔴
after:startAt2024-01-012025-01-01🔴
after:2024-01-012024-01-01🔴
after:2024-01-012020-01-01🔴

after_or_equal:date

The field unter validation must be after or equal to the given field

ts
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "after_or_equal:2023-01-01" });
ts
import { validate, afterOrEqual } from "robust-validator";
// ...
await validate(data, { startAt: [afterOrEqual("2023-01-01")] });
ts
import { isAfterOrEqual } from "robust-validator";
// ...
isAfterOrEqual("your-value", "2023-01-01");

TIP

robust-validator library uses the date-fns for the date validations.

You can check the possible date formats here.

RuleValuestartAtIs valid?
after_or_equal:2024-01-01null🔴
after_or_equal:2024-01-01undefined🔴
after_or_equal:2024-01-012025-01-01🟢
after_or_equal:2024-01-012024-01-01🟢
after_or_equal:startAt2025-01-012024-01-01🟢
after_or_equal:startAt2024-01-012024-01-01🟢
after_or_equal:startAt2024-01-012025-01-01🔴
after_or_equal:2024-01-012020-01-01🔴

alpha

The field under validation must be entirely alphabetic characters.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { username: "alpha" });
ts
import { validate, alpha } from "robust-validator";
// ...
await validate(data, { username: [alpha()] });
ts
import { isAlpha } from "robust-validator";
// ...
isAlpha("your-value");
RuleValueIs valid?
alphanull🔴
alphaundefined🔴
alphajohn🟢
alphajohn123🔴
alphajohn-doe🔴
alpha123🔴
alpha3.14🔴
alphatrue🔴

alpha_dash

The field under validation may have alpha-numeric characters, as well as dashes and underscores.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { username: "alpha_dash" });
ts
import { validate, alphaDash } from "robust-validator";
// ...
await validate(data, { username: [alphaDash()] });
ts
import { isAlphaDash } from "robust-validator";
// ...
isAlphaDash("your-value");
RuleValueIs valid?
alpha_dashnull🔴
alpha_dashundefined🔴
alpha_dashjohn🟢
alpha_dashjohn-doe🟢
alpha_dashjohn_doe🟢
alpha_dashjohn123🔴
alpha_dash123🔴
alpha_dash3.14🔴
alpha_dashtrue🔴

alpha_num

The field under validation must be entirely alpha-numeric characters.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { username: "alpha_num" });
ts
import { validate, alphaNum } from "robust-validator";
// ...
await validate(data, { username: [alphaNum()] });
ts
import { isAlphaNum } from "robust-validator";
// ...
isAlphaNum("your-value");
RuleValueIs valid?
alpha_numnull🔴
alpha_numundefined🔴
alpha_numjohn🟢
alpha_numjohn123🟢
alpha_numjohn-doe🔴
alpha_numjohn_doe🔴
alpha_num123🔴
alpha_num3.14🔴
alpha_numtrue🔴

array

The field under validation must be an array.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { selectedIds: "array" });
ts
import { validate, array } from "robust-validator";
// ...
await validate(data, { selectedIds: [array()] });
ts
import { isArray } from "robust-validator";
// ...
isArray([1, 2, 3]);
RuleValueIs valid?
arraynull🔴
arrayundefined🔴
array[]🟢
array[1, 2, 3]🟢
array[{"id": 1}]🟢
arrayjohn🔴
arrayjohn🔴
array123🔴
array3.14🔴
arraytrue🔴

before:date

The field under validation must be before the given date.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "before:2023-01-01" });
ts
import { validate, before } from "robust-validator";
// ...
await validate(data, { startAt: [before("2023-01-01")] });
ts
import { isBefore } from "robust-validator";
// ...
isBefore("your-value", "2023-01-01");

TIP

robust-validator library uses the date-fns for the date validations.

You can check the possible date formats here.

RuleValuefinishAtIs valid?
before:2024-01-01null🔴
before:2024-01-01undefined🔴
before:2024-01-012023-01-01🟢
before:finishAt2023-01-012024-01-01🟢
before:finishAt2023-01-012023-01-01🔴
before:finishAt2023-01-012022-01-01🔴
before:2024-01-012024-01-01🔴
before:2024-01-012025-01-01🔴

before_or_equal:date

ts
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "before_or_equal:2023-01-01" });
ts
import { validate, beforeOrEqual } from "robust-validator";
// ...
await validate(data, { startAt: [beforeOrEqual("2023-01-01")] });
ts
import { isBeforeOrEqual } from "robust-validator";
// ...
isBeforeOrEqual("your-value", "2023-01-01");

The field under validation must be before or equal to the given date.

TIP

robust-validator library uses the date-fns for the date validations.

You can check the possible date formats here.

RuleValuefinishAtIs valid?
before_or_equal:2024-01-01null🔴
before_or_equal:2024-01-01undefined🔴
before_or_equal:2024-01-012023-01-01🟢
before_or_equal:2024-01-012024-01-01🟢
before_or_equal:finishAt2023-01-012024-01-01🟢
before_or_equal:finishAt2023-01-012023-01-01🟢
before_or_equal:finishAt2023-01-012022-01-01🔴
before_or_equal:2024-01-012025-01-01🔴

between:min,max

The field under validation must have a size between the given min and max. Strings, and numerics are evaluated in the same fashion as the size rule.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { score: "between:1,10" });
ts
import { validate, score } from "robust-validator";
// ...
await validate(data, { score: [score(1, 10)] });
ts
import { isBetween } from "robust-validator";
// ...
isBetween(3, 1, 10);
RuleValueIs valid?
between:1,5null🔴
between:1,5undefined🔴
between:1,5'john'🟢
between:1,5'12345'🟢
between:1,5long-text🔴
between:1,512345🔴

boolean

The field under validation must be a boolean value of the form true, false, 0, 1, 'true', 'false', '0', '1',

ts
import { validate } from "robust-validator";
// ...
await validate(data, { isOpen: "boolean" });
ts
import { validate, boolean } from "robust-validator";
// ...
await validate(data, { isOpen: [boolean()] });
ts
import { isBoolean } from "robust-validator";
// ...
isBoolean(true);
RuleValueIs valid?
booleannull🔴
booleanundefined🔴
booleantrue🟢
boolean'true'🟢
boolean1🟢
boolean'1'🟢
booleanfalse🔴
boolean'false'🔴
boolean0🔴
boolean'0'🔴

confirmed

The field under validation must have a matching field of foo_confirmed. For example, if the field under validation is password, a matching password_confirmed field must be present in the input.

Let's assume that the value of the password field is 123456. If you use the confirmed definition on the password field's rules, the truth table would look like the following example:

ts
import { validate } from "robust-validator";
// ...
await validate(data, { password: "confirmed" });
ts
import { validate, confirmed } from "robust-validator";
// ...
await validate(data, { password: [confirmed()] });
ts
import { isConfirmed } from "robust-validator";
// ...
isConfirmed("your-data");
FieldValueIs valid?
password_confirmed123456🟢
password_confirmed654321🔴
password_confirmednull🔴
password_confirmedundefined🔴
password_confirmedtrue🔴
password_confirmedfalse🔴
password_confirmed{}🔴

date:format

The field under validation must be a valid date format which is acceptable by Javascript's Date object.

TIP

robust-validator library uses the date-fns for the date validations.

You can check the possible date formats here.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { startAt: "date:yyyy-MM-dd" });
ts
import { validate, date } from "robust-validator";
// ...
await validate(data, { startAt: [date("yyyy-MM-dd")] });
ts
import { isDate } from "robust-validator";
// ...
isDate("your-data", "yyyy-MM-dd");
RuleValueIs valid?
date:yyyy-MM-ddnull🔴
date:yyyy-MM-ddundefined🔴
date:yyyy-MM-dd2023-12-16🟢
date:yyyy-MM-dd2023-01-01🟢
date:yyyy-MM-ddDecember 16, 2023 12:00:00🔴
date:yyyy-MM-dd2022-13-01🔴
date:yyyy-MM-dd2022-12-32🔴
date:yyyy-MM-dd2022-02-29🔴
date:yyyy-MM-ddfalse🔴

digits:value

The field under validation must be numeric and must have an exact length of value.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { pin: "digits:4" });
ts
import { validate, digits } from "robust-validator";
// ...
await validate(data, { pin: [digits(4)] });
ts
import { isDigits } from "robust-validator";
// ...
isDigits("1234", 4);
RuleValueIs valid?
digits:4null🔴
digits:4undefined🔴
digits:41234🟢
digits:4123🔴
digits:4true🔴
digits:4'1234'🔴
digits:4123456🔴

digits_between:min,max

The field under validation must be numeric and must have length between given min and max.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { pin: "digits_between:4,6" });
ts
import { validate, digitsBetween } from "robust-validator";
// ...
await validate(data, { pin: [digitsBetween(4, 6)] });
ts
import { isdigitsBetween } from "robust-validator";
// ...
isdigitsBetween("1234", 4, 6);
RuleValueIs valid?
digits_between:4,6null🔴
digits_between:4,6undefined🔴
digits_between:4,61234🟢
digits_between:4,6123456🟢
digits_between:4,6123🔴
digits_between:4,6true🔴
digits_between:4,6'1234'🔴

email

The field under validation must be formatted as an e-mail address.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { contact_email: "email" });
ts
import { validate, email } from "robust-validator";
// ...
await validate(data, { contact_email: [email()] });
ts
import { isEmail } from "robust-validator";
// ...
isEmail("your-date");
RuleValueIs valid?
emailnull🔴
emailundefined🔴
emailfoo@bar.com🟢
emailjust a text🔴
emailtrue🔴
email'1234'🔴

hex

The field under validation should be a hexadecimal format.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { colorCode: "hex" });
ts
import { validate, hex } from "robust-validator";
// ...
await validate(data, { colorCode: [hex()] });
ts
import { isHex } from "robust-validator";
// ...
isHex("f1f1f1");
RuleValueIs valid?
hexnull🔴
hexundefined🔴
hex1aF🟢
hex1234567890ABCDEF🟢
hex123xyz🔴
hex0xg🔴
hexinvalid string🔴

in:foo,bar,...

The field under validation must be included in the given list of values.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { userChoice: "in:news,marketing" });
ts
import { validate, in } from "robust-validator";
// ...
await validate(data, { userChoice: [in(["news", "marketing"])] });
ts
import { isIn } from "robust-validator";
// ...
isIn("your-data", ["news", "marketing"]);
RuleValueIs valid?
in:A,Bnull🔴
in:A,Bundefined🔴
in:A,BA🟢
in:A,BB🟢
in:A,BC🔴
in:A,Btrue🔴
in:A,B{}🔴

integer

The field under validation must have an integer value.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { age: "integer" });
ts
import { validate, integer } from "robust-validator";
// ...
await validate(data, { age: [integer()] });
ts
import { isInteger } from "robust-validator";
// ...
isInteger(134);
RuleValueIs valid?
integernull🔴
integerundefined🔴
integer123🟢
integer3.14🔴
integerabc🔴

max:value

Validate that an attribute is no greater than a given size

ts
import { validate } from "robust-validator";
// ...
await validate(data, { age: "max:99" });
ts
import { validate, max } from "robust-validator";
// ...
await validate(data, { age: [max(99)] });
ts
import { isMax } from "robust-validator";
// ...
isMax(10, 99);
RuleValueIs valid?
max:5null🔴
max:5undefined🔴
max:5'123'🟢
max:53🟢
max:5'abcdef'🔴
max:510🔴

min:value

Validate that an attribute is at least a given size.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { age: "min:22" });
ts
import { validate, min } from "robust-validator";
// ...
await validate(data, { age: [min(22)] });
ts
import { isMin } from "robust-validator";
// ...
isMin(10, 22);
RuleValueIs valid?
min:5null🔴
min:5undefined🔴
min:5'abcdef'🟢
min:5'123456'🟢
min:510🟢
min:5'abcdef'🔴
min:52🔴

not_in:foo,bar,...

The field under validation must not be included in the given list of values.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { userChoice: "not_in:news,marketing" });
ts
import { validate, notIn } from "robust-validator";
// ...
await validate(data, { userChoice: [notIn(["news", "marketing"])] });
ts
import { isNotIn } from "robust-validator";
// ...
isNotIn("your-data", ["news", "marketing"]);
RuleValueIs valid?
not_in:A,Bnull🔴
not_in:A,Bundefined🔴
not_in:A,BC🟢
not_in:A,BA🔴
not_in:A,BB🔴
not_in:A,Btrue🔴

numeric

Validate that an attribute is numeric.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { salary: "numeric" });
ts
import { validate, numeric } from "robust-validator";
// ...
await validate(data, { salary: [numeric()] });
ts
import { isNumeric } from "robust-validator";
// ...
isNumeric(3000);
RuleValueIs valid?
integernull🔴
integerundefined🔴
integer123🟢
integer3.14🟢
integerabc🔴

required

Checks if the value is provided.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { salary: "required" });
ts
import { validate, required } from "robust-validator";
// ...
await validate(data, { salary: [required()] });
ts
import { isRequired } from "robust-validator";
// ...
isRequired("your-data");
RuleValueIs valid?
requiredjohn🟢
required123🟢
required1.23🟢
required{}🟢
requirednull🔴
requiredundefined🔴
required''🔴
required' '🔴

size:value

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { password: "size:12" });
ts
import { validate, size } from "robust-validator";
// ...
await validate(data, { password: [size(12)] });
ts
import { isSize } from "robust-validator";
// ...
isSize("your-data", 12);
RuleValueIs valid?
size:3null🔴
size:3undefined🔴
size:3abc🟢
size:31🟢
size:31.23🟢
size:3abcde🔴
size:310🔴

string

The field under validation must be a string.

ts
import { validate } from "robust-validator";
// ...
await validate(data, { content: "string" });
ts
import { validate, string } from "robust-validator";
// ...
await validate(data, { content: [string()] });
ts
import { isString } from "robust-validator";
// ...
isString("your-data");
RuleValueIs valid?
stringabc🟢
string''🟢
string' '🟢
string1🔴
string1.23🔴
stringabcde🔴
string10🔴
stringnull🔴
stringundefined🔴

url

Validate that an attribute has a valid URL format

ts
import { validate } from "robust-validator";
// ...
await validate(data, { profile: "url" });
ts
import { validate, url } from "robust-validator";
// ...
await validate(data, { profile: [url()] });
ts
import { isUrl } from "robust-validator";
// ...
isUrl("https://axe-api-com");
RuleValueIs valid?
urlnull🔴
urlundefined🔴
urlhttps://example.com🟢
urlhttp://example.com🟢
urlftp://example.com🟢
urlinvalid-url🔴

Released under the MIT License.