Golang中的多模式JSON验证
我需要根据Golang中的多模式JSON验证,json,go,jsonschema,Json,Go,Jsonschema,我需要根据Golang中的模式验证几个JSON文件 我已经能够通过使用实现它,这真是一个直截了当的库 然而,我现在面临的问题是,我得到的模式依赖于另一个模式,并且没有找到加载我需要的所有模式的方法。因此,我的验证总是失败 这是我的主要模式: { "$schema": "http://json-schema.org/draft-04/schema#", "$ref": "#/definitions/List", "definitions": { "List"
Golang
中的模式验证几个JSON
文件
我已经能够通过使用实现它,这真是一个直截了当的库
然而,我现在面临的问题是,我得到的模式依赖于另一个模式,并且没有找到加载我需要的所有模式的方法。因此,我的验证总是失败
这是我的主要模式:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$ref": "#/definitions/List",
"definitions": {
"List": {
"type": "array",
"items": {
"$ref": "#/definitions/Item"
}
},
"Item": {
"description": "An item ....",
"type": "object",
"additionalProperties": false,
"properties": {
"property01": {
"description": "The property01 code.",
"$ref": "./CommonTypes.json#/definitions/Type01Definition"
}
},
"required": [
"property01"
]
}
}
}
我还有一个常见类型的:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"Type01Definition": {
"description": "The definition for the type 01",
"type": "string",
"pattern": "^[A-Z0-9]{3}$"
}
}
}
有没有办法使用该库加载多个模式?或者是否有任何其他
Golang
库允许实现这一点?使用$ref
引用文件的方法是使用URL方案指定文件的绝对路径。如果将$ref
更改为“$ref”:file:///home/user/directory/CommonTypes.json#/definitions/Type01Definition
,您的示例将按预期工作
如果您需要更多的灵活性,您可以尝试
gojsonschema
,或者切换到不同的Golang
库。该库允许您添加自定义资源,以便您可以一次加载多个模式。第二个问题与so无关:问我们推荐或查找书籍、工具、软件库等的问题torial或其他非站点资源因堆栈溢出而脱离主题,因为它们往往会吸引固执己见的答案和垃圾邮件。