Rule definition
In React Advanced Form writing validation rules is about declaring resolver functions and applying them to a define set of fields.
Resolver
Resolver is a function that returns the next validity of the associated field.
Resolver is explicitly analyzed aside from the rest of validation context because it is a single independent unit of validation, and should be treated as such during the composition of a validation schema.
Definition
Parameters
Parameter name | Type | Description |
|
| Reactive props getter function. |
|
| The value of a field. |
|
| Props of a field. |
|
| Form component reference. |
Resolver example
It is a good practice to write high-order resolvers. Favor functional composition.
Selector
Selector is a key path of [propName, propValue]
that defines the application scope of a single or multiple validation resolvers. Think of it as a CSS selector, that controls which elements (fields) are effected by the specified rules (validation). The following selectors are supported:
type
— selects fields by their type,name
— selects fields by their name.fieldGroup
— selects the fields under the specified field group path.
Priority
Selectors are executed in a certain order (from highest to lowest):
name[fieldName]
/name[fieldName][ruleName]
type[fieldType]
/type[fieldType][ruleName]
Each selector (name
and type
) is exclusive, meaning that when any of its resolvers reject, the next selector will not be called. This does not apply to sibling rules in multiple resolvers.
Example
Multiple resolvers
Each field selector can have multiple resolvers applied in parallel by accepting an Object of the shape {[ruleName: string]: RuleResolver}
as its value.
Sibling resolvers are executed in parallel, and are not exclusive.
Having unique rule names allows to associate the rule with its validation message. Using example above, we can have different error messages for capitalLetter
and oneNumber
rules.
Grouped fields
Apart from less specific selectors (type/name), it is also possible to select fields based on their field group(s). Include the field groups key path in the schema as a value of the fieldGroup
key:
Learn more about Field grouping.
Last updated