rules library
The builder functions of all built-in rules, to compose rules of your own.
Functions
-
any(
List< Object?> args, RuleContext context) → LivrRule -
any: any value passes unchanged, objects, lists and empty values too. Unlike a field without rules, the value stays in the output. -
anyObject(
List< Object?> args, RuleContext context) → LivrRule -
any_object: the value must be an object (aMap). Its content is not validated. -
base64(
List< Object?> args, RuleContext context) → LivrRule -
base64: the value must be Base64 (RFC 4648): the alphabetA-Za-z0-9+/, a length that is a multiple of 4, and the padding=or==only at the end.{base64: 'url'}takes the URL-safe alphabetA-Za-z0-9-_, with the padding complete or absent. No whitespace. The output is the value as a string. -
boolean(
List< Object?> args, RuleContext context) → LivrRule -
boolean: the value must betrue,false,1,0,'1','0','true'or'false', and the output is abool. Strings are case-sensitive:'True'is not a boolean. -
cidr(
List< Object?> args, RuleContext context) → LivrRule -
cidr: the value must be an IP address,/and a prefix length,0-32for IPv4 and0-128for IPv6, without leading zeros:10.0.0.0/8,2001:db8::/32. Host bits may be set:10.0.0.1/8passes.{cidr: 'v4'}accepts only IPv4,{cidr: 'v6'}only IPv6. The output is the value as a string. -
contains(
List< Object?> args, RuleContext context) → LivrRule -
contains: the value must contain one of the arguments, non-empty strings:{contains: '@'}. The comparison is exact and case-sensitive. The output is the value as a string. -
creditCard(
List< Object?> args, RuleContext context) → LivrRule -
credit_card: the value must be a card number of 12-19 digits (ISO/IEC 7812) with a valid Luhn checksum, without card network detection. It is checked as it is: spaces and dashes are not removed. The output is the value as a string. -
decimal(
List< Object?> args, RuleContext context) → LivrRule -
decimal: the value must be a number, an integer or a decimal. Strings are accepted in the form-12or-12.34, and the output is always adouble. -
defaultValue(
List< Object?> args, RuleContext context) → LivrRule -
default: sets the value when it is empty: missing,nullor an empty string. Other values,0included, are kept. -
digest(
List< Object?> args, RuleContext context) → LivrRule -
digest: the value must be a hexadecimal digest of one of the given algorithms,md5(32 digits),sha1(40),sha256(64),sha384(96) orsha512(128):{digest: 'sha256'},{digest: ['md5', 'sha1']}. The output is the value as a string. -
digits(
List< Object?> args, RuleContext context) → LivrRule -
digits: the value must be one or more ASCII digits0-9. Leading zeros are kept, and the output is a string:'007'stays'007',7becomes'7'. -
e164(
List< Object?> args, RuleContext context) → LivrRule -
e164: the value must be a phone number in the E.164 format:+, then 7 to 15 digits, the first one is not0. The output is the value as a string. -
email(
List< Object?> args, RuleContext context) → LivrRule -
email: the value must be an email address. -
endsWith(
List< Object?> args, RuleContext context) → LivrRule -
ends_with: the value must end with one of the arguments, non-empty strings:{ends_with: '.pdf'}. The comparison is exact and case-sensitive. The output is the value as a string. -
eq(
List< Object?> args, RuleContext context) → LivrRule -
eq: the value must be equal to the argument. Values are compared as strings, and the output is the argument. -
equalToField(
List< Object?> args, RuleContext context) → LivrRule -
equal_to_field: the value must be equal to the value of another field. Values are compared as strings:'10'and10are equal. A missing field is not equal to any value. -
hex(
List< Object?> args, RuleContext context) → LivrRule -
hex: the value must be hexadecimal digits0-9,a-forA-F, without a0xprefix. The output is the value as a string. -
hexColor(
List< Object?> args, RuleContext context) → LivrRule -
hex_color: the value must be#and 3, 4, 6 or 8 hexadecimal digits:#RGB,#RGBA,#RRGGBBor#RRGGBBAA. The output is the value as a string. -
hostname(
List< Object?> args, RuleContext context) → LivrRule -
hostname: the value must be a host name (RFC 1123): labels of 1-63 letters, digits and-, not starting or ending with-, separated by., at most 253 characters, no trailing dot. A single label likelocalhostpasses. The last label is not all digits, so an IPv4 address is not a host name. The output is the value as a string. -
iban(
List< Object?> args, RuleContext context) → LivrRule -
iban: the value must be an IBAN (ISO 13616): 2 lettersA-Z, 2 digits and 11-30 lettersA-Zor digits, with a valid checksum (mod 97). It is checked as it is: spaces are not removed and lowercase letters fail. No per-country lengths. The output is the value as a string. -
integer(
List< Object?> args, RuleContext context) → LivrRule -
integer: the value must be an integer. Strings with an integer ('10','1e3') are accepted, and the output is always anint. -
ip(
List< Object?> args, RuleContext context) → LivrRule -
ip: the value must be an IPv4 or an IPv6 address;{ip: 'v4'}accepts only IPv4,{ip: 'v6'}only IPv6. IPv4 numbers have no leading zeros (010.0.0.1fails: some systems read it as octal); IPv6 is in the text forms of RFC 4291, without a zone or brackets. The output is the value as a string. -
isoDate(
List< Object?> args, RuleContext context) → LivrRule -
iso_date: the value must be an ISO 8601 date without time, like2014-08-14. -
isoDatetime(
List< Object?> args, RuleContext context) → LivrRule -
iso_datetime: the value must be a date and a time with an offset,YYYY-MM-DDTHH:MM[:SS[.fraction]]andZor+HH:MM/-HH:MM. The output is the instant in UTC with three fraction digits, cut off, not rounded:'2026-10-29T10:00:00.123456+02:00'becomes'2026-10-29T08:00:00.123Z'. -
isoTime(
List< Object?> args, RuleContext context) → LivrRule -
iso_time: the value must be a time of day without an offset,HH:MM,HH:MM:SSorHH:MM:SS.fractionwith 1-9 digits, as<input type="time">sends it. The value is not changed. -
jwt(
List< Object?> args, RuleContext context) → LivrRule -
jwt: the value must have the structure of a JSON Web Token (the JWS compact serialization): three base64url segments without padding, separated by., the last one may be empty (an unsecured JWT). The segments are not decoded or verified. The output is the value as a string. -
keys(
List< Object?> args, RuleContext context) → LivrRule -
keys: the value must be an object, and every key of it is validated as a string with the rules:{keys: {'one_of': ['uk', 'en']}}. A key that fails isWRONG_KEYunder that key. The rules only check: the keys and the value do not change. -
leaveOnly(
List< Object?> args, RuleContext context) → LivrRule -
leave_only: Removes all characters except the given ones. Numbers and booleans are converted to strings, objects and lists are left as they are. -
lengthBetween(
List< Object?> args, RuleContext context) → LivrRule -
length_between: the string must have frommintomaxcharacters (Unicode code points), inclusive. The output is the value as a string. -
lengthEqual(
List< Object?> args, RuleContext context) → LivrRule -
length_equal: the string must have exactly the given number of characters (Unicode code points). The output is the value as a string. -
like(
List< Object?> args, RuleContext context) → LivrRule -
like: the value must match the regular expression. Flags are the second argument:{like: ['^[a-z]+$', 'i']}; onlyi(ignore case) is supported. The output is the value as a string. -
listLength(
List< Object?> args, RuleContext context) → LivrRule -
list_length: the value must be a list with the given number of elements:{list_length: 2},{list_length: [1, 10]}(min and max) or{list_length: {'min': 1}}. An empty list is a value, not an empty value. -
listOf(
List< Object?> args, RuleContext context) → LivrRule -
list_of: validates every element of a list with the same rules. Errors are a list with the error of every element,nullfor valid elements. Both the new syntax{list_of: ['required', 'email']}and the old one{list_of: [['required', 'email']]}work. -
listOfDifferentObjects(
List< Object?> args, RuleContext context) → LivrRule -
list_of_different_objects: validates every element of a list with one of several object schemas. The value of the selector field of an element chooses the schema, like invariable_object. -
listOfObjects(
List< Object?> args, RuleContext context) → LivrRule -
list_of_objects: validates every element of a list with the same object schema. Errors are a list with the errors of every element,nullfor valid elements. -
luhn(
List< Object?> args, RuleContext context) → LivrRule -
luhn: the value must be at least 2 digits with a valid Luhn (mod 10) checksum. It is checked as it is: spaces and dashes are not removed. The output is the value as a string. -
mac(
List< Object?> args, RuleContext context) → LivrRule -
mac: the value must be a MAC address, six pairs of hexadecimal digits (48 bits, MAC-48 or EUI-48) separated by:or by-, the same separator everywhere:00:1A:2b:3c:4D:5e.{mac: 64}takes eight pairs (EUI-64). The output is the value as a string. -
maxDecimals(
List< Object?> args, RuleContext context) → LivrRule -
max_decimals: the number must have at most the given number of digits after the decimal point:{max_decimals: 2}. Trailing zeros do not count, so'1.230'and1.230have 2. Strings are accepted in the form-12or-12.34, and the output is the number. -
maxLength(
List< Object?> args, RuleContext context) → LivrRule -
max_length: the string must have at most the given number of characters (Unicode code points). The output is the value as a string. -
maxNumber(
List< Object?> args, RuleContext context) → LivrRule -
max_number: the number must be at most the argument. Strings with a number are accepted, and the output is the number. -
minLength(
List< Object?> args, RuleContext context) → LivrRule -
min_length: the string must have at least the given number of characters (Unicode code points). The output is the value as a string. -
minNumber(
List< Object?> args, RuleContext context) → LivrRule -
min_number: the number must be at least the argument. Strings with a number are accepted, and the output is the number. -
mongoId(
List< Object?> args, RuleContext context) → LivrRule -
mongo_id: the value must be a BSON ObjectId, 24 hexadecimal digits. The output is the value as a string. -
nestedObject(
List< Object?> args, RuleContext context) → LivrRule -
nested_object: validates a nested object with its own schema. Errors have the shape of the object. -
notEmpty(
List< Object?> args, RuleContext context) → LivrRule -
not_empty: the value must not be an empty string, if it is present. -
notEmptyList(
List< Object?> args, RuleContext context) → LivrRule -
not_empty_list: the value must be a list with at least one element. -
notOneOf(
List< Object?> args, RuleContext context) → LivrRule -
not_one_of: the value must not be one of the arguments:{not_one_of: ['admin', 'root']}. Values are compared as strings, so1equals'1'. The output is the value unchanged. -
numberBetween(
List< Object?> args, RuleContext context) → LivrRule -
number_between: the number must be frommintomax, inclusive. Strings with a number are accepted, and the output is the number. -
oneOf(
List< Object?> args, RuleContext context) → LivrRule -
one_of: the value must be one of the arguments. Values are compared as strings, and the output is the allowed value. Both the new syntax{one_of: ['a', 'b']}and the old one{one_of: [['a', 'b']]}work. -
or(
List< Object?> args, RuleContext context) → LivrRule -
or: applies the sets of rules one after another until one of them succeeds:{or: ['email', 'positive_integer']}. When all of them fail, the error of the last one is returned. -
positiveDecimal(
List< Object?> args, RuleContext context) → LivrRule -
positive_decimal: the value must be a number greater than zero. Strings are accepted in the form12or12.34, and the output is always adouble. -
positiveInteger(
List< Object?> args, RuleContext context) → LivrRule -
positive_integer: the value must be an integer greater than zero. Strings with an integer are accepted, and the output is always anint. -
remove(
List< Object?> args, RuleContext context) → LivrRule -
remove: Removes the given characters from the string. Numbers and booleans are converted to strings, objects and lists are left as they are. -
required(
List< Object?> args, RuleContext context) → LivrRule -
required: the value must be present, notnulland not an empty string.0andfalseare valid values. -
requiredIf(
List< Object?> args, RuleContext context) → LivrRule -
required_if: the value is required when other fields have the given values:{required_if: {'type': 'company', 'address/country': ['UA']}}. Keys are references to fields of the object (see parseReference), values are primitive values or lists of them, compared as strings with the input of the referenced fields. When all of them match, an empty value isREQUIRED. -
semver(
List< Object?> args, RuleContext context) → LivrRule -
semver: the value must be a version of Semantic Versioning 2.0.0, like1.2.3,1.0.0-rc.1or1.0.0+build.5, without avprefix. The output is the value as a string. -
split(
List< Object?> args, RuleContext context) → LivrRule -
split: Splits a string at every separator into a list of strings:{split: ','}turns'1,2,3'into['1', '2', '3'], empty parts are kept. A number or a boolean is split in its string form (5becomes['5']), a list stays as it is, empty values and objects are left to the next rules. -
startsWith(
List< Object?> args, RuleContext context) → LivrRule -
starts_with: the value must start with one of the arguments, non-empty strings:{starts_with: ['https://', 'http://']}. The comparison is exact and case-sensitive. The output is the value as a string. -
string(
List< Object?> args, RuleContext context) → LivrRule -
string: the value must be a primitive value, it is converted to a string. -
toLc(
List< Object?> args, RuleContext context) → LivrRule -
to_lc: Converts the string to lower case. Numbers and booleans are converted to strings, objects and lists are left as they are. -
toUc(
List< Object?> args, RuleContext context) → LivrRule -
to_uc: Converts the string to upper case. Numbers and booleans are converted to strings, objects and lists are left as they are. -
trim(
List< Object?> args, RuleContext context) → LivrRule -
trim: Removes leading and trailing whitespace. Numbers and booleans are converted to strings, objects and lists are left as they are. -
unique(
List< Object?> args, RuleContext context) → LivrRule -
unique: the elements of the list must be unique. Without arguments the elements are primitive values, compared as strings (1and'1'are duplicates). With keys,{unique: ['country', 'zip']}, the elements are objects, duplicates when the values of all the keys are equal. Empty elements and objects with an empty key value are ignored. Duplicates are oneNOT_UNIQUE_ITEMSfor the field, an element of a wrong type isFORMAT_ERROR. -
url(
List< Object?> args, RuleContext context) → LivrRule -
url: the value must be an http or https URL, with the protocol. -
uuid(
List< Object?> args, RuleContext context) → LivrRule -
uuid: the value must be a UUID (RFC 9562),8-4-4-4-12hexadecimal digits. Without arguments the version is 1-8 and the variant is RFC 9562, and the nil and the max UUID pass too. With versions,{uuid: 'v4'}or{uuid: ['v4', 'v7']}, the version must be one of them. The output is the value as a string. -
values(
List< Object?> args, RuleContext context) → LivrRule -
values: the value must be an object with any keys, and every value of it is validated with the rules, aslist_ofvalidates every element:{values: ['required', 'positive_integer']}. Errors are an object with the failing keys only, the output is an object with the same keys. -
variableObject(
List< Object?> args, RuleContext context) → LivrRule -
variable_object: validates an object with one of several schemas. The value of the selector field chooses the schema:{variable_object: ['type', {'material': {...}, 'service': {...}}]}. -
when(
List< Object?> args, RuleContext context) → LivrRule -
when: validates the value with rules that depend on another field.