尽管类型正确,json模式验证仍失败

尽管类型正确,json模式验证仍失败,json,mongodb,jsonschema,Json,Mongodb,Jsonschema,我有一个相当大的json模式。有问题的部分是模式中名为“translations”的较小模式,如下所示: "translations": { "bsonType": "object", "patternProperties": { "id": { "bsonType": "string" }, "^[a-z]{2}$": { "anyOf": [ {

我有一个相当大的json模式。有问题的部分是模式中名为“translations”的较小模式,如下所示:

"translations": {
    "bsonType": "object",
    "patternProperties": {
        "id": {
            "bsonType": "string"
        },
        "^[a-z]{2}$": {
            "anyOf": [
                {
                    "bsonType": "object"
                },
                {
                    "bsonType": "array"
                }
            ]
        }
    }
}
其中,正则表达式定义的对象包含更多属性(例如,一个名为“text”的字段),数组是这些对象的数组,但我只留下了对理解结构很重要的部分

我的问题是,当我根据此模式验证文件时,每一个都会失败,但当我从anyOf数组的第一个对象中删除“bsonType”:“object”时,它会正常工作

我所有的文件都是这样的,翻译对象中至少有一个对象是“object”类型的,它的键是正则表达式。所以我不明白为什么他们失败了

我使用mongoDB 3.6.0

下面是一个失败文件的示例:

 "translations":{  
    "id":"12345",
    "br":{  
       "text":"string1"
    },
    "en":{  
       "text":"string2"
    },
    "ja":[  
       {  
          "text":"string3"
       },
       {  
          "text":"string4"
       }
    ],
    "no":[  
       {  
          "text":"string6"
       },
       {  
          "text":"string7"
       }
    ]
 }

如果不清楚的话,问题是当模式在anyOf数组的第一个对象中定义为“bsonType”:“object”时,像这样的文件会失败,当我去掉它时,它就会工作。anyOf数组的第二个对象中的“bsonType”:“array”工作正常。

我认为您的问题是id与regex冲突,请尝试以下方法:

let MongoClient = require('mongodb').MongoClient;

let collectionName = 'translations';

let scheme =  {
    $jsonSchema:{
        "bsonType": "object",
        "patternProperties": {
            "^id$":{
                "bsonType":"string"
            },
            "^(?!id)([a-z]{2})$": {
                "anyOf": [
                    {
                        "bsonType": "object"
                    },
                    {
                        "bsonType": "array"
                    }
                ]
            }
        },
    }
};

let goodJson ={
    "id": "12345",
    "br":{
        "text":"string1"
    },
    "en":{
        "text":"string2"
    },
    "ja":[
        {
            "text":"string3"
        },
        {
            "text":"string4"
        }
    ],
    "no":[
        {
            "text":"string6"
        },
        {
            "text":"string7"
        }
    ]
};

let badJson ={
    "id": "12345",
    "br":{
        "text":"string1"
    },
    "en":{
        "text":"string2"
    },
    "ja":[
        {
            "text":"string3"
        },
        {
            "text":"string4"
        }
    ],
    "no":[
        {
            "text":"string6"
        },
        {
            "text":"string7"
        }
    ],
    "nt": "not_object_or_array"
};

async function run() {
    let db = await MongoClient.connect('mongodb://localhost:27017/exampleDb');
    let dbo = db.db('mydb');
    let collections = await dbo.collections();
    let collectionsNames = collections.map(c => c.s.name);
    if (collectionsNames.includes(collectionName)) {
        console.log('dropping collection');
        await dbo.collection(collectionName).drop();
    }
    console.log('creating collection');
    await dbo.createCollection(collectionName,  {validator: scheme});
    let translationCollection = dbo.collection(collectionName);
    console.log('this will validate successfully');
    await translationCollection.insertOne(goodJson);
    console.log('this will raise validation error because: "nt": "not_object_or_array"');
    try {
        await translationCollection.insertOne(badJson);
    } catch(error) {
        console.log(error);
    }
    await db.close();
}
run();

你能提供一些NodeJS代码吗?