当前位置: 首页 > 面试题库 >

在JSON模式中使用RegEx

秦安怡
2023-03-14
问题内容

尝试编写使用RegEx验证项目值的JSON模式。

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

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

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

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

问题答案:

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

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

PS-如果您希望模式与属性的
(而不是值)相匹配,则应使用"patternProperties"(就像"properties",但键是RegEx)。



 类似资料:
  • 尝试编写一个JSON模式,该模式使用RegEx来验证项的值。 找不到任何实际解释在JSON模式中使用RegEx的教程或示例。 任何帮助/信息将非常感谢! 谢谢,D 模式是有效的JSON,但不是有效的模式。 验证结果:失败

  • 是否可以定义一次regex并重用它?我有几个非常复杂的正则表达式,我想将它们用作模式,用于模式中各种不同对象的大量属性的值。复制粘贴这个看起来像是自找麻烦,但我似乎找不到合适的重用示例。 删去说明我想做什么的模式。 在http://www.jsonschemavalidator.net/中不进行验证,因为“pattern”不是字符串。这是一个重复使用的洞吗。我看过patternProperties

  • 我对文档模式的定义如下:

  • 问题内容: 我正在使用的其余服务响应类似于以下示例,这里仅包括3个字段,但还有更多内容: 我想为上述(草案04)编写一个JSON模式文件,该文件将明确指定: 但是,找不到任何文档或如何执行此操作的示例。 目前,我的JSON模式如下所示: 我应该如何处理的任何指针/示例。 问题答案: 我认为推荐的方法是Json-Schema web Example2中显示的方法。您需要使用一个枚举来“按值”选择架构

  • 使用Jackson(最新版本可以)为JAVA bean/POJO类生成一个JSON模式,这样它就可以正确地包含嵌套对象的结构,并且还希望向嵌套POJO添加自定义属性(在我的例子中,希望为每个嵌套POJO参数添加一个完全分类的classname属性)。 用例- 比如说,我有一个Person类,如下所示。我用这个人作为我某个操作的参数。- 这个问题肯定与此相关--如何使用jackson遍历生成的jso

  • 我试图使用另一个JSON模式来验证JSON模式。 要验证的JSON模式示例:https://jsonschema.net/home 验证上述模式的验证模式参考:https://github.com/ajv-validator/ajv/blob/master/lib/refs/json-schema-draft-07.json 从上面的中,如果我删除,我的JSON将变得无效。 我有什么方法可以定义根