Mihail Kuznetsov Mihail Kuznetsov - 1 month ago 10
JSON Question

Json schema validate property name in array

I have this json object

{
"status": 68,
"error": [
{
"description_ru": "Ошибка авторизации. Пожалуйста, проверьте правильность введенных e-mail и пароля."
},
{
"description_en": "Authorization error. Please check login and password."
}
]
}


And i write next json schema

{
"title": "Неверно указан пароль",
"type": "object",
"properties":{
"status":{
"description": "Статус операции",
"type": "integer"
},
"error":{
"description":"Информация об ошибке на разных языках",
"type": "array",
"items": {
"properties":{
"description_ru":{
"description": "Информация об ошибке на русском языке",
"type": "string"
},
"description_en":{
"description": "Информация об ошибке на английском языке",
"type": "string"
}
}
}
}
},
"required":["status", "error"],
"additionalProperties": false
}


I have a next problem. If i change name in json schema error to error-edited and run validation, then i get next error:


false Property error does not exist in the schema


Ok, it's perfect!

However, if I change property name inside array error in json schema description_en to *description_en-edited and run validation, then i don't get the error.

Please tell me why is this happening?

Answer

Because "error" is required but "description_en" is not.

Add "additionalProperties" to the object in the items:

...
"items": {
    "type": "object",
    "properties": {
      "description_ru":{
        "description": "Информация об ошибке на русском языке",
        "type": "string"
      },
      "description_en":{
        "description": "Информация об ошибке на английском языке",
        "type": "string"
      }
    },
    "additionalProperties": false
  },
"minItems": 2
...