user8479984
user8479984

Reputation: 501

Understanding JSON Schema errors using ajv

I have the following schema and json to validate using ajv.

const schema = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [ "countries" ],
  "definitions": {
    "europeDef": {
      "type": "object",
      "required": ["type"],
      "properties": { "type": {"const": "europe"} }
    },
    "asiaDef": {
      "type": "object",
      "required": ["type"],
      "properties": { "type": {"const": "asia"} }
    }
  },
  "properties": {
    "countries": {
      "type": "array",
      "items": {
        "oneOf":[
          { "$ref": "#/definitions/europeDef" },
          { "$ref": "#/definitions/asiaDef"}
        ]
      }
    }
  }
}
const data = {
  "countries":[
    {"type": "asia"},
    {"type": "europe1"}
  ]
}
const isValid = ajv.validate(schema, data); //schema, data
if(! isValid){
  console.log(ajv.errors);
}

and the error is:

[
  {
    keyword: 'const',
    dataPath: '/countries/1/type',
    schemaPath: '#/definitions/europeDef/properties/type/const',
    params: { allowedValue: 'europe' },
    message: 'should be equal to constant'
  },
  {
    keyword: 'const',
    dataPath: '/countries/1/type',
    schemaPath: '#/definitions/asiaDef/properties/type/const',
    params: { allowedValue: 'asia' },
    message: 'should be equal to constant'
  },
  {
    keyword: 'oneOf',
    dataPath: '/countries/1',
    schemaPath: '#/properties/countries/items/oneOf',
    params: { passingSchemas: null },
    message: 'should match exactly one schema in oneOf'
  }
]

I know why the error is appearing (reason: as I have used 'europe1' and it is not conforming the schema standard)

I have following questions from the above error situation:

  1. Being, I have provided 'asia' as a valid const, the error stills talks about 'asia' as part of second entry in the array. Why did it showing as an error despite schema is absolute fine from asia perspective. Is this because 'oneOf' getting used ? In other words, it is very hard to understand, what and where is the error and what is not?

  2. For asia, 'message: 'should be equal to constant' (2nd item of the array) is misleading imo. It gives an impression that there are still some problems with the 'asia'.

  3. How to parse this error: on the basis of schemaPath or dataPath? Also in any case, it will still give an impression that there is a problem in terms of 'asia' (and actually its not)

  4. Also, how to explain the above error output to a novice, as the novice will still say, why asia is coming part of error despite its correct?

  5. Also, if the schema become more complex using oneOf/anyOf,allOf or using if-then-else, the ajv.errors output becomes more complex to understand and to explain (when certain condition are accurate but displayed as error, example asia here)

  6. Are there any theory/documentaion/guidelines to understand the errors in a better way?

Upvotes: 0

Views: 2091

Answers (1)

Relequestual
Relequestual

Reputation: 12295

For JSON Schema draft 2019-09, we created several standardised output formats. ajv provides one of the most useful outputs from a draft-07 schema in comparison to many libraries.

When looking at the errors, what you might be overlooking is the dataPath value.

In answer to 1, the errors reported are all when applying to data path /countries/1. /countries/0 is fine, as you say. Arrays in javascript start at 0.

I think knowing that also answers all your other questions.

I think you may have assumed that arrays start at 1, and the data path was referring to asia object while it's actually targeting europe1 object.

Please do comment if I'm missing something or you're still confused on this.

Upvotes: 1

Related Questions