我试图通过构建验证两种不同对象类型的模式来弄清楚oneOf是如何工作的.例如一个人(名字,姓氏,运动)和车辆(类型,成本).
以下是一些示例对象:
{"firstName":"John", "lastName":"Doe", "sport": "football"}
{"vehicle":"car", "price":20000}
Run Code Online (Sandbox Code Playgroud)
问题是我做错了什么,我该如何解决它.这是架构:
{
"description": "schema validating people and vehicles",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [ "oneOf" ],
"properties": { "oneOf": [
{
"firstName": {"type": "string"},
"lastName": {"type": "string"},
"sport": {"type": "string"}
},
{
"vehicle": {"type": "string"},
"price":{"type": "integer"}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在此解析器中验证它时:
https://json-schema-validator.herokuapp.com/
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
[ {
"level" : "fatal",
"message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n \"level\" : \"error\",\n \"schema\" : {\n \"loadingURI\" : \"#\",\n \"pointer\" : \"/properties/oneOf\"\n },\n \"domain\" …Run Code Online (Sandbox Code Playgroud) 我想知道我是否可以定义一个JSON模式(草案4),该模式至少需要一个对象的许多属性中的一个.我已经知道了allOf,anyOf并且oneOf只是无法弄清楚如何我想要的方式使用它们.
以下是一些示例JSON来说明:
// Test Data 1 - Should pass
{
"email": "hello@example.com",
"name": "John Doe"
}
// Test Data 2 - Should pass
{
"id": 1,
"name": "Jane Doe"
}
// Test Data 3 - Should pass
{
"id": 1,
"email": "hello@example.com",
"name": "John Smith"
}
// Test Data 4 - Should fail, invalid email
{
"id": 1,
"email": "thisIsNotAnEmail",
"name": "John Smith"
}
// Test Data 5 - Should fail, missing one of …Run Code Online (Sandbox Code Playgroud) 链接到规范:http://json-schema.org/latest/json-schema-validation.html#anchor64
第5.4.4.2节规定:
针对这三个关键字成功验证对象实例取决于"additionalProperties"的值:如果其值为布尔值true或模式,则验证成功; ...
第5.4.4.3节规定:
如果不存在"additionalProperties",则可以认为它具有空模式作为值.
好的,所以如果"additionalProperties"不存在,它将被视为存在空模式.如果它是一个模式(任何类型),那么无论其他任何考虑因素,该对象都会成功验证.
但是这与第5.4.4.5节"示例"中的断言相矛盾,即给定实例无法针对给定的模式进行验证(它没有为"additionalProperties"指定任何内容).
有人可以解释我在哪里以及以何种方式误解了规范?
如何设置JSON Schema规则以确定必须设置其中一个属性并且是必需的?
我尝试了各种方法来解决它:
{
"id":"#",
"required":true,
"additionalProperties":true,
"type":"object",
"properties":{
"surname":{
"id":"surname",
"required":true,
"type":"string"
},
"oneOf":[
{
"$ref":"#/definitions/station_id"
},
{
"$ref":"#/definitions/station"
}
]
},
"definitions":{
"station_id":{
"type":"integer"
},
"station":{
"type":"string"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它从来没有奏效.我需要做的是接受station_id什么是整数或者什么是字符串名称.
请问有办法吗?
请帮我解决这个问题:我尝试编写一个 json 模式来验证以下对象:
json 对象:
{"param":value}
Run Code Online (Sandbox Code Playgroud)
可能的值:'all',[任何整数的数组]
所以它是一个简单的 json 对象,其中包含一个变量,可以是字符串“all”,也可以是任何整数数组 []。
我尝试了这个,但它在 json 模式验证器中不起作用:
{
"type": ["string","array"],
"items": { "oneOf": [
"all",
{ "type": "integer" }
]
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢。
我想验证以下 json 架构,我正在使用 Ajv npm 包。
{
"email": "xyzmail@gmail.com",
"phone": "1112223334",
"country_code": "91"
}
Run Code Online (Sandbox Code Playgroud)
我想要仅电子邮件,或仅电话和国家/地区代码,或者所有三个属性都应该在那里。
我已经尝试过 oneOf、allOf、anyOf 也尝试过嵌套主题,但在某些情况下它可以工作,而在某些情况下它不能工作。
我尝试过以下代码
{
"type": "object",
"properties": {
"email": {
"type": "string",
"format": "email",
"maxLength": constants.LENGTHS.EMAIL.MAX
},
"phone": {
"type": "string",
"pattern": constants.REGEX.PHONE,
"maxLength": constants.LENGTHS.PHONE.MAX
},
"country_code": {
"type": "string",
"pattern": constants.REGEX.COUNTRY_CODE,
"maxLength": constants.LENGTHS.COUNTRY_CODE.MAX
}
},
"anyOf": [
{
"required": ["email"],
},
{
"required": ["phone", "country_code"],
},
{
"required": ["email", "phone", "country_code"]
},
],
"additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)