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:
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
| Rule | Checks | Type |
|---|---|---|
required | The value is present: not missing, not null and not an empty string. 0 and false are valid | the type of earlier rules, field becomes required |
not_empty | The value is not an empty string, if the field is passed at all | string, or the type of earlier rules |
not_empty_list | The value is a list with at least one element | unknown[], or the list type of earlier rules, field becomes required |
any_object | The value is a plain object, its content is not validated | Record<string, unknown> |
required_if2.5 | The field is required when other fields have the given values: { required_if: { type: 'company' } } | the type of earlier rules |
any2.5 | Any value passes, unchanged: a rule for a field that is kept without checks | unknown, or the type of earlier rules |
boolean2.5 | true, false, 1, 0, "1", "0", "true" or "false", returned as a boolean | boolean |
String rules
| Rule | Checks | Type |
|---|---|---|
string | The value is a primitive, returned as a string | string |
eq | The value equals the given one | the literal |
one_of | The value is one of the allowed values | a union of the literals |
max_length | The string is not longer than the given length | string |
min_length | The string is at least the given length | string |
length_between | The length is between two values, inclusive | string |
length_equal | The string has exactly the given length | string |
like | The value matches a regular expression | string |
not_one_of2.5 | The value is none of the given values | string | number | boolean, or the type of earlier rules |
digits2.5 | The string has only the digits 0-9 | string |
starts_with2.5, ends_with2.5, contains2.5 | The string starts with, ends with or contains one of the given strings | string |
Numeric rules
Numeric rules accept strings that look like numbers, such as '42' from a form, and return numbers.
| Rule | Checks | Type |
|---|---|---|
integer | The value is an integer | number |
positive_integer | The value is an integer greater than zero | number |
decimal | The value is a number, an integer or a decimal | number |
positive_decimal | The value is a number greater than zero | number |
max_number | The number is not greater than the given value | number |
min_number | The number is not less than the given value | number |
number_between | The number is between two values, inclusive | number |
max_decimals2.5 | The number has at most the given count of decimal places | number |
Special rules
| Rule | Checks | Type |
|---|---|---|
email | The value is an email address | string |
url | The value is an http or https URL | string |
iso_date | The 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 unchanged | string |
iso_datetime2.5 | An 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.5 | A 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 max | string |
equal_to_field | The value equals another field, like a password confirmation | string, or the type of earlier rules |
Metarules
Metarules describe nested data with other rules.
| Rule | Describes | Type |
|---|---|---|
nested_object | A nested object | the object type |
variable_object | An object whose rules depend on a selector field | a union of the object types |
list_of | A list, the rules apply to every element | an array |
list_of_objects | A list of objects | an array of the object type |
list_of_different_objects | A list of objects whose rules depend on a selector field | an array of the union |
or | Alternatives: the first set of rules that succeeds wins | a union |
when2.5 | Rules that depend on another field: { when: ['type', { eq: 'company' }, 'required'] }, with rules for the other case, or cases by value | a union of the types of the branches |
values2.5 | An object, the rules apply to every value: { values: 'integer' } | Record<string, T> |
keys2.5 | An 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.
| Rule | Checks | Type |
|---|---|---|
list_length | The list has the given length, or a length between min and max | the list type of earlier rules |
unique | The items of the list are unique, or the values of the given keys of its objects | the list type of earlier rules |
Format rules
All format rules were added in LIVR 2.5.
| Rule | Checks | Type |
|---|---|---|
uuid | A UUID (RFC 9562), of the given versions 'v1'-'v8' | string |
ip, cidr | An IPv4 or IPv6 address, or a network in CIDR notation, 'v4' or 'v6' only | string |
hostname | A DNS host name | string |
base64 | Base64, 'url' for the URL-safe alphabet | string |
hex, digest | Hex digits, or a hex digest of md5, sha1, sha256, sha384 or sha512 | string |
mongo_id | A MongoDB ObjectId, 24 hex digits | string |
hex_color | A CSS hex color, like #fff or #ffffff80 | string |
mac | A MAC address, 48 bits or with 64 an EUI-64 | string |
e164 | A phone number in the E.164 format, like +380441234567 | string |
semver | A semantic version, like 1.2.3-beta.1 | string |
jwt | A JSON Web Token in the compact form. The signature is not checked | string |
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.
| Rule | Checks | Type |
|---|---|---|
luhn | At least 2 digits with a valid Luhn checksum | string |
credit_card | A payment card number: 12-19 digits with a valid Luhn checksum | string |
iban | An IBAN with valid check digits | string |
Modifiers
Modifiers change the output and never return errors.
| Rule | Does | Type |
|---|---|---|
trim | Removes leading and trailing whitespace | string |
to_lc | Converts to lower case | string, literal types in lower case |
to_uc | Converts to upper case | string, literal types in upper case |
remove | Removes the given characters | string |
leave_only | Removes all characters except the given ones | string |
default | Sets a value when the value is empty | the type of the default, field becomes required |
split2.5 | Splits a string at the separator into a list of strings | string[] |
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.