Ajv, ajv-errors define custom message for nested properties

Viewed 34

I am using Ajv version 6 for error validation. I am also using ajv-errors for custom messages. I need to define custom error messages for nested properties, but cannot figure out how to do it. I cant seem to find such a case in the ajv-errors documentation. I'm trying to do something like the following

var Ajv = require('ajv'/*@6.11.0*/);
var ajv = new Ajv({allErrors: true, jsonPointers: true});

require('ajv-errors/*@1.0.1*/')(ajv /*, {singleError: true} */);


var schema = {
  type: 'object',
    properties: {
      foo: {
       'type': 'object',
       properties: {
        bar: { type: 'string', minLength: 2 }
       }
      },
    },
  errorMessage: {
    properties: {
      'foo.bar': 'foo.bar should be string with length >= 2'
    }
  }
};

var validate = ajv.compile(schema);
// console.log(validate({foo: 1, bar: 'a'})); // false
console.log(validate({foo: { bar: '' }})); // false
console.log(validate.errors); // processed errors

This does not work off course, any ideas how to make this work?

0 Answers
Related