Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-20547

Expose the reason an operation fails document validation

    • Fully Compatible
    • Query 2020-09-21, Query 2020-10-05, Query 2020-10-19, Query 2020-11-02

      Issue Status as of October 21, 2020

      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.

        1. SERVER-20547.patch
          116 kB
          Kyle Suarez

            Created:
            Updated:
            Resolved: