在JSON模式中使用RegEx

Des*_*yer 25 regex schema json jsonschema

尝试编写一个使用RegEx来验证项目值的JSON模式.

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

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

任何帮助/信息将非常感谢!

感谢:D

JSON SCHEMA

{
    "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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

警告!更好地检查你的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"
} ]
Run Code Online (Sandbox Code Playgroud)
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"])
Run Code Online (Sandbox Code Playgroud)

clo*_*eet 45

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

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

PS - 如果你想让模式匹配属性的(而不是值),那么你应该使用"patternProperties"(就像"properties",但键是RegEx).


Lod*_*rds 11

您的JSON架构语法不正确.更改

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

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

  • @Destroyer你能接受cloudfleet的回答吗? (4认同)
  • 问题不是关于测试*值*,而不是属性名称吗? (2认同)