Post

Check the validity of the data as early as possible

Check the validity of the data as early as possible

Story time

Recently, we had an interesting situation: someone entered “Nigeria 🇳” as a zip code. Yes, with an emoji.

Lesson

This got us talking about data validation. If we allow emojis or other unexpected characters at the point of entry, problems will pop up later. In our case it broke during a csv export. Sure, you can wrap things in a begin rescue, but that just moves the problem further down the line.

Instead, we should validate data as early as possible. Ideally, right when the user enters it, before it ever reaches the database. We should have done this directly in the form(external form, outside our Rails app), or with a validation in the model.

Summary

Implementation doesn’t matter, just don’t let “Nigeria 🇳” sneak into your zip codes. Validate early.

This post is licensed under CC BY-NC 4.0 by the author.