I have tags schema created in mongo database. I am putting validation verification using JsonSchema. { $jsonSchema: { bsonType: "object", required: ["name"], properties: { name: { bsonType: "string", description: "Tag name must be a string and is required.", }, }, }, }
when I try request with the { "name": 1 }
. I don’t get the detailed error message mentioning the "“Tag name must be a string and is required.” as reponse to my API, when I run this in shell i get detailed response {
failingDocumentId: {},
details: {
operatorName: ‘$jsonSchema’,
schemaRulesNotSatisfied: [
{
operatorName: ‘properties’,
propertiesNotSatisfied: [
{
propertyName: ‘name’,
details: [
{
operatorName: ‘bsonType’,
specifiedAs: {
bsonType: ‘string’
},
reason: ‘type did not match’,
consideredValue: 1,
consideredType: ‘int’
}
]
}
]
}
]
}
} is it not enabled on the MongoDB atlas, I am using 5.0.15 cluster any help will be appreciated.
Have you tried adding , validationAction: "error"
before the last }
?
Thank you for your time!
{
$jsonSchema: {
bsonType: 'object',
required: [
'name'
],
properties: {
name: {
bsonType: 'string',
description: 'Tag name must be a string and is required.'
}
}
}
}
This is my schema. i get this message in the mongo db compass shell,
Additional information:
{
failingDocumentId: {},
details: {
operatorName: '$jsonSchema',
schemaRulesNotSatisfied: [
{
operatorName: 'properties',
propertiesNotSatisfied: [
{
propertyName: 'name',
details: [
{
operatorName: 'bsonType',
specifiedAs: {
bsonType: 'string'
},
reason: 'type did not match',
consideredValue: 1,
consideredType: 'int'
}
]
}
]
}
]
}
}
but when I make an REST API call with the request
{
"name": 1
}
I get only this as error message
Document failed validation
expected result was to get the detailed error message, I am using Mongodb Atlas.
Reference link : Improved Error Messages for Schema Validation in MongoDB 5.0 | MongoDB
1 Like
I still would suggest trying this.
{
$jsonSchema: {
bsonType: 'object',
required: [
'name'
],
properties: {
name: {
bsonType: 'string',
description: 'Tag name must be a string and is required.'
}
}
},
validationAction: 'error'
}
Hi Thank you for your time, I added this but I still get the legacy message. MongoError: “Document failed validation” and not the details as mentioned in the article.
What version of MongoDB Server are you running?
5.0.15 it was recently upgraded from 4.4 automatically I think.
Have you examined the System Log Options in your MongoDB configuration file?