Product Features & Updates

An Introduction to Flatfile Field Validators

Brent Kulwicki

Posted 7/20/2021

While there are many features that make Flatfile's leading data onboarding platform unique, the data healing elements, such as Data Hooks, are among the most powerful. In addition to Data Hooks, there are other validations offered in Flatfile which help ensure your data is imported seamlessly (and correctly!) In this post we'll dive into the validations you can explore outside of Data Hooks.

If you haven't spent much time digging into the validators key for fields in your schema, this is where you can set up additional data validation apart from Data Hooks. To use them, you will add a validators array to a field that contains each separate validator as an object.

fields: [ 
{

key: "email",
label: "Email",
validators: [
{}
]
}
]

Now that we've checked out the basic setup, let's talk about some of the specific validators and what they do.

Keyword Validators

The keyword validators let you simply use one of our specific keywords to generate additional validations. The available keywords here are "required" and "unique" . The "required" keyword will display a validation error for any blank values for this field while the "unique" keyword will throw errors for any duplicate values in a specified field. This can be especially helpful for de-duplication of records within a file. For example, we could receive an email address on every record but also ensure that an email isn't duplicated across the file. We can set up the code as follows to obtain this behavior during imports.

fields: [ 
{
key: "email",
label: "Email",
validators: [
{validate: "unique"},
{validate: "required"}
]
}
]

Regex Validators

The next type of field validators allow you to specify a regex pattern for validating the data against. There are a few requirements to using regex within Flatfile that you can find documented here, but the important thing to note about these available regex validators is that you can assure that the data either matches or does not match the provided regex pattern by using "regex_matches" or "regex_excludes" respectively. The main difference between these and the above keyword validators is that an additional regex key is required where you provide the pattern you want to match or exclude. Here's an example with some very basic email format validation.

fields: [ 
{
key: "email",
label: "Email",
validators: [
{
validate: "regex_matches",
regex: "^([A-Za-z0-9_\\-.])+@([A-Za-z0-9_\\-.])+\\.([A-Za-z]{2,4})$"
}
]
}
]

Cross-field Validators

The last type of validators are cross-field validators. These validators can be split into two distinct categories. One being field-only validation and the other being field and value validation. An example of field-only validation would be "required_with" where we specify that a first_name field is required if we have a last_name field. An example of a field and value validation would be "required_with_values" where we specify that a first_name field is required where the last_name field's values is Jones . This is just one example of the two different types these cross-field validators. To learn about all the available options, check out the documentation here. The scenarios via code are below:

fields: [ 
{
key: "first_name",
label: "First Name",
validators: [
{validate: "required_with", fields: ["last_name"]},
]
},
{
key: "last_name",
label: "Last Name"
}
]

One final note on all the above validations is custom error messaging. While not shown in the above examples, there is an optional error key that accepts a string value. When used, the validators will show your customer an error message in the UI as opposed to the Flatfile standard messaging.

fields: [ 
{
key: "first_name",
label: "First Name",
validators: [
{
validate: "required",
error: "This is my custom error message"
},
]
}
]