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
| Rule | Checks | Type |
|---|---|---|
required | The value is present: not missing, not null and not an empty string. 0 and false are valid | makes the field required |
not_empty | The value is not an empty string, if the field is passed at all | string |
not_empty_list | The value is a list with at least one element | unknown[] |
any_object | The value is a plain object, its content is not validated | Record<string, unknown> |
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 |
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 |
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 | string |
equal_to_field | The value equals another field, like a password confirmation | string |
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 |
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 |
to_uc | Converts to upper case | string |
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 |
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.