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.

1 Answers

1
Community On Best Solutions
"items": [ { ... } ]

in your schema should be

"items": { ... }

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