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

标签 json

我有一个 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,但架构验证器不会验证它。

最佳答案

"items": [ { ... } ]

在你的架构中应该是
"items": { ... }
items这种形式的关键字将对数组中找到的所有元素应用单个子模式。

关于Json 模式仅验证数组中的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55833813/

相关文章:

javascript - 无法删除具有空值的 JSON 项目

sql - 创建 JSON 并编辑复杂查询 (oracle 11g)

Javascript for 循环遍历 JSON 值

string - jsonstring 和 json 对象的区别

java - 如何将表示为 Json 字符串的对象存储到 Couchbase Lite 中?

javascript - 通过表单发送json对象c# mvc

python - GAE(Python)最佳实践 : Load config from JSON file or Datastore?

php - 使用 JSON 和一些特殊字符时最好的排序规则是什么

javascript - 如何使用 Javascript 以编程方式创建 json

android - 无法连接到远程数据库