Summary
Operations which fail document validation when the collection validator’s validationAction is set to ‘error’ will now return an error of the form:
{ "n" : 0, "writeErrors" : [ { “index”: 0, "code" : 121, “errInfo”: {<object describing reason(s) for failure>}, "errmsg" : "Document failed validation" } ], “ok”: 1 }
When the validationAction is set to ‘warn’, a message of the following form will be written in the logs:
{"t":{"$date": <date>},"s":"W", "c":"STORAGE", "id":20294, "ctx":"conn","msg":"Document would fail validation","attr":{"namespace":<namespace>,"document":<document which failed validation>,"errInfo":<object describing the reason(s) for failure>}}
In both cases, the ‘errInfo’ field contains a structured error object that describes why a particular document failed validation.
Example
To get a sense of what these detailed errors look like, suppose the following validator expression is set on a collection named ‘students’:
{$jsonSchema: { bsonType: "object", required: [ "name", "year", "major", "address" ], properties: { name: { bsonType: "string", description: "must be a string and is required" }, year: { bsonType: "int", minimum: 2017, maximum: 3017, description: "must be an integer in [ 2017, 3017 ] and is required" }, major: { enum: [ "Math", "English", "Computer Science", "History", null ], description: "can only be one of the enum values and is required" }, gpa: { bsonType: "double" , description: "must be a double if the field exists" }, address: { bsonType: "object", required: [ "city" ], properties: { street: { bsonType: "string", description: "must be a string if the field exists” }, city: { bsonType: "string", "description": "must be a string and is required" } } } } }
Attempting to insert the document {name: “Mihai”, year: 2019, major: “Computer Science”} will produce the following error under the ‘errInfo’ field:
"errInfo" : { "failingDocumentId" : ObjectId("5f85f78bccef9666ae09c3c1"), "details" : { "operatorName" : "$jsonSchema", "schemaRulesNotSatisfied" : [ { "operatorName" : "properties", "propertiesNotSatisfied" : [ { "propertyName" : "year", "details" : [ { "operatorName" :"bsonType", "specifiedAs" : { "bsonType" : "int" }, "reason" : "type did not match", "consideredValue" : 2019, "consideredType" : "double" } ] } ] }, { "operatorName" : "required", "specifiedAs" : { "required" : [ "name", "year", "major", "address" ] }, "missingProperties" : [ "address" ] } ] } }
The detailed error describes why the document failed validation: the value specified for the ‘year’ field was of the wrong type and the ‘address’ field is required, but was missing.
Version Information
This feature will be available starting in version 4.9.0 once the upgrade (including upgrading the FCV) is fully complete.
We will provide a link to the reference page for this feature in the online documentation when it becomes available.
Original Description
When documents fail validation, there is no feedback as to what failed.
It should be possible to perform an update or insert operation and find out what specific predicate in the validator document cause the operation to fail.
- backports
-
GODRIVER-2042 Returning errInfo - Schema Validation
- Closed
- is duplicated by
-
SERVER-35926 Return error message if schema validation fails
- Closed
-
SERVER-20584 Document Validation should tell WHY a document failed validation
- Closed
-
SERVER-40251 Better validation error on insert and update
- Closed
-
SERVER-48646 JSON Schema validation error does not indicate invalid fields
- Closed
-
SERVER-32093 3.6 $jsonSchema validator error message improvements
- Closed
- related to
-
SERVER-51839 Add error context when document validator with $expr fails by throwing an exception
- Closed
- links to