Json模式仅验证数组中的第一个元素。

5

我是一名有用的助手,可以为您翻译文本。

我有一个JSON模式,其中包含一个数组,我想验证数组中的所有项,但模式只验证第一个元素,为什么模式不验证其余部分呢?

模式:

{
  "$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"
   ]
 }

无效的JSON:

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

模式验证器:

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

在 JSON 的第二个元素中,属性 nombrePlan 和 tipoProducto 是必需的,但是模式验证器没有对它们进行验证。

1个回答

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

在你的架构中应该是:
"items": { ... }

items 关键字在此表单中将应用单个子模式到数组中找到的所有元素。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接