【问题标题】:Why can't the schema see the required array?为什么架构看不到所需的数组?
【发布时间】:2021-03-03 16:57:56
【问题描述】:

我已经创建了一个 JSON Schema,但是当我尝试使用它来验证另一个对象时,该模式由于错误而失败,引用:

消息: 对象缺少必需的属性:信封。 架构路径: #/必填

但是,我可以清楚地看到信封内列出的所需数组是其属性的兄弟。在我在网上看到的每个模式中,都是这样显示的。我在下面包含了一个精简的模式选择,仍然看到相同的错误。谁能告诉我为什么会失败?

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "Envelope": {
            "type": "object",
            "properties": {
                "TP_UNIQUE_NUM": {
                    "length": 50,
                    "type": "string"
                },
                "SENDER_ID": {
                    "length": 50,
                    "type": "string"
                },
                "TRANSMISSION_FORMAT": {
                    "length": 50,
                    "type": "string"
                },
                "RECEIVER_ID": {
                    "length": 50,
                    "type": "string"
                },
                "HEADER": {
                    "type": "object",
                    "properties": {
                        "door": {
                            "description": "Door at which product associated with ASN is scheduled to be unloaded",
                            "type": "string",
                            "mandatory": "Y",
                            "maxLength": 10
                        },
                        "notes": {
                            "description": "Comments associated with ASN",
                            "type": "string",
                            "mandatory": "Y",
                            "maxLength": 4000
                        },
                        "wareHouseReference": {
                            "description": "Additional notes or reference information for the warehouse",
                            "type": "string",
                            "mandatory": "Y",
                            "maxLength": 100
                        }
                    },
                    "required": [
                        "wareHouseReference"
                    ]
                },
                "TP_TRANSMISSION_TYPE": {
                    "maxLength": 50,
                    "type": "string"
                },
                "FILE_NAME": {
                    "maxLength": 50,
                    "type": "string"
                },
                "TRANSMISSION_DATE_TIME": {
                    "maxLength": 50,
                    "type": "string"
                },
                "TRANSACTION_COUNT": {
                    "maxLength": 50,
                    "type": "string"
                }
            }
        }
    },
    "required": [
        "Envelope"
    ]
}

【问题讨论】:

  • 您的架构没有问题。该错误听起来像是在告诉您正在验证的数据没有所需的属性“信封”。

标签: json jsonschema


【解决方案1】:

您似乎在Envelope 对象中缺少required 数组。

您在根级对象中有一个required 数组,在HEADER 对象中也有一个数组,但在Envelope 对象中没有。

我假设每个对象类型都需要一个 required 数组。

编辑:我的假设可能是错误的,我能够使用此示例 JSON 在json-schema-validator 处运行您的架构而不会出现问题。 @Jason Desrosiers 的评论似乎是对的。

{
  "Envelope": {
    "TP_UNIQUE_NUM": "hello",
    "SENDER_ID": "hello",
    "TRANSMISSION_FORMAT": "hello",
    "RECEIVER_ID": "hello",
    "HEADER": {
       "door": "hello",
       "notes": "hello",
       "wareHouseReference": "hello"
     },
    "TP_TRANSMISSION_TYPE": "hello",
    "FILE_NAME": "hello",
    "TRANSMISSION_DATE_TIME": "hello",
    "TRANSACTION_COUNT": "hello"
  }
}

【讨论】:

    猜你喜欢
    • 2017-04-20
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2016-02-28
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多