Skip to content

Commit db12978

Browse files
(DOCS-14761): Validation failures include description
1 parent 680f6a0 commit db12978

File tree

4 files changed

+136
-52
lines changed

4 files changed

+136
-52
lines changed

source/core/schema-validation.txt

Lines changed: 120 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,20 @@ query expression:
144144

145145
:ref:`query operators <query-selectors>`
146146

147-
Behavior
148-
--------
147+
Behavior and Examples
148+
---------------------
149149

150150
Validation occurs during updates and inserts. When you add validation to
151151
a collection, existing documents do not undergo validation checks until
152152
modification.
153153

154+
Validation on Existing Documents
155+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156+
154157
To perform validation checks on existing documents, use the
155158
:dbcommand:`validate` command or the :method:`db.collection.validate()`
156159
shell helper.
157160

158-
159-
Existing Documents
160-
~~~~~~~~~~~~~~~~~~
161-
162161
The ``validationLevel`` option determines which operations MongoDB
163162
applies the validation rules:
164163

@@ -176,7 +175,7 @@ documents:
176175

177176
.. code-block:: json
178177

179-
db.contacts.insert([
178+
db.contacts.insertMany([
180179
{ "_id": 1, "name": "Anne", "phone": "+1 555 123 456", "city": "London", "status": "Complete" },
181180
{ "_id": 2, "name": "Ivan", "city": "Vancouver" }
182181
])
@@ -194,11 +193,11 @@ collection:
194193
properties: {
195194
phone: {
196195
bsonType: "string",
197-
description: "must be a string and is required"
196+
description: "phone must be a string and is required"
198197
},
199198
name: {
200199
bsonType: "string",
201-
description: "must be a string and is required"
200+
description: "name must be a string and is required"
202201
}
203202
}
204203
} },
@@ -230,12 +229,12 @@ validation rule we created above that requires ``name`` to be a string.
230229

231230
.. code-block:: javascript
232231

233-
db.contacts.update(
232+
db.contacts.updateOne(
234233
{ _id: 1 },
235234
{ $set: { name: 10 } }
236235
)
237236

238-
db.contacts.update(
237+
db.contacts.updateOne(
239238
{ _id: 2 },
240239
{ $set: { name: 20 } }
241240
)
@@ -247,48 +246,44 @@ document did not meet the initial criteria when validation was added.
247246

248247
.. code-block:: javascript
249248
:copyable: false
250-
:emphasize-lines: 9-27
251249

252250
// _id: 1
253-
WriteResult({
254-
"nMatched" : 0,
255-
"nUpserted" : 0,
256-
"nModified" : 0,
257-
"writeError" : {
258-
"code" : 121,
259-
"errmsg" : "Document failed validation",
260-
"errInfo" : {
261-
"failingDocumentId" : 1,
262-
"details" : {
263-
"operatorName" : "$jsonSchema",
264-
"schemaRulesNotSatisfied" : [
251+
MongoServerError: Document failed validation
252+
Additional information: {
253+
failingDocumentId: 1,
254+
details: {
255+
operatorName: '$jsonSchema',
256+
schemaRulesNotSatisfied: [
257+
{
258+
operatorName: 'properties',
259+
propertiesNotSatisfied: [
265260
{
266-
"operatorName" : "properties",
267-
"propertiesNotSatisfied" : [
268-
{
269-
"propertyName" : "name",
270-
"details" : [
271-
{
272-
"operatorName" : "bsonType",
273-
"specifiedAs" : {
274-
"bsonType" : "string"
275-
},
276-
"reason" : "type did not match",
277-
"consideredValue" : 10,
278-
"consideredType" : "double"
279-
}
280-
]
281-
}
282-
]
283-
}
284-
]
261+
propertyName: 'name',
262+
description: 'name must be a string and is required',
263+
details: [
264+
{
265+
operatorName: 'bsonType',
266+
specifiedAs: { bsonType: 'string' },
267+
reason: 'type did not match',
268+
consideredValue: 10,
269+
consideredType: 'int'
270+
}
271+
]
272+
}
273+
]
285274
}
286-
}
287-
}
288-
})
275+
]
276+
}
277+
}
289278

290279
// _id: 2
291-
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
280+
{
281+
acknowledged: true,
282+
insertedId: null,
283+
matchedCount: 1,
284+
modifiedCount: 0,
285+
upsertedCount: 0
286+
}
292287

293288
To disable validation entirely, you can set ``validationLevel`` to
294289
``off``.
@@ -333,16 +328,13 @@ Schema validator:
333328
validationAction: "warn"
334329
} )
335330

336-
With the ``warn`` :collflag:`validationAction`, MongoDB logs any
337-
violations but allows the insertion or update to proceed.
338-
339331
For example, the following insert operation violates the validation rule:
340332

341333
.. code-block:: javascript
342334

343335
db.contacts2.insert( { name: "Amanda", status: "Updated" } )
344336

345-
However, since the ``validationAction`` is ``warn`` only, MongoDB only
337+
However, since the ``validationAction`` is ``warn``, MongoDB only
346338
logs the validation violation message and allows the operation to
347339
proceed. Run the following command to view the MongoDB logs:
348340

@@ -384,6 +376,83 @@ readability) contains information like this:
384376
\"specifiedAs\":{\"required\":[\"phone\"]},
385377
\"missingProperties\":[\"phone\"]}]}}}}"
386378

379+
.. _validation-description-example:
380+
381+
Use Title and Description Fields to Clarify Validation Rules
382+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
383+
384+
.. include:: /includes/fact-5.1-schema-validation-description-overview.rst
385+
386+
Consider a ``users`` collection with the following schema validation:
387+
388+
.. code-block:: javascript
389+
390+
db.runCommand( {
391+
collMod: "users",
392+
validator: { $jsonSchema: {
393+
bsonType: "object",
394+
title: "Email validation",
395+
properties: {
396+
email: {
397+
"bsonType": "string",
398+
"pattern": "^@mongodb\.com$",
399+
"description": "Email address must end with '@mongodb.com'"
400+
},
401+
}
402+
} },
403+
validationLevel: "moderate"
404+
} )
405+
406+
The ``pattern`` field indicates that all ``email`` fields must
407+
end with ``@mongodb.com``. If you try to insert a document that
408+
does not match the pattern, MongoDB includes the validation
409+
``title`` and ``description`` in the error output:
410+
411+
Input:
412+
413+
.. code-block:: javascript
414+
415+
db.users.insertOne( { "name": "Amelia Morrison", "email": "[email protected]" } )
416+
417+
Output:
418+
419+
.. code-block:: javascript
420+
:copyable: false
421+
:emphasize-lines: 6,13
422+
423+
MongoServerError: Document failed validation
424+
Additional information: {
425+
failingDocumentId: ObjectId("614a10bab93bbd15dd2e2eb6"),
426+
details: {
427+
operatorName: '$jsonSchema',
428+
title: 'Email validation',
429+
schemaRulesNotSatisfied: [
430+
{
431+
operatorName: 'properties',
432+
propertiesNotSatisfied: [
433+
{
434+
propertyName: 'email',
435+
description: "Email address must end with '@mongodb.com'",
436+
details: [
437+
{
438+
operatorName: 'pattern',
439+
specifiedAs: { pattern: '^@mongodb.com$' },
440+
reason: 'regular expression did not match',
441+
consideredValue: '[email protected]'
442+
}
443+
]
444+
}
445+
]
446+
}
447+
]
448+
}
449+
}
450+
451+
.. note::
452+
453+
The validation error output is formatted differently in the legacy
454+
``mongo`` shell.
455+
387456
Restrictions
388457
------------
389458

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Starting in MongoDB 5.1, when a document fails :ref:`schema validation
2+
<schema-validation-overview>`, MongoDB includes the validation ``title``
3+
and ``description`` in the error response. You can use these fields to
4+
provide a clearer explanation of the validation when the rules
5+
are not immediately clear, such as when using regular expressions.

source/reference/operator/query/jsonSchema.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ Available Keywords
489489
* - description
490490
- N/A
491491
- string
492-
- A string that describes the schema and has no effect.
492+
- A string that describes the schema and has no effect on
493+
validation. Starting in MongoDB 5.1, if the ``description`` field
494+
is specified, MongoDB includes the ``description`` in the error
495+
output when a document fails validation.
493496

494497
.. _jsonSchema-extension:
495498

source/release-notes/5.1.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ If the new parameter
8080
the :ref:`query explain plan output
8181
<explain-results-enhanced-execution>`.
8282

83+
Schema Validation Errors Contain Description Field
84+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85+
86+
.. include:: /includes/fact-5.1-schema-validation-description-overview.rst
87+
88+
For an example, see :ref:`validation-description-example`.
89+
8390
.. _5.1-rel-notes-repl-sets:
8491

8592
Replica Sets

0 commit comments

Comments
 (0)