Skip to content

Validation rules ​

The standard rules of the specification. Open a rule to see how it works and to try it live, or filter the list by an error code to find the rule that returns it.

Rules marked 2.5 were added in LIVR 2.5. Implementations of LIVR 2.0 do not have them, the conformance table shows which implementations pass the 2.5 test suite.

68 rules
Common rulesCheck that a value is present, or that it is an object or a boolean.
String rulesCompare and measure strings. Numbers are validated as strings.
stringChecks that the value is a primitive type and converts it to a string, for example 25 becomes "25". Prefer stricter rules where possible.'string'FORMAT_ERROReqChecks that the value is equal to the given one. Values are compared as strings, and the output is the value from the rule.{ eq: 'Anton' }NOT_ALLOWED_VALUEone_ofChecks that the value is one of the allowed values. Values are compared as strings, and the output is the value from the rule.{ one_of: ['a', 'b'] }NOT_ALLOWED_VALUEmax_lengthChecks that the string is not longer than the given number of characters.{ max_length: 10 }TOO_LONGmin_lengthChecks that the string is at least the given number of characters long.{ min_length: 2 }TOO_SHORTlength_betweenChecks that the length of the string is between the two values, inclusive.{ length_between: [2, 10] }TOO_LONGTOO_SHORTlength_equalChecks that the string is exactly the given number of characters long.{ length_equal: 2 }TOO_LONGTOO_SHORTlikeChecks that the value matches a regular expression.{ like: '^[0-9]+$' }WRONG_FORMATnot_one_of2.5Checks that the value is not equal to any of the given values. Values are compared as strings, and the output is the value unchanged.{ not_one_of: ['admin', 'root'] }NOT_ALLOWED_VALUEdigits2.5Checks that the value consists of ASCII digits 0-9 only. Unlike positive_integer, leading zeros are kept and the output is a string.'digits'NOT_DIGITSstarts_with2.5Checks that the value starts with one of the given strings.{ starts_with: 'https://' }WRONG_FORMATends_with2.5Checks that the value ends with one of the given strings.{ ends_with: '.pdf' }WRONG_FORMATcontains2.5Checks that the value contains at least one of the given strings.{ contains: '@' }WRONG_FORMAT
Numeric rulesCheck numbers and strings that look like numbers, and return them as numbers.
Special rulesEmails, URLs, dates and times, and comparison with another field.
MetarulesDescribe nested objects, lists, conditions and alternatives with other rules.
List rulesCheck the length of a list and that its items are unique.
Format rulesIdentifiers, network addresses, encodings and other common string formats.
uuid2.5Checks that the value is a UUID (RFC 9562), of any version or of the given versions.'uuid', { uuid: 'v4' }WRONG_UUIDip2.5Checks that the value is an IPv4 or an IPv6 address.'ip', { ip: 'v4' }WRONG_IPcidr2.5Checks that the value is an IP address with a prefix length, for example 192.0.2.0/24.'cidr', { cidr: 'v4' }WRONG_CIDRhostname2.5Checks that the value is a hostname (RFC 1123), for example api.example.com or localhost.'hostname'WRONG_HOSTNAMEbase642.5Checks that the value is a Base64 string (RFC 4648).'base64'FORMAT_ERRORhex2.5Checks that the value is a string of hex digits 0-9a-fA-F, without the 0x prefix.'hex'WRONG_HEXdigest2.5Checks that the value is a hex digest of one of the given hash algorithms.{ digest: 'sha256' }WRONG_DIGESTmongo_id2.5Checks that the value is a BSON ObjectId: 24 hex digits.'mongo_id'WRONG_MONGO_IDhex_color2.5Checks that the value is a hex color: # and 3, 4, 6 or 8 hex digits, for example #f80 or #FF8800.'hex_color'WRONG_HEX_COLORmac2.5Checks that the value is a MAC address: six pairs of hex digits separated by : or by -. { mac: 64 } checks a 64-bit EUI-64 identifier: eight pairs.'mac', { mac: 64 }WRONG_MACe1642.5Checks that the value is a phone number in the E.164 format: +, then 7–15 digits, the first of which is not 0.'e164'FORMAT_ERRORsemver2.5Checks that the value is a semantic version, for example 1.2.3 or 2.0.0-rc.1+build.5.'semver'WRONG_SEMVERjwt2.5Checks that the value has the structure of a JSON Web Token: header.payload.signature in base64url, the JWS compact serialization (RFC 7515, section 7.1). The token is not decoded or verified.'jwt'WRONG_JWT
Checksum rulesNumbers with a check digit: Luhn, payment cards and IBANs.
ModifiersChange the data. Modifiers never return errors.

Empty values are skipped ​

All standard rules skip empty values: a missing field, null and an empty string "". Only required, not_empty and not_empty_list check that a value is present (in LIVR 2.5 also required_if). So the same rules work for optional fields:

js
first_name: { min_length: 10 }               // optional: the length is checked only if "first_name" is passed
first_name: ['required', { min_length: 10 }] // the name must be present and at least 10 characters long

Rules that expect a primitive value (a string, a number or a boolean) return FORMAT_ERROR when they receive an object or a list. See Error codes.

Learn more ​