正在try 编写一个JSON模式,该模式使用正则表达式验证项的值.

Have an item named progBinaryName whose value should adhrere to this RegEx string "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$".

找不到任何教程或示例真正解释在JSON模式中使用正则表达式.

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

谢谢

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.

Instance is not a required type - http://json-schema.org/draft-03/hyper-schema#


Schema is valid JSON, but not a valid schema.


验证结果:失败

[ {
    "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"])

推荐答案

To test a string value (not a property name) against a RegEx, you should use the "pattern" keyword:

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

P.S. - if you want the pattern to match the key for the property (not the value), then you should use "patternProperties" (it's like "properties", but the key is a RegEx).

Json相关问答推荐

输入请求中不存在null的条件抖动

使用单元和非单元版本反序列化Rust中的枚举,而无需编写自定义反序列化程序

Flutter -控制器问题-JSON API

Vega-Lite(Deneb):难以将最小和最大值应用于折线图和文本标签以及线条末尾的点

Ansible - 将文件内容添加到字典中

Bicep脚本中如何设置弹性池的维护窗口?

小写嵌套特定键的震动转换

如何将复杂的 JSON 反序列化为 Rust 类型?

如何在循环中传递列表(会话(字符串属性))以在 Gatling Scala 中创建批量 Json 请求

在 Flutter 中将对象转换为可编码对象失败

在 json 嵌入的 YAML 文件中 - 使用 Python 仅替换 json 值

在 JSON 反序列化期间没有为System.String类型定义无参数构造函数

将文件发送到 Rails JSON API

有没有办法使用 Jackson 将 Map 转换为 JSON 表示而不写入文件?

有没有办法在 angular.json 中扩展配置?

POST:在 url 本身中发送 post 请求

使用 JSONArray 和 JSONObject 进行 Foreach

如何在所有子项中查询具有特定值的属性的firebase

log4j 支持 JSON 格式吗?

将对象序列化为 JSON 时循环引用检测到异常