Skip to content

Built-in rules ​

Every rule of the LIVR specification is built in. This page is a short list with the TypeScript type of every rule. 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.

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

Common rules ​

RuleChecksType
requiredThe value is present: not missing, not null and not an empty string. 0 and false are validmakes the field required
not_emptyThe value is not an empty string, if the field is passed at allstring
not_empty_listThe value is a list with at least one elementunknown[]
any_objectThe value is a plain object, its content is not validatedRecord<string, unknown>

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

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

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-14string
equal_to_fieldThe value equals another field, like a password confirmationstring

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

Modifiers ​

Modifiers change the output and never return errors.

RuleDoesType
trimRemoves leading and trailing whitespacestring
to_lcConverts to lower casestring
to_ucConverts to upper casestring
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

More rules ​

livr-extra-rules adds boolean, ipv4, uuid, credit_card, iso_date with ranges, required_if, list_length and more. You can also write your own.