Creating a Validation Library in JS Using Propositional Calculus
One of the cornerstones of building the bank of the future is ensuring the correctness and validity of our data. Banking is, after all, an industry that demands extreme precision and correctness.
Recently the platform engineering team at Earnest wrote a validation library that developers can then use to easily compose rules around any set of data and, well, validate it. In order to maximize reusability as well as establish confidence in the correctness of our validations we opted to write a validation library.
For example, here is what the `isEqual` and `isAfter` predicates look like.
While these look deceptively simple, they allow for currying which can be incredibly powerful when writing a series of validations that use some of the same values.
Moving on to the combinators… to get an idea of the combinators we wrote that can combine predicates check out the `S`, `K`, `I`, `alwaysFalse`, and `and` combinator as well as `and`’s helper method — `variadic` — below.
Here’s an example of two of the many tests we wrote using the basic and derived argument forms from the propositional calculus table linked to above:
This is ✨super cool✨ because the combination of testing all of these laws and property-based testing insures that our logic is sound even before we write our first validation rule! We are leveraging already proven mathematical laws to ensure correctness in our validation library.
At this point you may be asking yourself, “Why not simply write a bunch of `if` statements? This seems overkill!”. Well, I’m glad you asked. Mathematics has already codified these rules. Calculus has already given us a system to do exactly what we needed to do with this project. If we’d have went the route of writing `if else` statements for every rule we’d have to write *a lot* more tests for each and every rule, have a much higher chance of logic errors, writing rules would take a lot more time, rules would be incredibly less readable, and — finally — because we can reuse predicates we have a much more standardized approach ensuring stuff like `is between two dates` and `this or that` are done exactly the same every time. In short — the rule engine gives us a strictly smaller domain specific language (DSL) where we can reason about rules and validation in a declarative way.
We’re incredibly proud of this library as well as the validations (and ultimately the huge business value) we were able to quickly create as a result of.
And if any of that sounds interesting to you Earnest is always looking for more great engineers to join the team!