Shapeless and Applicative Functors for Server side validations

Audience

This is targeted for those who know type classes (Haskell type class taken to Scala), Generic Repr in shapeless, applicative functors, applicative builder pattern in Scalaz/Cats (and optionally tagged type)

Problem(s)

A typical example of using applicative builders is when you need to validate an entity where the validation of each field in the entity returns an applicative functor. And when you have dozens of such entities (never dealt with it until yesterday as I am writing this) sharing fields across each other, you finally have a bloated boiler plate of Advanced (for some) Scala.

(column1.validate|@| column2.validate|@| column3.successNel[ValidationError]) {Entity.apply}

Solution

What do you think of a solution that provides the following as an alternative?

Let’s write some code

The validation behaviour is encoded as a type class, with a validate function returning an applicative functor.

--

--

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Afsal Thaj

Afsal Thaj

A software engineer and a functional programming enthusiast at Simple-machines, Sydney, and a hardcore hiking fan. https://twitter.com/afsalt2