【问题标题】:Using RegEx in JSON Schema在 JSON 模式中使用 RegEx
【发布时间】:2013-05-05 17:04:30
【问题描述】:

尝试编写使用 RegEx 验证项目值的 JSON 模式。

有一个名为 progBinaryName 的项目,其值应符合此 RegEx 字符串 "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"

找不到任何教程或示例来实际解释在 JSON 模式中使用 RegEx。

任何帮助/信息将不胜感激!

谢谢, D

JSON 架构

{
    "name": "string",
    "properties": {
        "progName": {
            "type": "string",
            "description": "Program Name",
            "required": true
        },
        "ID": {
            "type": "string",
            "description": "Identifier",
            "required": true
        },
        "progVer": {
            "type": "string",
            "description": "Version number",
            "required": true
        },
        "progBinaryName": {
            "type": "string",
            "description": "Actual name of binary",
            "patternProperties": {
                "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
            },
            "required": true
        }
    }
}

错误:

警告!最好检查你的 JSON。

实例不是必需的类型 - http://json-schema.org/draft-03/hyper-schema#


架构是有效的 JSON,但不是有效的架构。


验证结果:失败

[ {
    "level" : "warning",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : ""
    },
    "domain" : "syntax",
    "message" : "unknown keyword(s) found; ignored",
    "ignored" : [ "name" ]
}, {
    "level" : "error",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/properties/ID"
    },
    "keyword" : "required",
    "message" : "value has incorrect type",
    "expected" : [ "array" ],
    "found" : "boolean"
}, {
    "level" : "error",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/properties/progBinaryName"
    },
    "keyword" : "required",
    "message" : "value has incorrect type",
    "expected" : [ "array" ],
    "found" : "boolean"
}, {
    "level" : "error",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/properties/progBinaryName/patternProperties/progBinaryName"
    },
    "domain" : "syntax",
    "message" : "JSON value is not a JSON Schema: not an object",
    "found" : "string"
}, {
    "level" : "error",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/properties/progName"
    },
    "keyword" : "required",
    "message" : "value has incorrect type",
    "expected" : [ "array" ],
    "found" : "boolean"
}, {
    "level" : "error",
    "domain" : "syntax",
    "schema" : {
        "loadingURI" : "#",
        "pointer" : "/properties/progVer"
    },
    "keyword" : "required",
    "message" : "value has incorrect type",
    "expected" : [ "array" ],
    "found" : "boolean"
} ]

Problem with schema#/properties/progBinaryName/patternProperties/progBinaryName : Instance is not a required type
Reported by http://json-schema.org/draft-03/hyper-schema#
Attribute "type" (["object"])

【问题讨论】:

  • 什么不起作用? (您可能希望将该连字符放在字符类的末尾)
  • 任何在线验证都无法正常工作。
  • “不能正常工作”是什么意思?你会得到误报吗?你会得到假阴性吗?你有什么错误吗?
  • 如果您在问题中包含这些错误消息,您不认为它可能有助于潜在的回答者吗?

标签: regex json schema jsonschema


【解决方案1】:

您的 JSON 架构语法不正确。改变

"patternProperties": {
    "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
    }

"patternProperties": {
    "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$": {}
    }

【讨论】:

  • 感谢您提供答案而不是浪费我的时间。非常非常感谢!
  • 问题不是关于测试,而不是属性名称吗?
【解决方案2】:

要针对 RegEx 测试字符串值(不是属性名称),您应该使用 "pattern" 关键字:

{
    "type": "object",
    "properties": {
        "progBinaryName": {
            "type": "string",
            "pattern": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
        }
    }
}

附: - 如果您希望模式匹配属性的 key(而不是值),那么您应该使用 "patternProperties"(类似于 "properties",但键是 RegEx)。

【讨论】:

猜你喜欢
  • 2016-02-29
  • 2013-07-01
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多