Subscribe now to stay up-to-date
Developers
Ashley Mulligan
Published 12/21/2022
Just as every new day begins with possibilities, it's up to us to fill it with the things that move us toward progress and peace. At Flatfile, this looks like short functions to re-format, correct, validate, and enrich data automatically during a data import. These functions can run on specific fields and records (rows), or they can run across an entire batch of records.
Hello world, meet Field Hooks. Field Hooks — as their name suggests — clean up or validate data at the field level. Let's think about a real-world example to wrap our minds around the possibilities of Field Hooks, which — as their name suggests — clean up or validate data at the field level.
Say we want to accept data about loans, including the interest rate. We want to accept either percentages, e.g. "6.5%", or pure numbers representing a percentage, e.g. "0.065", and interpret them the same way.
Later, we want to use this data to calculate monthly payments on the loan. In that case, we'd want both "6.5%" and "0.065" to be represented as the numerical version, 0.065.
We want to accept different kinds of incoming data (in this case strings and numbers) and cast them to numbers depending on how they are formatted. We will do this in three steps:
First, we check for null
, undefined
, and empty strings, and then we cast them to null
.
Next, we check if we have a number written as a percentage. If so, we extract the numerical portion and divide it by 100.
For other strings, we attempt to parse them directly to numbers using parseFloat
. If we can, we return them as-is; if we cannot, we surface an error to the user.
cast
is present on every field.
cast
converts the initial user-inputted data to the correct type needed for that field so it can be processed.
Each of Flatfile's basic field types comes prebuilt with a default cast
hook, except for OptionField
and LinkedField
.
When cast
cannot parse an incoming value, it will display an error message in the UI and retain the original value so users can edit that value. When cast
receives null
or undefined
, it returns null
. In both cases, compute
and validate
are not called. More on compute
and validate
soon.
Check out the docs to learn more about processing data with Flatfile or get started not with CLI Quickstart.