Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajv custom error message for type

i was exploring Ajv with ajv-errors for validating json schema and producing custom error messages. everything works as of now but i can't set custom error message for type for individual values.

const emailSchema = {
 type: 'object',
 required: ['foo', 'bar', 'car'],
 properties: {
  foo: { type: 'integer' },
  bar: { type: 'string' },
  car: { type: 'string' }
 },
 errorMessage: {
  type: 'should be an object',
  required: {
  foo: 'foo field is missing',
  bar: 'bar field is missing',
  car: 'car field is missing'
  }
 } 
};

outputs following error

[
    {
        "keyword": "type",
        "dataPath": "/foo",
        "schemaPath": "#/properties/foo/type",
        "params": {
            "type": "integer"
        },
        "message": "should be integer"
    },
    {
        "keyword": "errorMessage",
        "dataPath": "",
        "schemaPath": "#/errorMessage",
        "params": {
            "errors": [
                {
                    "keyword": "required",
                    "dataPath": "",
                    "schemaPath": "#/required",
                    "params": {
                        "missingProperty": "bar"
                    },
                    "message": "should have required property 'bar'"
                }
            ]
        },
        "message": "bar field is missing"
    },
    {
        "keyword": "errorMessage",
        "dataPath": "",
        "schemaPath": "#/errorMessage",
        "params": {
            "errors": [
                {
                    "keyword": "required",
                    "dataPath": "",
                    "schemaPath": "#/required",
                    "params": {
                        "missingProperty": "car"
                    },
                    "message": "should have required property 'car'"
                }
            ]
        },
        "message": "car field is missing"
    }
]

the first error object with message "should be integer", can i customize it like foo must be an Integer. I am expecting something like below but it gives be schema error.

type : {
  foo : "foo must be an Integer"
}

Thanks.

like image 437
Nikhil Pujari Avatar asked Mar 10 '18 18:03

Nikhil Pujari


People also ask

Is it possible to modify messages in AJV?

Since the property name is already in the params object, in an application you can modify the messages in any way you need. ajv-errors package allows modifying messages as well. See #127, #129, #134, #140, #193, #205, #238, #264.

What is non-empty string in AJV- errors package?

non-empty string: this string is used as a separator to concatenate messages ajv-errors package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.

What is the difference between AJV and the existing API?

The existing API is more efficient from the performance point of view. Ajv also supports asynchronous validation (with asynchronous formats and keywords) that returns a promise that either resolves to true or rejects with an error. Would errors get overwritten in case of "concurrent" validations? No.

Why does AJV assign errors as a property of validation function?

See #65, #212, #236, #242, #256. Why Ajv assigns errors as a property of validation function (or instance) instead of returning an object with validation results and errors? The reasons are history (other fast validators with the same api) and performance (returning boolean is faster).


1 Answers

You must declare errorMessage as keyword inside each of properties, see this example:

const emailSchema = {
  type: 'object',
  required: ['foo', 'bar', 'car'],
  properties: {
    foo: {
      type: 'integer',
      errorMessage: {
        // In here must be errorMessage not errorMessages
        type: 'foo must be an Integer', // Your Custom Error Message
      },
    },
    bar: { type: 'string' },
    car: { type: 'string' },
  },
  errorMessages: {
    // Change from errorMessage to errorMessages
    type: 'should be an object',
    required: {
      foo: 'foo field is missing',
      bar: 'bar field is missing',
      car: 'car field is missing',
    },
  },
}

like image 143
Squidward Tentacles Avatar answered Oct 06 '22 06:10

Squidward Tentacles