【问题标题】:jsonschema not validating missing required propertiesjsonschema 未验证缺少的必需属性
【发布时间】:2020-03-25 19:01:11
【问题描述】:

我知道这个问题经常出现,我在发布之前尝试过研究这个问题,但仍然不知道我错过了什么

我有一个包含嵌套对象的架构。我的架构期望 company 属性是一个对象。该对象具有必需的属性,但它们被忽略了。为什么它忽略了必需的属性?

架构:

{
        'business_type': {
            'type': 'string',
            "enum": ['company', 'non_profit']
        },
        'email': {
            'type': 'string'
        },
        'company': {
            'type': 'object',
            'properties': {
                'address': {
                    'type': 'object',
                    'properties': {
                        'city': {
                            'type': 'string',
                        },
                        'country': {
                            'type': 'string',
                            'enum': ['US']
                        },
                        'line1': {
                            'type': 'string'
                        },
                        'line2': {
                            'type': 'string'
                        },
                        'postal_code': {
                            'type': 'string'
                        },
                        'state': {
                            'type': 'string'
                        }
                    },
                    'required': ['city', 'country', 'line1', 'postal_code', 'state'],
                },
                'name': {
                    'type': 'string'
                },
                'phone': {
                    'type': 'string'
                }
            },
            'required': ['address', 'name', 'phone'],
        },
        'required' : ['business_type', 'email', 'company']
    }

没有失败但应该失败的示例对象,因为它缺少电话属性

{
    "business_type": "company",
    "email": "email@email.com",
    "company": {
        "address": {
            "city": "city",
            "country": "US",
            "line1": "line1",
            "line2": "line2",
            "postal_code": "00000",
            "state": "AZ"
        },
        "name": "name"
    }
}

如果缺少业务类型、电子邮件或公司,验证工作正常,因此它不会验证嵌套结构。

我假设我忽略了某些东西,我只是不知道我忽略了什么

【问题讨论】:

    标签: node.js validation jsonschema


    【解决方案1】:

    您需要将business_typeemailcompany 包装在properties 关键字中。否则,架构不会将它们视为属性,而只是架构中的额外数据。 JSON Schema 将忽略它不知道的关键字。

    您在 company 子模式中正确。

    【讨论】:

    • 我知道这一定是小问题,我专注于嵌套结构中的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多