From ee8436880bd72f82454844e4e9806e8af2ab3524 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Wed, 21 Mar 2018 11:35:48 +0100 Subject: [PATCH] DATAMONGO-1891 - Improve $jsonSchema documentation. Original pull request: #542. --- src/main/asciidoc/reference/mongodb.adoc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index 685ce9ba1..84d0346b1 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -1487,7 +1487,7 @@ WARNING: Indexes are only used if the collation used for the operation and the i [[mongo.jsonSchema]] === JSON Schema -As of version 3.6 MongoDB supports collections that validate ``Document``s against a provided JSON Schema. +As of version 3.6 MongoDB supports collections that validate ``Document``s against a provided https://docs.mongodb.com/manual/core/schema-validation/#json-schema[JSON Schema]. The schema itself and both validation action and level can be defined when creating the collection. .Sample JSON schema @@ -1547,6 +1547,18 @@ MongoJsonSchema.builder() <1> <4> Build the schema object. Use the schema to either create a collection or <>. ==== +There are already some predefined strongly typed `JsonSchemaObject` and `JsonSchemaProperty` available via static methods on the gateway interfaces. +However there may arise the need to build custom property validation rules which can be done via builder API. + +[source,java] +---- +// "birthdate" : { "bsonType": "date" } +JsonSchemaProperty.named("birthdate").ofType(Type.dateType()); + +// "birthdate" : { "bsonType": "date", "description", "Must be a date" } +JsonSchemaProperty.named("birthdate").with(JsonSchemaObject.of(Type.dateType()).description("Must be a date")); +---- + `CollectionOptions` provides the entry point to schema support for collections. .Create collection with `$jsonSchema`