为什么 oneOf 不适用于我在 jsonschema 中的架构?
Why oneOf does not work on my schema in jsonschema?
我的登录有不同的有效负载,其中一个是:
{
"username": "",
"pass": ""
}
另一个是:
{
"username": "",
"pass": "",
"facebook": true
}
最后一个:
{
"username": "",
"pass": "",
"google": true
}
我的架构如下:
login_schema = {
"title": "UserLogin",
"description": "User login with facebook, google or regular login.",
"type": "object",
"properties": {
"username": {
"type": "string"
},
"pass": {
"type": "string"
},
"facebook": {
"type": "string"
},
"google": {
"type": "string"
}
},
"oneOf": [
{
"required": [
"username",
"pass"
],
"additionalProperties": False,
},
{
"required": [
"username",
"pass"
"google"
]
},
{
"required": [
"username",
"pass",
"facebook"
]
}
],
"minProperties": 2,
"additionalProperties": False,
}
它应该给出以下示例的错误:
{
"username": "",
"pass": "",
"google": "",
"facebook": ""
}
但它成功验证了架构!我在上面的架构中做错了什么?
编辑-1:
pip3 show jsonschema
Name: jsonschema
Version: 3.0.2
Summary: An implementation of JSON Schema validation for Python
Home-page: https://github.com/Julian/jsonschema
Author: Julian Berman
Author-email: Julian@GrayVines.com
License: UNKNOWN
Location: /usr/local/lib/python3.7/site-packages
Requires: setuptools, six, attrs, pyrsistent
编辑-2:
我得到的错误是:
jsonschema.exceptions.ValidationError: {'username': '', 'pass': '', 'google': '12'} is valid under each of {'required': ['username', 'pass', 'google']}, {'required': ['username', 'pass']}
错误的现场演示:https://jsonschema.dev/s/mXg5X
您的解决方案非常接近。您只需要将 /oneOf/0
更改为
{
"properties": {
"username": true,
"pass": true
},
"required": ["username", "pass"],
"additionalProperties": false
}
问题是 additionalProperties
在确定考虑哪些属性时没有考虑 required
关键字 "additional"。它只考虑 properties
和 patternProperties
。仅使用 required
时,additionalProperties
将 所有 属性视为 "additional",唯一有效值是 {}
.
但是,我建议采用不同的方法。 dependencies
关键字在这些情况下很有用。
{
"type": "object",
"properties": {
"username": { "type": "string" },
"pass": { "type": "string" },
"facebook": { "type": "boolean" },
"google": { "type": "boolean" }
},
"required": ["username", "pass"],
"dependencies": {
"facebook": { "not": { "required": ["google"] } },
"google": { "not": { "required": ["facebook"] } }
},
"additionalProperties": false
}
我的登录有不同的有效负载,其中一个是:
{
"username": "",
"pass": ""
}
另一个是:
{
"username": "",
"pass": "",
"facebook": true
}
最后一个:
{
"username": "",
"pass": "",
"google": true
}
我的架构如下:
login_schema = {
"title": "UserLogin",
"description": "User login with facebook, google or regular login.",
"type": "object",
"properties": {
"username": {
"type": "string"
},
"pass": {
"type": "string"
},
"facebook": {
"type": "string"
},
"google": {
"type": "string"
}
},
"oneOf": [
{
"required": [
"username",
"pass"
],
"additionalProperties": False,
},
{
"required": [
"username",
"pass"
"google"
]
},
{
"required": [
"username",
"pass",
"facebook"
]
}
],
"minProperties": 2,
"additionalProperties": False,
}
它应该给出以下示例的错误:
{
"username": "",
"pass": "",
"google": "",
"facebook": ""
}
但它成功验证了架构!我在上面的架构中做错了什么?
编辑-1:
pip3 show jsonschema
Name: jsonschema
Version: 3.0.2
Summary: An implementation of JSON Schema validation for Python
Home-page: https://github.com/Julian/jsonschema
Author: Julian Berman
Author-email: Julian@GrayVines.com
License: UNKNOWN
Location: /usr/local/lib/python3.7/site-packages
Requires: setuptools, six, attrs, pyrsistent
编辑-2:
我得到的错误是:
jsonschema.exceptions.ValidationError: {'username': '', 'pass': '', 'google': '12'} is valid under each of {'required': ['username', 'pass', 'google']}, {'required': ['username', 'pass']}
错误的现场演示:https://jsonschema.dev/s/mXg5X
您的解决方案非常接近。您只需要将 /oneOf/0
更改为
{
"properties": {
"username": true,
"pass": true
},
"required": ["username", "pass"],
"additionalProperties": false
}
问题是 additionalProperties
在确定考虑哪些属性时没有考虑 required
关键字 "additional"。它只考虑 properties
和 patternProperties
。仅使用 required
时,additionalProperties
将 所有 属性视为 "additional",唯一有效值是 {}
.
但是,我建议采用不同的方法。 dependencies
关键字在这些情况下很有用。
{
"type": "object",
"properties": {
"username": { "type": "string" },
"pass": { "type": "string" },
"facebook": { "type": "boolean" },
"google": { "type": "boolean" }
},
"required": ["username", "pass"],
"dependencies": {
"facebook": { "not": { "required": ["google"] } },
"google": { "not": { "required": ["facebook"] } }
},
"additionalProperties": false
}