Data validation in Haskell
Haskell is one of my favorite programming languages, and one of the reasons I like it so much is how nicely it supports modeling string-like data with
newtypes
when doing web programming. Much of web development involves dealing with strings (idk why exactly but I imagine mostly because JavaScript and the fact that most web communication protocols use strings as primitives), but not all strings are created equal! Strings can be emails, or addresses, or routing numbers, or can be a range of possible enums, and just using typeString
to capture all of the variance is a poor data model: it doesn’t have the granularity needed to distinguish between different use cases.This is where using
newtypes
can help. Using newtypes (and their associated smart constructors) to model string-like data is a nice approach for adding granularity to strings to help differentiate string-like types from each other. Furthermore, the enhanced data modeling provided by newtypes can be extended (via aeson) all the way to JSON parsing, which makes it possible to write a JSON API client in Haskell that provides a type-safe parsing experience with ergonomic errors and a high degree of confidence with respect to data validation. It’s even easy to write tests! To explain more about what I mean, let’s dive into an example of some data modeling I’m doing for an upcoming project that takes advantage ofnewtypes
smart constructors, and custom JSON parsing with aeson.
Source: Data validation in Haskell with newtypes, smart constructors, and aeson, an article by Dylan Martin.