【问题标题】:Json schema only validate the first element from an arrayJson 模式仅验证数组中的第一个元素
【发布时间】:2019-04-25 23:25:29
【问题描述】:

我有一个 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/

数组中第二个元素中的属性 nombrePlan 和 tipoProducto 是 json 所必需的,但模式验证器不会对其进行验证。

【问题讨论】:

    标签: json


    【解决方案1】:
    "items": [ { ... } ]
    

    在你的架构中应该是

    "items": { ... }
    

    items 这种形式的关键字将对数组中找到的所有元素应用一个子模式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2023-03-04
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多