【问题标题】:How i can tell jsonschem to need one/more of patternProperties?我如何告诉 jsonschem 需要一个/多个 patternProperties?
【发布时间】:2021-03-21 22:26:40
【问题描述】:

我开发了这个架构

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["users"],
    "properties": {
        "users":{
            "type":"object",
            "additionalProperties": false,
            "patternProperties": {
                "^APP-[A-Z_-]+$": {
                    "$ref": "#/definitions/user"
                },
                "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$": {
                    "$ref": "#/definitions/user"
                }
            }
        }
    },
    "definitions": {
        "user":{
            "type":"object",
            "additionalProperties": false,
            "properties": {
                "full_name": {"type": "string", "pattern": "^[^\\s]+(\\s+[^\\s]+)*$"},
                "email": {"type": "string", "format": "email"}
            }
        }
    }
}

满足以下文件

{
    "users": {
        "APP-TEST": {
            "full_name": "test",
            "email": "test@local.com"
        },
        "user1@local.com": {
            "full_name": "test",
            "email": "test@local.com"
        },
        "user2@local.com": {
            "full_name": "test",
            "email": "test@local.com"
        }
    }
}

但我不知道如何验证 JSON 文档,例如说我想要一个“APP-”键和许多“email-pattern-users”键。

【问题讨论】:

    标签: python json jsonschema


    【解决方案1】:

    您可以使用not 关键字和false 架构指定“我需要至少一个与此模式匹配的属性”:

    {
      "not": { "patternProperties": { "^APP-[A-Z_-]+$": false } }
    }
    

    即:“如果有匹配的模式属性,则为真,否则为假”

    但是,我看不出有什么方法可以区分只需要一个匹配属性和至少两个匹配属性。

    【讨论】:

    • if 模式将始终为真,else 将永远不会适用。您可以将 true 更改为 false 并将整个内容放入 not 以使其正常工作。
    • 实际上,您可以进一步简化它。 if/else 不是必需的。 { "not": { "patternProperties": { "^APP-[A-Z_-]+$": false } } }.
    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多