Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json schema only validate the first element from an array

Tags:

json

I have a json schema, it has an array, I want to validate all items from the array but the schema only validate the first element, why the schema doesn't validate the rest?

Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "codigoEmpresa": {
      "type": "string"
    },
    "nombreEmpresa": {
      "type": "string"
    },
    "planes": {
      "type": "array",
      "items": [
        {
          "type": "object",
          "properties": {
            "codigoPlan": {
              "type": "string"
             },
             "nombrePlan": {
               "type": "string"
             },
             "tipoProducto": {
               "type": "integer"
             }
          },
         "required": [
           "codigoPlan",
           "nombrePlan",
           "tipoProducto"
          ]
        }
      ]
    }
  },
  "required": [
    "codigoEmpresa",
    "nombreEmpresa",
    "planes"
   ]
 }

Invalid json:

{
   "codigoEmpresa":"204",
   "nombreEmpresa":"Claro",
   "planes":[
      {
         "codigoPlan":"M-PP-Premium-30.03",
         "nombrePlan":"Plan Max Premium Libre",
         "tipoProducto":1
      },
      {
         "tipoProducto":3
      }
   ]
}

Schema validator:

https://json-schema-validator.herokuapp.com/

The attributes nombrePlan and tipoProducto in second element from the array are required on the json but the schema validator doesn't validate it.

like image 838
Octavio Cárdenas Avatar asked Mar 05 '23 02:03

Octavio Cárdenas


1 Answers

"items": [ { ... } ]

in your schema should be

"items": { ... }

items keyword in this form will apply a single subschema to all elements found in the array.

like image 177
leadpony Avatar answered Mar 25 '23 06:03

leadpony