Following along? Download the starting code from our Getting Started repository and refactor it as we go, or jump directly to the final version with validation.
What Is Data Validation?
Data validation in Flatfile allows you to:- Check data formats and business rules
- Provide warnings and errors to guide users
- Ensure data quality before processing
- Give real-time feedback during data entry
- Field-level: Validate individual Field values (email format, date ranges, etc.)
- Record-level: Validate relationships between Fields in a single Record
- Sheet-level: Validate across all Records (duplicates, unique constraints, etc.)
Email Validation Example
This example shows how to perform email format validation directly when Records are committed. When users commit their changes, we validate that email addresses have a proper format and provide helpful feedback for any invalid emails.This approach validates Records as they’re committed, providing immediate feedback to users. For more complex validations or when you need an object-oriented approach, we recommend using the Record Hook plugin.
If you use both Record Hooks and regular listener
validators (like this one) on the same sheet, you may encounter race
conditions. Record Hooks will clear all existing messages before applying new
ones, which can interfere with any messages set elsewhere. We have ways to
work around this, but it’s a good idea to avoid using both at the same time.
What Changes We’re Making
To add validation to our basic Listener, we’ll add a listener that triggers when users commit their changes and performs validation directly:Complete Example with Validation
Here’s how to add email validation to your existing Listener:Complete Example: The full working code for this tutorial step is available in our Getting Started repository: JavaScript | TypeScript
Testing Your Validation
Local Development
To test your Listener locally, you can use theflatfile develop
command. This will start a local server that will listen for Events and respond to them, and will also watch for changes to your Listener code and automatically reload the server.
Step-by-Step Testing
After running your listener locally:Testing Steps
- Create a new space in your Flatfile environment
- Enter an invalid email address in the Email Field
- See error messages appear on invalid email Fields
- Fix the emails and see the error messages disappear
What Just Happened?
Your Listener now handles two key Events:space:configure
- Sets up the data structurecommit:created
- Validates email format when users commit changes
1. Listen for Commits
This listener triggers whenever users save their changes to any sheet in the workbook.2. Get the Records
We retrieve all records from the sheet to validate them.3. Validate Email Format
We use a simple regex pattern to check if each email follows the basicuser@domain.com
format.
4. Add Error Messages
For invalid emails, we create an update that adds an error message to that specific field.You can apply different types of validation messages:
info
: Informational messages (mouseover tooltip)warn
: Warnings that don’t block processing (yellow)error
: Errors that should be fixed, blocks Actions with thehasAllValid
constraint (red)
5. Update Records
Finally, we send all validation messages back to the sheet so users can see the errors.Next Steps
Ready to make your Listener interactive? Continue to Adding Actions to learn how to handle user submissions and create custom workflows. For more detailed information:- Understand Job lifecycle patterns in Jobs and Spaces
- Learn more about Events
- Organize your Listeners with Namespaces
- Explore plugins: Job Handler and Space Configure
- Check out Record Hook for simpler Field-level validations