正在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相关问答推荐

如何在 Django 的模板语言中获取 json 键和值?

hook到 Decodable.init() 以获得未指定的键?

C# 中的 JSON 格式化程序?

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

.NET CORE 3 升级 CORS 和 Json(cycle) XMLHttpRequest 错误

将 TypeScript 与 tsconfig.json 文件一起使用时是否可以包含 json 文件?

带有 Jackson 的不可变 Lombok 注释类

Protocol Buffer vs Json - 何时选择一个而不是另一个

使用 serde 生成漂亮的(缩进的)JSON

MySQL 选择 JSON 字段属性具有值的位置

使用可编码的值,有时是 Int,有时是 String

如何将 FormData(HTML5 对象)转换为 JSON

从 VS 2017 Azure Function 开发中的 local.settings.json 读取值

如何在 JSON Postgres 数据类型列中搜索特定字符串?

在 React 中导入 JSON 文件

json.loads 可以忽略尾随逗号吗?

来自 JSON 字符串的 Typescript `enum`

如何在本地存储中存储对象数组?

ASP.NET Core 返回带有状态码的 JSON

如何在不消除对象歧义的情况下使用 circe 解码 ADT