Better validation output of $jsonSchema

I’m trying to apply $jsonSchema validation to existing data, and it’s quite hard to debug what’s actually wrong with the data: I can find faulty documents, but it would be nice if there was a way to report what’s actually wrong with them, e.g. having a more detailed reporting like “field X is required but it’s missing”. With large documents it’s a nightmare trying to debug them.

Is there some library or a tool that could do that?

Hi @dimaip,

Which version of MongoDB are you running? If have a vague memory of the output being improved at some point.

Cheers,
Maxime.

Hi @dimaip,

As @MaBeuLux88 mentioned, JSON Schema validation was improved in MongoDB 5.0 to report more detailed validation errors: Improved Error Messages for Schema Validation in MongoDB 5.0 | MongoDB.

There are also client libraries for JSON Schema validation available for most languages: Implementations | JSON Schema. Those may need a slight tweak for MongoDB extensions including the bsonType keyword. For example AJV is a popular (and extensible) Node.js library.

Typically you want to perform validation as early as possible to provide fast feedback for the user. There are opportunities to validate and provide feedback client-side, in your application layer, and at the database level. JSON Schema should help with creating reusable validation rules.

Regards,
Stennie

2 Likes