Details properties for schema validation in mongosh

hi I am testing out the schema validation for documents. I am using mongosh v1.5.4 in ubuntu 20.

db.createCollection("post", {
  validator: {
    $jsonSchema: {
      bsonType: "object",
      required: ["title", "creator"],
      properties: {
        title: {
          bsonType: "string",
          description: "must be a string and is required",
        },
        text: { bsonType: "string", description: "must be a string" },
        creator: {
          bsonType: "objectId",
          description: "must be an object id and is required",
        },
        comments: {
          bsonType: "array",
          description: "must be an array",
          items: {
            bsonType: "object",
            required: ["text", "author"],
            properties: {
              text: { bsonType: "string", description: "must be a string" },
              author: {
                bsonType: "objectId",
                description: "must be an objectId and is required",
              },
            },
          },
        },
      },
    },
  },
});

using the above I created a collection with validation. When I try to add a document with wrong structure it fails as expected but the details field in the error is not readable

 db.post.insertOne({title:"My First Post",text:"Lorem ipsum dolor",tags:["new","tech"],creator:ObjectId("62ebe7c819cfdcc24a7f5017"),comments:[{author:ObjectId("62ebe7c819cfdcc24a7f5017")}]})
MongoServerError: Document failed validation
Additional information: {
  failingDocumentId: ObjectId("62ebf4fb19cfdcc24a7f501f"),
  details: {
    operatorName: '$jsonSchema',
    schemaRulesNotSatisfied: [
      {
        operatorName: 'properties',
        propertiesNotSatisfied: [
          {
            propertyName: 'comments',
            description: 'must be an array',
            details: [ [Object] ]
          }
        ]
      }
    ]
  }
}

can someone help me with this?

@Siddharth_Badola by default, mongosh only expands objects 6 levels down. You can configure it to go deeper, e.g. 100 or even Infinity potentially:

config.set('inspectDepth', 100)
2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.