$jsonSchema如何使用$ref

vik*_*kin 3 validation jsonschema mongodb

我正在使用 $jsonSchema 检查 mongo 层上的 json 模式验证。我需要定义一个多语言对象。我使用$ref。但我收到错误 - $jsonSchema 关键字“定义”当前不受支持根据文档,它必须具有常见的 json 模式行为。

 async install(): Promise<void> {
        let db = await this.dbContainer.getDb();
        let properties = _.fromPairs(this.config.languages.map((language) => {
           return  [language, {$ref: '#/definitions/post'}]
        }));
        await db.createCollection(
            this.collectionName,
            {
                validator: {
                    $jsonSchema: {
                        bsonType: 'object',
                        required: ['title', 'summary', 'link'],
                        definitions: {
                            post: {
                                title: {
                                    bsonType: 'string',
                                    description: 'Title is required'
                                },
                                summary: {
                                    bsonType: 'string',
                                    description: 'Summary is required'
                                },
                                href: {
                                    bsonType: 'string',
                                    description: 'Summary is required'
                                },
                            },
                        },
                        properties,
                    }
                }
            }
        );
Run Code Online (Sandbox Code Playgroud)

有人有什么建议吗?我应该使用 json-schema 作为单独的库吗?

Dan*_*iel 5

这里描述的 JSON Schema 规范与mongojsonSchema中引入的规范相似但不相同。

在mongo 文档的ommion部分,您可以看到$ref不支持。

在此输入图像描述