javascriptnode.jsexpressajv

Should I always compile Ajv schema in express?


I want to use the Ajv validator to validate the request bodies of my express app.

To use the validator, we first need to compile the schema we want to test against, and then run the validation, like this:

const Ajv = require('Ajv');
const ajv = new Ajv();

const schema = {
  type: 'object',
  required: ['username', 'password'],
  properties: {
    username: {
      type: 'string',
      minLength: 3,
    },
    password: {
      type: 'string',
      minLength: 8,
    },
  },
};

/* What we want to validate */
const body = {
  username: 'johndoe',
  password: 'secret'
};

/* We compile the validator */
const validate = ajv.compile(schema.query)

/* We test our data to see if it is valid */
const valid = validate(body)

Should I always compile the validator when my endpoints are called, and so include it inside my routes, or can I leave the compile process outside the route body?


Solution

  • After trying out Josh Wulf's answer, I confirmed that it works compiling the validator outside the route's handler.

    Although it works as intended and it's possible to compile outside the route's handler, I noticed it comes with a drawback.

    The method of knowing which errors were detected by the validator is by calling validate.errors:

    var validate = ajv.compile(schema);
    var valid = validate(data);
    
    if (!valid) {
      console.log(validate.errors);
    }
    

    Because of this, we should not compile the validator outside of the route's handler, as there could be concurrency problems.

    As the validator is the same across different requests to the same route, accessing the validator's errors property could return another client's errors.