【问题标题】:json schema validation fails despite correct type尽管类型正确,但 json 模式验证失败
【发布时间】:2018-09-03 12:32:22
【问题描述】:

我有一个相当大的 json 架构。有问题的部分是模式中称为“翻译”的较小模式,它看起来像这样:

"translations": {
    "bsonType": "object",
    "patternProperties": {
        "id": {
            "bsonType": "string"
        },
        "^[a-z]{2}$": {
            "anyOf": [
                {
                    "bsonType": "object"
                },
                {
                    "bsonType": "array"
                }
            ]
        }
    }
}

由正则表达式定义的对象包含更多属性(例如,称为“文本”的字段),数组是这些对象的数组,但我只留下了对理解结构很重要的部分。

我的问题是,当我针对此架构验证我的文件时,每个文件都失败了,但是当我从 anyOf 数组中的第一个对象中删除“bsonType”:“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" 工作正常。

【问题讨论】:

  • 能否提供一些NodeJS代码?

标签: json mongodb jsonschema


【解决方案1】:

我认为你的 id 与正则表达式冲突的问题试试这个:

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多