我有一个json模式,它使用“$ ref”(相对路径)引用另一个文件夹中存在的另一个json模式,我得到一个“MalformedURLException”。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Base",
"definitions": {
"Base": {
"type": "object",
"additionalProperties": false,
"properties": {
"event": {
"$ref": "com/artifacts/click/ClickSchema.json"
},
"arrival_timestamp": {
"type": "integer",
"minimum": 0.0
}
},
"title": "Base"
}
}
}
而另一个文件夹中的点击模式如下:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "com/artifacts/click/ClickSchema.json",
"Event": {
"type": "object",
"additionalProperties": false,
"properties": {
"sourceName": {
"type": "string"
}
}
}
}
有人可以请帮助。我正在使用this架构验证器。
默认情况下,JSON Schema对文件中的位置一无所知,也不知道文件夹中其他文件的内容。
看起来您正在使用的库识别出这一点,并建议您使用特殊的参考协议(classpath
)轻松地定位文件夹中的其他文件:
https://github.com/everit-org/json-schema#loading-from-the-classpath
随着模式的增长,您需要将其拆分为多个源文件,并使用“$ ref”引用进行连接。如果要将模式存储在类路径上(而不是例如通过HTTP提供它们),那么推荐的方法是使用classpath:protocol来使模式相互引用。
这不是JSON Schema定义的。
更常见的方法是加载您打算使用的所有模式,并允许在已有文件的情况下进行本地解析。您正在使用的库也支持:https://github.com/everit-org/json-schema#registering-schemas-by-uri
有时使用预加载的模式很有用,我们为其分配一个仲裁URI(可能是一个uuid),而不是通过URL加载模式。这可以通过使用模式加载器的#registerSchemaByURI()方法将模式分配给URI来完成。例:
SchemaLoader schemaLoader = SchemaLoader.builder() .registerSchemaByURI(new URI("urn:uuid:a773c7a2-1a13-4f6a-a70d-694befe0ce63"), aJSONObject) .registerSchemaByURI(new URI("http://example.org"), otherJSONObject) .schemaJson(jsonSchema) .resolutionScope("classpath://my/schemas/directory/") .build();
如果您打算让其他人使用您的模式,还有其他注意事项。如果是这种情况,请做评论,然后我会扩展。