Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$jsonSchema how to use $ref

I am checking json schema validation on the mongo layer using $jsonSchema. I need define a multilingual object. I use $ref. But I receive the error - $jsonSchema keyword 'definitions' is not currently supported According to the documentation it has to have a common json-schema behavior.

 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,
                    }
                }
            }
        );

Does anybody has any suggestions? Should I use json-schema as a separated library?

like image 937
Victor Shelepen Avatar asked Jun 04 '26 16:06

Victor Shelepen


1 Answers

JSON Schema as specification described here is similar but not the same as jsonSchema introduced in mongo.

In ommision section of mongo documentation, you can see that $ref is not supported.

enter image description here

like image 103
Daniel Avatar answered Jun 06 '26 06:06

Daniel