MongoDb Json模式验证草案4

问题描述 投票:0回答:1

我正在使用通过JSON模式验证的Mongodb的oneOf验证器。据我所知,MongoDb支持架构验证的草案4,我正在使用的验证规则在this online validator]中显示为有效


{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "foo": {},
      "bar": {}
    },
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "foo": {}
        },
        "additionalProperties": false
      },
      {
        "type": "object",
        "properties": {
           "bar": {}
        },
        "additionalProperties": false
      }
    ]
}

我正在传递的输入文件是

{  
    "bar": {},
}

为什么当我在Mongo中使用相同的架构验证并传入具有属性foo

或属性bar的对象时,它为什么会失败?
{
  $jsonSchema: {
    bsonType: 'object',
    additionalProperties: false,
    properties: {
      foo: {
        bsonType: 'string'
      },
      bar: {
        bsonType: 'string'
      }
    },
    oneOf: [
      {
        bsonType: 'object',
        properties: {
          foo: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      },
      {
        bsonType: 'object',
        properties: {
          bar: {
            bsonType: 'string'
          }
        },
        additionalProperties: false
      }
    ]
  }
}


db.dmt2.insert({"bar": ""})
WriteResult({
        "nInserted" : 0,
        "writeError" : {
                "code" : 121,
                "errmsg" : "Document failed validation"
        }
})

我正在使用通过JSON模式验证的Mongodb的oneOf验证器。据我所知,MongoDb支持模式验证的草案4,而我正在使用的验证规则在此在线上显示为有效...

json jsonschema json-schema-validator
1个回答
0
投票

通过指定_ id

© www.soinside.com 2019 - 2024. All rights reserved.