Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-10810

Docs for SERVER-30191: Add JSON Schema support for document validation

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.6.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Documentation Request Summary:

      We should 1) add mention of JSON Schema support in the release notes for 3.6, and 2) add a reference page for $jsonSchema.

      The reference page should probably include the following:

      • The basic syntax of $jsonSchema.
      • Mention that we are supporting draft 4 of the JSON Schema spec, and links to the official spec.
      • A list of the JSON Schema draft 4 features that we do not support. This is available in the design document linked above.
      • A list of the extensions we have made to draft 4. Further information is available in the design document linked above, but I believe this just boils down to the addition of the bsonType keyword.
      • An example of setting up a document validator using $jsonSchema, which is the intended use case.
      • That featureCompatibilityVersion must be set to "3.6" or higher in order to use $jsonSchema in a document validator. Any such validator must be removed (either by dropping the collection or by collMod) before downgrading to version 3.4 of the server, otherwise a 3.4 server will fail to start up.

      Engineering Ticket 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.

            Assignee:
            pavithra.vetriselvan@mongodb.com Pavithra Vetriselvan
            Reporter:
            kay.kim@mongodb.com Kay Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved:
              6 years, 27 weeks, 3 days ago