Core Concepts
While transformers can help auto-heal your data and bring consistency, they don't cover all cases. What if the email provided is invalid or the last name is blank? Validators have been designed to address this problem and provide feedback to your end-users, so they can fix these errors.
Let's see how we can extend our previous example to make sure that both email and last name are required. We will be using pipe syntax, like in transformers.
1[ 2 { 3 "label": "First name", 4 "transformers": "trim|capitalize" 5 }, 6 { 7 "label": "Last name", 8 "transformers": "trim|capitalize", 9 "validators": "required" 10 },11 {12 "label": "Phone",13 "transformers": "trim"14 },15 {16 "label": "Email",17 "transformers": "trim|lowercase",18 "validators": "required" 19 },20 {21 "label": "Country",22 "transformers": "trim|uppercase"23 }24]
Validators can be chained using the pipe syntax. Let's say we want to make sure that the email provided is valid. We can use the email
validator.
1[ 2 { 3 "label": "First name", 4 "transformers": "trim|capitalize" 5 }, 6 { 7 "label": "Last name", 8 "transformers": "trim|capitalize", 9 "validators": "required"10 },11 {12 "label": "Phone",13 "transformers": "trim"14 },15 {16 "label": "Email",17 "transformers": "trim|lowercase",18 "validators": "required|email" 19 },20 {21 "label": "Country",22 "transformers": "trim|uppercase"23 }24]
Validator | Description |
---|---|
between:*min*,*max* |
Asserts that the field under validation has a numeric value between min and max (inclusive). |
boolean |
Asserts that the field under validation is able to be cast as a boolean. Accepted inputs are true, false, 1, 0, yes, no, y, and n. |
date:*format*,... |
Asserts that the field under validation can be matched to one of the given formats. The validation rule supports all formats supported by date-fns parse function. When no format is specified, the validation rule asserts that the field under validation can be matched to ISO 8601 format. |
email |
Asserts that the field under validation is formatted as an e-mail address. |
in:*provider* |
Asserts that the field under validation is provided by the specified data provider. |
integer |
Asserts that the field under validation is an integer. |
length:*min*,*max* |
Asserts that the field under validation has a length between the given min and max (inclusive). |
number |
Asserts that the field under validation is a number. |
required |
Asserts that the field under validation is present in the input data and is not empty. |
unique |
Asserts that the field under validation is unique across all the rows in the input data. Empty strings are ignored. |
On top of the built-in validators, you can always build your own validators so that you can report data errors and issues according to your requirements.
Validators are great when you want to validate data synchronously. However, there are cases where you want to fetch data asynchronously from your API. For that you will need to use data providers.
In the next step we are going to extend the above example to push the normalized data to our API using webhooks.