Test JSON schema with AJV and Jest

Mosh Feu
3 min readMar 30, 2019

--

Photo by Lacie Slezak on Unsplash

While I worked on a project I was involved (Coding-Coach if you must know 😉) I faced with an interesting challenge.

The project users “database” is actually a big JSON file which contains all the entries. (Why? this is for another post)

This file has some restrictions to make sure the data will be consistency. For instance, “country” field which its value can be only from a list.

Each user can add himself to that JSON file with PR, and travis-ci is running the test which should verify the schema (among other things).

So how can we sure the new member added himself with the right schema without review it in each PR?

AJV is “The fastest JSON Schema Validator”. I didn’t check how fast it is but I can tell that its API is nice, flexible, well documented (except 1 case. We will talk about it later) and I liked to work with it.

So, how we do this?

TLD’R include the validation call in a it spec and fail the test if you get an error from the validation method.

Let’s take their example as a base:

Basic example

Let’s say, our schema should looks like:

So the user will have a fullname field and let’s say has minimum of 2 characters, the validation code will be:

ajv object has errors property — Array with the errors so if the JSON will not meet the schema, the errors will be:

[ { keyword: 'minLength',
dataPath: '[0].fullname',
schemaPath: '#/items/properties/fullname/minLength',
params: { limit: 2 },
message: 'should NOT be shorter than 2 characters' } ]

So far so good but it will not break our test.. Where is the test actually?

It’s not so hard. We just “wrap” the validation call with it.

In this case, the test will fail with the message:

● should user's schema be validexpect(received).toBeTruthy()Received: false

As you can see, I don’t know what’s wrong, I just know that the data’s schema is invalid, it’s not good enough. It will be better to show more meaningful message.

As we saw earlier, we get the errors from ajv ( ajv.errors ) remember? So we can display it when the test is failing.

In Jest (unlike other libraries), you can’t specify the error message. So, in order to display a meaningful error message, we will have to create a custom matcher to show our own message. We also, collect the field name and the index so we could know which one is the problematic and what field.

This one will show the error message we wanted:

● should user's schema be validerror with item #0's field "fullname". The error is: should NOT be shorter than 2 characters

The next field I wanted to validate is an avatar URL. It shouldn’t be a problem because with AJV we get a URL validation out of the box (Hint: by using format: ‘uri').

The problem is, our website is running on https schema so we wanted that all the resources will come from https either (to avoid Mixed Content issue). URL with http is valid so how can we verify the the URL schema?

AJV let us to define “Custom Keywords” — a custom function validation of top of the original function based on the type of the field.

Here is how:

And the output will be

● should user's schema be validerror with item #0's field "avatar". The error is: should pass "securedUrl" keyword validation

The only thing that left is to display what’s exactly secureUrl validation is.

The docs does mention how to return a custom error message

The function should return validation result as boolean. It can return an array of validation errors via .errors property of itself (otherwise a standard error will be used).

But I wasn’t sure how exactly to implement it (whoever thinks that it straightforward, well done 👏)

…well documented (except 1 case. We will talk about it later)

Remember?

So after diving into the repo in Github, I found the answer in an issue who asks exactly that.

So the full code will be

And the output will be

● should user's schema be validerror with item #0's field "avatar". The error is: avatar url must be "https" schema

And now the user could understand exactly what he / she needs to fix.

--

--

Mosh Feu
Mosh Feu

No responses yet