Json模式动态密钥验证

时间:2014-12-30 12:29:47

标签: jsonschema json-schema-validator

面对架构验证的问题。

架构:

{
    "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

进行验证

2 个答案:

答案 0 :(得分:7)

我在你的模式中看到你似乎忘记了逃避一些字符或者没有正确地做到这一点:

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

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

enter image description here

它应该是:

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

或者没有转义内部/类字符但是我使用第一个模式因为我认为它的意图更清楚:

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

您需要有两个\,因为第一个\是第二个\的转义符。只使用一个,它就行不通,因为javascript中没有\.\+\。您希望模式本身具有patternProperties

但是,默认情况下,json架构a-z区分大小写,因此您需要通过向其添加"^[A-Za-z0-9\\._%\\+-]+@[A-Za-z0-9\\.-]+\\.[A-Za-z]{2,6}$" 来扩展您的电子邮件模式:

"additionalProperties": false

(我没有找到任何其他方法让它不区分大小写)

您还需要通过在patternProperties旁边添加{ "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 } 来排除任何其他属性名称,否则它会捕获与该模式不匹配的所有其他属性。

工作架构应如下所示:

{{1}}

我在escape sequence

上测试了它

答案 1 :(得分:4)

根据草案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
}