首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在JSON Schema中使用RegEx

在JSON Schema中使用RegEx
EN

Stack Overflow用户
提问于 2013-05-11 06:59:47
回答 2查看 53.5K关注 0票数 37

尝试编写一个JSON schema,它使用RegEx来验证项目的值。

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

我找不到任何教程或示例来实际解释RegEx在JSON schema中的用法。

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

谢谢,D

JSON模式

代码语言:javascript
复制
{
    "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,但不是有效的架构。

验证结果:失败

代码语言:javascript
复制
[ {
    "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"
} ]

代码语言:javascript
复制
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"])
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-18 20:33:09

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

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

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

票数 60
EN

Stack Overflow用户

发布于 2013-05-11 18:34:54

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

代码语言:javascript
复制
"patternProperties": {
    "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$"
    }

代码语言:javascript
复制
"patternProperties": {
    "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$": {}
    }
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16491973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档