【问题标题】:Json schema dynamic key validationJson 模式动态密钥验证
【发布时间】:2023-03-30 06:19:01
【问题描述】:

面临架构验证问题。

架构:

{
    "type": "object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "#",
    "required": true,
    "patternProperties": {
        "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$": {
            "type": "object",
            "required": true,
            "properties": {
                "_from": {
                    "id": "_from",
                    "type": "string",
                    "required": true
                },
                "message": {
                    "type": "object",
                    "id": "message",
                    "properties": {
                        "detail": {
                            "type": "string",
                            "id": "detail",
                            "required": true
                        },
                        "from": {
                            "type": "string",
                            "id": "from",
                            "required": true
                        }
                    }
                }
            }
        }
    }
}

json:

{
    "tom@example.com": {
        "_from": "giles@gmail.com",
        "message": {
            "from": "Giles@gmail.com",
            "detail": "AnyonewanttomeetmeinParis"
        }
    },
    "harry@example.com": {
        "_from": "giles@gmail.com",
        "message": {
            "from": "Giles@gmail.com",
            "detail": "AnyonewanttomeetmeinParis"
        }
    }
}

这里的关键电子邮件地址是动态的,不知何故它不会验证电子邮件验证的正则表达式。

您能否建议我更正架构。

我正在验证使用:http://json-schema-validator.herokuapp.com/index.jsp

【问题讨论】:

    标签: jsonschema json-schema-validator


    【解决方案1】:

    我在您的模式中看到您似乎忘记了转义一些 字符 或者没有正确转义:

    "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$"
    

    它会导致您将鼠标悬停在验证器顶部的链接上时看到的错误:

    应该是:

    "^[A-Z0-9\\._%\\+-]+@[A-Z0-9\\.-]+\\.[A-Z]{2,6}$"
    

    或者不转义内部/类字符,但我会使用第一个模式,因为我认为它的意图更清晰:

    "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$"
    

    您需要有两个\,因为第一个\ 是第二个\ 的转义。一个单独的它不会工作,因为在javascript中没有\.\+之类的escape sequence。您希望在模式本身中有一个 \

    然而 json 架构 patternProperties 默认区分大小写,因此您需要通过添加 a-z 来扩展您的电子邮件模式:

    "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$"
    

    (我没有找到其他不区分大小写的方法)

    您还需要通过在 patternProperties 旁边添加 "additionalProperties": false 来排除任何其他属性名称,否则它会捕获与模式不匹配的所有其他内容。

    工作模式应如下所示:

    {
        "type": "object", 
        "$schema": "http://json-schema.org/draft-03/schema", 
        "id": "#", 
        "required": true,     
        "patternProperties": {
            "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$": {
                "type": "object", 
                "required": true, 
                "properties": {
                    "_from": {
                        "id": "_from", 
                        "type": "string", 
                        "required": true
                    }, 
                    "message": {
                        "type": "object", 
                        "id": "message", 
                        "properties": {
                            "detail": {
                                "type": "string", 
                                "id": "detail", 
                                "required": true
                            }, 
                            "from": {
                                "type": "string", 
                                "id": "from", 
                                "required": true
                            }
                        }
                    }
                }
            }
        }, 
        "additionalProperties": false
    }
    

    我已经测试过了:http://jsonschemalint.com/

    【讨论】:

    • 尝试您的更改仍然失败。更改后您是否尝试过验证?
    • 是的,我有。这可能是因为您错误地格式化了 sn-ps,如果您从这里复制它们,它们缺少右括号。
    • 验证无效电子邮件地址也成功。您是否尝试提供无效的电子邮件地址。
    • 这不是问题的一部分,所以没有;-](还没有)
    • 好的,你能帮我解决这个问题吗。
    【解决方案2】:

    根据草案 04 更改了架构:

    {
    "type": "object", 
    "$schema": "http://json-schema.org/draft-04/schema", 
    "patternProperties": {
        "^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$": {
            "type": "object", 
            "properties": {
                "__from": {
                    "type": "string"
                }, 
                "message": {
                    "type": "object", 
                    "properties": {
                        "from": {
                            "type": "string" 
                        },
                        "detail": {
                            "type": "string"
                        }
                    },
                     "required": [ "from","detail"]
                }
            },
            "required": [ "__from","message"]
       }
    }, 
    "additionalProperties": false
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2011-06-08
      相关资源
      最近更新 更多