经过几个小时的谷歌搜索和各种文档阅读后,我终于能够使用"$ref" : "file:..." 编写两个 json-schema。
我的示例数据如下所示:
john-doe.json:
{
"first_name": "john",
"last_name": "doe",
"age": 42,
"address": {
"street_address": "foo street 42",
"city": "baklavastan",
"state": "foobar"
"foo": 42
}
}
然后我有两个 json 架构文件,它们位于我的文件系统的同一目录中。
customer.json
{
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"age" : { "type" : "integer" },
"address" : { "$ref" : "file:address.json#" }
},
"required": ["first_name", "last_name", "age", "address"],
"additionalProperties": false
}
address.json
{
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"],
"additionalProperties": false
}
我能够使用库 scjsv(它是 Java json-schema-validation 库的包装器)针对 Clojure 中的架构进行验证
它给了我以下 john-doe.json 示例的验证错误(在 clojure edn 中):
{ :level "error",
:schema {:loadingURI "file:address.json#", :pointer ""},
:instance {:pointer "/address"},
:domain "validation",
:keyword "additionalProperties",
:message
"object instance has properties which are not allowed by the schema: [\"foo\"]",
:unwanted ["foo"] }