Skip to content

Built-in rules ​

livr implements LIVR 2.0 and LIVR 2.5: every rule of the LIVR specification is built in. This page is a short list with the TypeScript type of every rule, in the groups of the specification. The full reference, with the arguments, error codes and examples of every rule, is on the specification site:

All validation rules Every rule with its arguments, error codes and examples. Opens livr-spec.org, the site of the LIVR specification.

Rule names below link to their pages in the specification. The last column shows the TypeScript type that InferFromSchema gives a field with this rule. Rules that check a value keep a more precise type of earlier rules: [{ one_of: ['a', 'b'] }, { max_length: 1 }] is 'a' | 'b', not string.

Rule names work in snake_case and in camelCase: min_length and minLength are the same rule.

Rules marked 2.5 were added in LIVR 2.5, see below what they have in common.

Common rules ​

RuleChecksType
requiredThe value is present: not missing, not null and not an empty string. 0 and false are validthe type of earlier rules, field becomes required
not_emptyThe value is not an empty string, if the field is passed at allstring, or the type of earlier rules
not_empty_listThe value is a list with at least one elementunknown[], or the list type of earlier rules, field becomes required
any_objectThe value is a plain object, its content is not validatedRecord<string, unknown>
required_if2.5The field is required when other fields have the given values: { required_if: { type: 'company' } }the type of earlier rules
any2.5Any value passes, unchanged: a rule for a field that is kept without checksunknown, or the type of earlier rules
boolean2.5true, false, 1, 0, "1", "0", "true" or "false", returned as a booleanboolean

String rules ​

RuleChecksType
stringThe value is a primitive, returned as a stringstring
eqThe value equals the given onethe literal
one_ofThe value is one of the allowed valuesa union of the literals
max_lengthThe string is not longer than the given lengthstring
min_lengthThe string is at least the given lengthstring
length_betweenThe length is between two values, inclusivestring
length_equalThe string has exactly the given lengthstring
likeThe value matches a regular expressionstring
not_one_of2.5The value is none of the given valuesstring | number | boolean, or the type of earlier rules
digits2.5The string has only the digits 0-9string
starts_with2.5, ends_with2.5, contains2.5The string starts with, ends with or contains one of the given stringsstring

Numeric rules ​

Numeric rules accept strings that look like numbers, such as '42' from a form, and return numbers.

RuleChecksType
integerThe value is an integernumber
positive_integerThe value is an integer greater than zeronumber
decimalThe value is a number, an integer or a decimalnumber
positive_decimalThe value is a number greater than zeronumber
max_numberThe number is not greater than the given valuenumber
min_numberThe number is not less than the given valuenumber
number_betweenThe number is between two values, inclusivenumber
max_decimals2.5The number has at most the given count of decimal placesnumber

Special rules ​

RuleChecksType
emailThe value is an email addressstring
urlThe value is an http or https URLstring
iso_dateThe value is an ISO 8601 date without time, like 2014-08-14. LIVR 2.5 adds bounds2.5: { iso_date: { min, max } } with dates, today, yesterday or tomorrow (days in UTC). The 2.0 forms are unchangedstring
iso_datetime2.5An ISO 8601 date and time with an offset, returned as the instant in UTC, like 2026-01-02T01:04:05.000Z. Also with { min, max }string
iso_time2.5A time HH:MM, HH:MM:SS or with a fraction, without an offset. Also with { min, max }, a range over midnight when min is after maxstring
equal_to_fieldThe value equals another field, like a password confirmationstring, or the type of earlier rules

Metarules ​

Metarules describe nested data with other rules.

RuleDescribesType
nested_objectA nested objectthe object type
variable_objectAn object whose rules depend on a selector fielda union of the object types
list_ofA list, the rules apply to every elementan array
list_of_objectsA list of objectsan array of the object type
list_of_different_objectsA list of objects whose rules depend on a selector fieldan array of the union
orAlternatives: the first set of rules that succeeds winsa union
when2.5Rules that depend on another field: { when: ['type', { eq: 'company' }, 'required'] }, with rules for the other case, or cases by valuea union of the types of the branches
values2.5An object, the rules apply to every value: { values: 'integer' }Record<string, T>
keys2.5An object, the rules check every key: { keys: { one_of: ['en', 'uk'] } }the object type of earlier rules

List rules ​

All list rules were added in LIVR 2.5.

RuleChecksType
list_lengthThe list has the given length, or a length between min and maxthe list type of earlier rules
uniqueThe items of the list are unique, or the values of the given keys of its objectsthe list type of earlier rules

Format rules ​

All format rules were added in LIVR 2.5.

RuleChecksType
uuidA UUID (RFC 9562), of the given versions 'v1'-'v8'string
ip, cidrAn IPv4 or IPv6 address, or a network in CIDR notation, 'v4' or 'v6' onlystring
hostnameA DNS host namestring
base64Base64, 'url' for the URL-safe alphabetstring
hex, digestHex digits, or a hex digest of md5, sha1, sha256, sha384 or sha512string
mongo_idA MongoDB ObjectId, 24 hex digitsstring
hex_colorA CSS hex color, like #fff or #ffffff80string
macA MAC address, 48 bits or with 64 an EUI-64string
e164A phone number in the E.164 format, like +380441234567string
semverA semantic version, like 1.2.3-beta.1string
jwtA JSON Web Token in the compact form. The signature is not checkedstring

Checksum rules ​

All checksum rules were added in LIVR 2.5. They check the value as it is: remove spaces and dashes before them, with remove.

RuleChecksType
luhnAt least 2 digits with a valid Luhn checksumstring
credit_cardA payment card number: 12-19 digits with a valid Luhn checksumstring
ibanAn IBAN with valid check digitsstring

Modifiers ​

Modifiers change the output and never return errors.

RuleDoesType
trimRemoves leading and trailing whitespacestring
to_lcConverts to lower casestring, literal types in lower case
to_ucConverts to upper casestring, literal types in upper case
removeRemoves the given charactersstring
leave_onlyRemoves all characters except the given onesstring
defaultSets a value when the value is emptythe type of the default, field becomes required
split2.5Splits a string at the separator into a list of stringsstring[]

About the LIVR 2.5 rules ​

LIVR 2.5 added the rules marked 2.5, the list, format and checksum rules, and bounds for iso_date, see the LIVR 2.5 specification. It only adds rules: every LIVR 2.0 schema gives the same result. Implementations of LIVR 2.0 in other languages do not have these rules.

The rules are loaded on first use, so a schema without them costs nothing. A rule you register under one of these names replaces the built-in one, so schemas with boolean, uuid or required_if of livr-extra-rules keep working while the package is registered.

Like the LIVR 2.0 rules, they skip a missing value, null and an empty string, so they work with required; only required_if checks them. A rule without arguments rejects any argument, { boolean: null } included, and an unknown option is a schema error: a schema says what it checks.

More rules ​

livr-extra-rules adds more rules, like ipv4 and md5; most of its other rules are built in now, as LIVR 2.5 rules. You can also write your own.