[SERVER-30191] Add JSON Schema support for document validation Created: 17/Jul/17  Updated: 30/Oct/23  Resolved: 19/Sep/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 3.6.0-rc0

Type: New Feature Priority: Major - P3
Reporter: David Storch Assignee: Backlog - Query Team (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
is documented by DOCS-10810 Docs for SERVER-30191: Add JSON Schem... Closed
Duplicate
is duplicated by SERVER-24846 disallow extra fields when validating... Closed
Related
related to SERVER-20718 $type should take an array of types t... Closed
is related to SERVER-1176 Unique index on array keys only Closed
Assigned Teams:
Query
Backwards Compatibility: Fully Compatible
Participants:

 Description   

We will add support for JSON Schema via a new $jsonSchema operator in the match language. The following syntax can be used to create a collection with a JSON Schema specification as <schema>:

db.createCollection("myCollectionName", {validator: {$jsonSchema: <schema>}});

For example, the following creates a collection called stringsOnly in which the str field, if present, must be of type "string":

> db.stringsOnly.drop()
false
> db.createCollection("stringsOnly", {validator: {$jsonSchema: {properties: {str: {type: "string"}}}}});
{ "ok" : 1 }
> db.stringsOnly.insert({str: "foo"})
WriteResult({ "nInserted" : 1 })
> db.stringsOnly.insert({str: 1})
WriteResult({
	"nInserted" : 0,
	"writeError" : {
		"code" : 121,
		"errmsg" : "Document failed validation"
	}
})

We will support draft 4 of the JSON Schema spec, as described by https://tools.ietf.org/html/draft-zyp-json-schema-04 and https://tools.ietf.org/html/draft-fge-json-schema-validation-00. However, we cannot claim to be fully compliant with draft 4, since MongoDB's initial schema support will omit the following:

  • Hypermedia and linking properties of JSON Schema. The use of JSON References and JSON Pointers will not be supported.
  • Meta-validation of the schema specification via "$schema", a special JSON Schema-specific keyword that self-describes the schema itself. This keyword will be explicitly rejected, as opposed to silently ignored.
  • Semantic validation of strings via the "format" keyword.

We will support a few extensions to JSON schema which make sense for MongoDB. In particular, type validation will support not just number, integer, string, object, and array, but will also support the full list of BSON types.



 Comments   
Comment by David Storch [ 19/Sep/17 ]

Support for JSON Schema via the $jsonSchema operator in the match language will be available in upcoming development release 3.5.14.

Comment by David Storch [ 05/Sep/17 ]

jesse, note that $jsonSchema is a match language operator just like $and or $text. I assume that driver helpers for createCollection can already handle arbitrary match expressions within the validator argument?

Comment by A. Jesse Jiryu Davis [ 18/Jul/17 ]

We'll need to check that all drivers accept arbitrary options in their create-collection helper methods.

Generated at Thu Feb 08 04:22:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.