Validating Your Data

We’ve completed the first step of making a change to the database, and we have a Changeset struct with the changes we want to apply. But before we send it off to the database, we want to make sure that the data we’ve got is correct. Ecto provides two tools to help us check the integrity of our data: validations and constraints. They perform similar functions, but differ in the way that they’re implemented. We’ll explore each of them in the rest of this section.

Working with Validations

Validations are utility functions provided by the Ecto.Changeset module to help check the integrity of your data. If you look at the module documentation, the validation functions are easy to spot because they all start with validate_:

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.