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:
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.