- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有三个 json-schema 定义。客户、地址和联系方式。
客户端.json
{
"$id": "client.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"name": {
"$id": "/properties/name",
"type": "string"
},
"id": {
"$id": "/properties/id",
"type": "integer"
},
"contact": {
"$ref": "contact.json"
},
"address": {
"$ref": "address.json"
}
}
}
地址.json
{
"$id": "address.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"addressId": {
"$id": "/items/properties/addressId",
"type": "integer"
},
"addressName": {
"$id": "/items/properties/addressName",
"type": "string"
}
}
}
}
联系人.json
{
"$id": "contact.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-06/schema#",
"items": {
"$id": "/items",
"type": "object",
"properties": {
"contactId": {
"$id": "/items/properties/contactId",
"type": "integer"
},
"contactName": {
"$id": "/items/properties/contactName",
"type": "string"
},
"address": {
"$ref": "address.json"
}
}
}
}
待验证对象
var client = {
"name": "test",
"id": 12,
"contact": [
{
"contactId": 12212,
"contactName": "jon",
"address": [
{
"addressId": 64,
"addressName": "pi"
}
]
}
],
"address": [
{"addressId": 4242,
"addressName": "doe"}
]
};
“client.json”中的 $ref 工作正常,但在从“contact.json”中引用“address.json”时出现错误。我在“additionalItems”中使用 $refs 时没有遇到任何错误,但无法根据 $ref 指向的模式进行验证。
我想知道如何使用数组类型模式定义中的 $ref。另外,我使用 AJV 进行模式验证。
编辑 1:AJV 设置
var Ajv = require('ajv');
var ajv = new Ajv({
$data: true,
allErrors: true,
useDefaults: true,
coerceTypes: true,
});
ajv.addSchema(client);
ajv.addSchema(contact);
ajv.addSchema(address);
let valid = ajv.validate('client.json', payload);
if(!valid){
console.log(ajv.errors);
}
最佳答案
我确定问题是 $id
更改了 $ref
的解析范围。我猜测 $ref
解析是通过在文件系统上查找文件来实现的。假设您的三个架构在 file:///path/to/schema
中可用。
file:///path/to/schema/client.json
模式。contact.json
。这是相对 URI,因此您需要确定它相对的 URI 才能解析它。$id
,其值为 client.json
。$id
,所以文件的路径,file:///path/to/schema/client.json
被使用。file:///path/to/schema/client.json
解析 client.json
并获取 file:///path/to/schema/client.json
.file:///path/to/schema/client.json
解析 contact.json
并获取 file://path/到/schema/contact.json
.这就是它开始变得奇怪的地方。
file:///path/to/schema/contact.json
架构。address.json
。这是一个相对 URI,因此您需要确定它相对的 URI 才能解析它。$id
,其值为 /items
。contact.json
。$id
,所以文件的路径 file:///path/to/schema/contact.json
被使用。file:///path/to/schema/contact.json
解析 /items
并获得 file:///items
。file:///items
解析 address.json
并获得 file:///address.json
。file:///address.json
架构,但它不存在。因为 $id
改变了 $ref
的解析范围,所以强烈建议不要像在模式中那样给所有东西一个 $id
.此功能适用于将多个小模式组合成一个这样的用例。除了在文档的根目录之外,你真的不应该使用它,除非你有充分的理由并理解其中的含义。
关于json - $ref 不适用于数组类型 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473116/
我想读取帖子的数据并获取用户 key ,然后通过它进行搜索并同时获取用户数据,我尝试过,但它后退了一步,直到它才显示用户名我执行任何其他操作 这是帖子和经过身份验证的用户的 Firebase 实时数据
您知道是否有办法将 js ref 和 css ref 作为单个 ref 包含在 html 中?通常这些 ref 单独包含在 html head 中,但我的经理想知道下游消费者是否有一种简化的方法将这些
我正在使用 Swing+Clojure 开发一个 GUI 应用程序,它需要各种可变数据(例如滚动位置、用户数据、文件名、选定的工具选项等)。 我至少可以看到三种不同的处理这组数据的方式: 创建对所有数
我正在尝试通过 React 使用 ref 属性。我的浏览器出现奇怪的错误,但我无法弄清楚问题出在哪里。谁能向我解释一下为什么我会收到此错误: Error: Invariant Violation: a
在我的程序中,我有模板类,这些模板类主要是用于特殊目的 std::function 的包装器。最小的例子是: template class Foo { public: exp
如果被引用为参数的对象在函数中被修改,是否使用 ref 有关系吗?下面两个函数有区别吗? void DisposeObject(ClassThing c) { c.Dispose(); } vo
尝试将大型但线性的 svn 存储库迁移到 git。 svn 存储库没有标准布局(主干、分支、标签)...只有主干的一个目录。 Ubuntu 12.4 LTS,git 1.7.9.5。 $ git sv
您现在如何设置动态引用? 我收到一个错误 cannot set property of 'test' undefined ,如果我使用 this.someRef['test'] = ref;}/>
试图理解 gerrit 中的 refs/for/refs/* 功能。这个问题与 refs/for/master 无关。 我们什么时候可以使用这个 refs/for/refs/* 功能。 有人可以为此解
我以不同的方式调用 4 种方法时得到不同的结果: static void Main(string[] args) { var emp = new Employee { Name = "ABC"
假设我有以下内容: var t = typeof(Foo).MakeByRefType(); 有没有办法将结果转换回typeof(Foo)? 老实说,我发现的唯一方法是: var t = typeof
我以下列方式使用 ref。那么当在第 5 种方法中创建一个新对象时,是否会一直访问 main 方法中的原始 emp 并在那里创建一个新对象? 如果是,有没有一种方法可以实现相同的功能而无需多次迭代,即
我在文档的 html 标签内有一些文本。文字看起来像这样 I need this text <ref> Some unwanted text </ref> I need thi
一些背景: 前几天我遇到了一些事情,这让我开始思考嵌套函数调用中的重载解析。考虑以下代码: #include void printer(const int &a) { std::cout <<
如果直接从 this.refs 获取元素对象,那么为什么要使用 ReactDOM.findDOMNode? var HelloMessage = React.createClass({ click:f
我在这里做错了什么,或者从 C# 7.2 开始,不支持通过 ref 返回并允许设置的索引器? 作品: public ref byte this[int index] { get { r
看来我现在几乎每天都在这里问问题。我不确定这是好事还是坏事... 今天的“WTF flavor ”涉及我在使用来自 NI Measurement Studio 对象的函数时完全和完全无能为力。与我之前
这个问题在这里已经有了答案: Does foreach() iterate by reference? (10 个答案) Alternative to using ref in foreach? (
给定一个函数声明 dynamic DoSomething(dynamic inputObject) 我可以用枚举调用它作为inputObject: MyEnum myEnum; myEnum = Do
如果我将数组传递给函数并在函数内对数组进行更改,则函数外部存在的数组会反射(reflect)这些效果。例如: var myArr = [1, 2, 3]; function popAll(arr) {
我是一名优秀的程序员,十分优秀!