- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 JSON 模式和 JSON,如下面的链接中提供的用于说明目的。
JSON Schema and the JSON
格式:
数组中的单个 JSON 对象(具有它们的附加属性,可能会随数组中的其他对象而变化)可以是任何 3 个区域:'america'、'asia' 和 'europe' 并且至少在区域对象的类型上应该在那里。这可以通过数组 minItems 属性来实现)
问题陈述:
{
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {},
"anyOf": [{
"required": ["region"],
"properties": {
"region": {
"enum": ["america"]
},
"country": {
"type": "string"
},
"population": {
"type": "string"
}
}
},
{
"oneOf": [
{
"required": ["region"],
"properties": {
"region": {
"enum": ["asia"]
},
"country": {
"type": "string"
},
"details": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"tz": {
"type": "string"
}
}
}
}
}, {
"required": ["region"],
"properties": {
"region": {
"enum": ["europe"]
},
"country": {
"type": "string"
},
"language": {
"type": "string"
}
}
}
]
}
]
}
}
}
}
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}
]
}
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}
最佳答案
我可以理解你为什么尝试你所做的方法,但是它没有按预期工作,因为你已经定义了,数组中的每个项目可能是 america
或( europe
或 asia
),这不是您想要的。
记住,items
将值模式应用于数组中的每个元素。它对整个数组本身没有任何限制。 contains
检查数组中的至少一项是否根据其值模式进行了验证。
你要说的是,数组中的每一项都可能有america
或 europe
或 asia
,但数组可能不包含 europe
如果它包含 asia
,反之亦然。
我重构了架构并进行了一些更改。
希望你也能看出 oneOf
的使用意图很明确>> ( contains
和 not
> contains
)。
JSON Schema 通过添加约束来工作。您通常不能通过省略来定义约束。
JSON Schema and data validation demo
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"containtsAsia": {
"contains": {
"properties": {
"region": {
"const": "asia"
}
}
}
},
"containsEurope": {
"contains": {
"properties": {
"region": {
"const": "europe"
}
}
}
}
},
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"region": {
"enum": [
"america",
"asia",
"europe"
]
},
"country": {
"type": "string"
},
"population": {
"type": "string"
}
}
},
"oneOf": [
{
"allOf": [
{
"$ref": "#/definitions/containtsAsia"
},
{
"not": {
"$ref": "#/definitions/containsEurope"
}
}
]
},
{
"allOf": [
{
"$ref": "#/definitions/containsEurope"
},
{
"not": {
"$ref": "#/definitions/containtsAsia"
}
}
]
}
]
}
}
}
关于jsonschema - 在 anyOf 中为 JSON Schema 嵌套 oneOf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172931/
我有两个参数,比如 number_1和 number_2 . number_2的值应该大于 number_1 的值.这如何在 jsonschema 中完成? 最佳答案 这不能用普通的 jsonsche
我有正确验证从返回单个文章的端点返回的文章的代码。我很确定它可以正常工作,因为当我故意不在文章中包含必填字段时,它会给出验证错误。 我还有这段代码,它尝试验证从返回文章数组的端点返回的文章数组。但是,
使用 Python 时 jsonschema可以定义无法用有效 JSON 表达的模式和实例。 >>> import jsonschema >>> schema = { ... "type": "o
我刚学 jsonschema .我不清楚 items 之间的区别是什么和 properties .有人可以解释一下吗? 最佳答案 items用于验证数组,properties用于验证对象。 items
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 10 个月前关闭。 Improve
件JSON Schema很容易与 Angular、React 和 Alpaca 等前端 JavaScript 库一起使用,以创建带验证的 html 表单。这也适用于 Python 解决方案 djang
这是一个使用 propertyNames 的 JSON 模式: { "$schema": "http://json-schema.org/draft-07/schema#", "typ
假设我有一个具有四个可能属性的对象:a、b、c、d。 a 和 b 只能一起出现(即,当且仅当 b 出现时 a 出现)。如果a和b出现,c就不能出现(即a/b和c是互斥的)。如果 a 和 b 没有出现,
我正在使用以下架构来验证我的 json: { "$schema": "http://json-schema.org/schema#", "title": " Rules", "
我希望将 json 模式的公共(public)部分捕获到一个文件中,然后从主模式文件中引用这个文件。所以基本上不是 1 个大的 json 模式文件,而是多个相互引用的文件。我正在使用 json-sch
它是有效的json模式吗? object: $ref: '#/definitions/object' 您会建议使用这种格式吗? 最佳答案 自我引用是允许的并且有用的。但是,您的示例看起来就
我有一个 json 模式,它描述了一个相当复杂的 API 查询语法。一些属性是模式匹配的,但还需要接受除模式之外的其他值(即其他显式字符串)。我似乎无法在众多 json 模式站点中找到任何此类示例。
我有一个 JSON 响应,我正在尝试为其创建 JSONSchema { "gauges": { "foo": { "value": 1234
我正在尝试学习 json 模式,但有些事情对我来说并不奏效。我正在尝试运行来自 http://json-schema.org/understanding-json-schema/reference/c
在下面的架构中,我需要 items_list , price和 variance作为必需的键。条件是 price和 variance可能为空,也可能不为空,但都不能为空。 虽然我能够实现它,但我很期待
对于以下 json 字符串: { "abc" : 123, "def" : 345 } 以下架构认为它有效: { "$schema": "http://json-schema.
在 jsonSchema 中,您可以使用“required”属性指示定义的字段是否是必填的: { "$schema": "http://json-schema.org/draft-04/sch
我想使用 JSON Schema 并允许新类型(目前有字符串数字数组对象 boolean 和 null) 例如我想添加一个联系人类型 在 JSON 中甚至可能吗?有什么建议吗? 谢谢 最佳答案 如果您
给定一个名为 location 的架构的一部分,如下所示: { type: 'object', required: [ 'country' ], additionalProperties:
我想使用 JSON Schema 并允许新类型(目前有字符串数字数组对象 boolean 和 null) 例如我想添加一个联系人类型 在 JSON 中甚至可能吗?有什么建议吗? 谢谢 最佳答案 如果您
我是一名优秀的程序员,十分优秀!