8

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 Answer 1

16
"items": [ { ... } ]

in your schema should be

"items": { ... }

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.