Ajv: Duplicate error with "coerceTypes" option

Created on 24 Apr 2017  路  2Comments  路  Source: ajv-validator/ajv

There seems to be an issue with unsupported formats. I don't have control over what's passed in as a format for certain types in the json schema. I tried ignoring them (unknownFormats: 'ignore') and adding them (addFormat) as described below. I always end up getting duplicate error messages for the same fields. If I remove the format keyword in the schema, the problem disappears.
Any clue?

What version of Ajv are you using? Does the issue happen if you use the latest version?
5.0.0

Ajv options object

const ajv = new Ajv({
    removeAdditional: true,
    // unknownFormats: 'ignore',
    coerceTypes: 'array',
    allErrors: true,
    verbose: true,
});

JSON Schema

{
  "type": "number",
  "format": "double",
  "description": ""
}

Sample data

'lorem'

Your code

const ajv = new Ajv({
    removeAdditional: true,
    // unknownFormats: 'ignore',
    coerceTypes: 'array',
    allErrors: true,
    verbose: true,
});

ajv.addFormat('double', /.*/); // double format isn't standard.  Accept anything

const validate = ajv.compile({
  "type": "number",
  "format": "double",
  "description": ""
});

validate('lorem');
logger.error({ ajv: validate.errors });

Validation result, data AFTER validation, error messages

 [
      {
        "keyword": "type",
        "dataPath": "",
        "schemaPath": "#/type",
        "params": {
          "type": "number"
        },
        "message": "should be number",
        "schema": "number",
        "parentSchema": {
          "type": "number",
          "format": "double",
          "description": ""
        },
        "data": "lorem"
      },
      {
        "keyword": "type",
        "dataPath": "",
        "schemaPath": "#/type",
        "params": {
          "type": "number"
        },
        "message": "should be number",
        "schema": "number",
        "parentSchema": {
          "type": "number",
          "format": "double",
          "description": ""
        },
        "data": "lorem"
      }
    ]

What results did you expect?

 [
      {
        "keyword": "type",
        "dataPath": "",
        "schemaPath": "#/type",
        "params": {
          "type": "number"
        },
        "message": "should be number",
        "schema": "number",
        "parentSchema": {
          "type": "number",
          "format": "double",
          "description": ""
        },
        "data": "lorem"
      }
    ]
bug

All 2 comments

@jfstgermain thank you. Option coerceTypes seems to be causing it when "type" is used in combination with any other keyword that may apply to a number, not necessarily format:

https://runkit.com/esp/58fe72cc29e27100129b2212

fixed in 5.0.1 and in 4.11.8

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jabberwo picture jabberwo  路  7Comments

videni picture videni  路  5Comments

vankop picture vankop  路  4Comments

JonathanWilbur picture JonathanWilbur  路  3Comments

GabDug picture GabDug  路  5Comments