Putting order into validation chaos

Making your front, back and app use the same schema to validate data

Jonatas Gusmao
Tech@Grupo ZAP
2 min readJun 24, 2019

--

Validations always give us some headache right? Sometimes minor issues appear in a simple project, but as your product grows more business rules are applied to it, and soon, you enter in validation chaos, with teams misreading announcements and documentation out of date about what and how data should be considered valid or not.

Well, I present to you LIVR, one of the multiple ways that exist out there to put some order into this madness, a quite simple spec designed to work with a lot of cases and the best part, easily extendable 🙌

Language Independent Validation Rules

Well, first of all, that's 👆 what LIVR stands for ok… 🤔

What does that mean? It means that LIVR is a specification of how your data should be validated, following this thought, you can validate data based on this spec with any language, all you have to do is understand each definition in the spec and use your preferred language to do that, LIVR already has a few implementations, but it’s really simple to implement a new one as we did here at Tech@GrupoZAP with this Swift implementation and to extend with this Vue Plugin.

With this spec, we are actually creating the source of truth of our validations, no matter who’ll validate it, it’ll have to comply with the spec.

Now, let’s use it

First, let's use a basic model to see how LIVR can be used to validate our data.

Here we have a complex validation to achieve, but we can do that with a simple rule using a validation schema that translates what we expect from our model and returns a comprehensible error code.

And besides being simple to implement is also easy to create custom rules and register them to LIVR.

Just register a new rule and you can use it in your schemas

Not everything is perfect, there's a small downside when we need to show users a friendly message when something is wrong, you see, LIVR only returns a simple error code, and in some scenarios, that's just isn't enough, luckily it's easy to achieve a better error response by extending LIVR registered rules like this.

This can be made after you register your custom rules (so you don't need to handle different code formats while extending rules)

Now errors will have this format

🎉

Wrapping it up, with LIVR we can apply all level of complexity to our validations and still extend it as we need, LIVR also has neat docs and an online playground where you can experiment it and understand how it works. So, give it a try and comment here what did you think.

--

--