Mongo Json Schema Validator AnyOf无法正常工作 [英] Mongo Json Schema Validator AnyOf not working
问题描述
我创建了一个具有以下验证的集合:
I have created the a collection with the below validation:
{
$jsonSchema: {
bsonType: 'object',
additionalProperties: false,
properties: {
_id: {
bsonType: 'objectId'
},
test: {
bsonType: 'string'
}
},
anyOf: [
{
bsonType: 'object',
properties: {
test1: {
bsonType: 'string'
}
},
additionalProperties: false
},
{
bsonType: 'object',
properties: {
test2: {
bsonType: 'string'
}
},
additionalProperties: false
}
]
}
}
验证操作"设置为错误",验证级别"设置为中等".
The Validation Action was set to Error and Validation Level to Moderate.
当我尝试插入包含 test 和 test1 字段的文档时,出现验证错误
When I try to insert a document containing the field test and test1, I am getting a validation error
Document:
db.dmt9.insert([{test:"123"},{test1:"456"}])
Error:
BulkWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5dd511de1f7b2047ed527044"),
"test" : "123"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
由于我要插入一个包含AnyOf内字段之一的文档,所以该文档不能插入成功吗?如果尝试将验证操作"更改为警告",则会插入文档,但是我可以插入其他任何字段.
Since I am inserting a document containing one of the fields inside the AnyOf, shouldn't the document be inserted successfully? If I try and change the Validation Action to Warning, the document is inserted however I can insert any other field.
Test with validation Action - Warning
rs0:PRIMARY> db.dmt9.insert([{test:"123"},{test1:"456"},{test9:123}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
更新2: 当我删除现场测试时,我得到了这个验证器:
Update 2: When I remove the field test I get this validator:
{
$jsonSchema: {
bsonType: 'object',
additionalProperties: false,
anyOf: [
{
bsonType: 'object',
properties: {
test1: {
bsonType: 'string'
}
},
additionalProperties: false
},
{
bsonType: 'object',
properties: {
test2: {
bsonType: 'string'
}
},
additionalProperties: false
}
],
properties: {
_id: {
bsonType: 'objectId'
}
}
}
}
当我再次尝试插入test1时,我仍然收到错误消息文档验证失败"
and when i try to insert test1 again I still get the error message Document failed validation
rs0:PRIMARY> db.dmt8.insert([{test1:"123"}])
BulkWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 121,
"errmsg" : "Document failed validation",
"op" : {
"_id" : ObjectId("5dd51b4766ba25a01fbcf8e6"),
"test1" : "123"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
推荐答案
{test:"123"}
验证失败,因为它不符合anyOf
中的任何架构,而这些架构仅需要test1
或test2
键.
{test:"123"}
fails validation because it doesn't conform to any of the schemas in anyOf
, which need test1
or test2
as the only key.
anyOf
将每个子模式应用于您的实例,并在至少一个子模式通过验证时断言有效.
anyOf
applies each subschema to your instance, and asserts valid if at least one of the subschemas passes validation.
{test1: "123" }
失败,因为根架构additionalProperties: false
阻止了对象中未在SAME架构对象properties
或patternProperties
中定义的任何键.
{test1: "123" }
fails because the root schemas additionalProperties: false
prevents any keys in your object not defined in the SAME schema object properties
or patternProperties
.
解决方案是进行一些重复.
The solution is to have some duplication.
在 THIS 例子(链路是用于在浏览器测试但draft-仅7),我添加了根属性test1
和test2
.这将允许您通过键test1
或test2
传递数据,但是鉴于我不知道您的要求,因此我无法告诉您如何修改架构以允许键test
通过(因为每个anyOf
子方案都阻止了它).
In THIS example (link is for in browser testing but draft-7 only), I've added root properties test1
and test2
. This will allow data where you have a key of test1
or test2
to pass, but given I don't know your requirements, I can't tell you how to modify the schema to allow an object with a key of test
to pass (as each of the anyOf
subschemas prevent it).
{
"type": "object",
"additionalProperties": false,
"properties": {
"_id": {},
"test": {},
"test1": {},
"test2": {}
},
"anyOf": [
{
"type": "object",
"properties": {
"test1": {}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"test2": {}
},
"additionalProperties": false
}
]
}
如果您要检查插入的内容中是否包含test1
或test2
,那么恐怕JSON Schema无法帮助您. Mongo上下文中的JSON Schema只能单独检查每个项目,并且无法验证可能插入的记录的集合.
If your intent is to check that one of the things you are inserting has test1
or test2
, then I'm afraid JSON Schema cannot help you. JSON Schema in the context of Mongo can only check each item individually, and is not afforded the ability to validate a collection of potentially inserted records.
在上面的示例架构中,我删除了类型检查,因为这与该问题无关,并且bsonType还是与JSON架构类型不同.
In the above example schema, I've removed type checking because that is not relevant to this question, and bsonType differs from JSON Schema type anyway.
这篇关于Mongo Json Schema Validator AnyOf无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!