在JSON模式使用正则表达式(Using RegEx in JSON Schema)

2019-09-02 13:53发布

尝试编写使用正则表达式来验证项目的值的JSON模式。

有一个名为progBinaryName一个项目,其价值应该adhrere这个正则表达式的字符串"^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"

找不到实际解释一个JSON模式使用正则表达式的任何教程或例子。

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

感谢: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"])

Answer 1:

要测试字符串值(不是属性名)对正则表达式,你应该使用"pattern"的文章:

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

PS -如果你想在模式匹配的财产(不是值),那么你应该使用的关键 "patternProperties" (这就像"properties" ,但关键是一个正则表达式)。



Answer 2:

你的JSON模式语法不正确。 更改

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

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


文章来源: Using RegEx in JSON Schema