This is the story about how I created my own schema validation library: Schemy. I was writing a tiny API for a small app with a few friends. The packaged application weighted around 9 KB. I know, right? I told you. It was small.


Eventually, we needed to validate user input in the backend, so naturally, we came out with a few common validation library names we could use, such as Joi or AJV. The problem was their package size. The first one weighs around 500 KB, and the second one almost 1 MB!

This made us think, do we really need a library for just a few validations that weigh like 110x times our app?

Let’s start from scratch

We want to validate an object. Something composed of keys and values. To validate what the value should be, we can assign to that key some properties to check for later. We wanted something simple, like mongoose’s schemas, but you know, without all the DB stuff. Something like this:

Image for post

Our desired schema definition

Now that we know what we want, we can start coding it. We basically want a function that validates an input against this schema. In my case, I decided to write a class to keep the references more easily:

Image for post

#javascript #libraries #tips #validation #diy

You don’t always need to not reinvent the wheel
1.15 GEEK