@@ -21,8 +21,9 @@ Overview
2121--------
2222
2323Laravel provides a **facade** to access the schema builder class ``Schema``,
24- which lets you create and modify tables. Facades are static interfaces to
25- classes that make the syntax more concise and improve testability.
24+ which lets you create and modify tables, or collections in MongoDB.
25+ Facades are static interfaces to classes that make the syntax more
26+ concise and improve testability.
2627
2728The {+odm-short+} supports a subset of the index and collection management methods
2829in the Laravel ``Schema`` facade.
@@ -33,16 +34,10 @@ in the Laravel documentation.
3334The following sections describe the Laravel schema builder features available
3435in the {+odm-short+} and show examples of how to use them:
3536
36- - :ref:`<laravel-eloquent-migrations>`
37- - :ref:`<laravel-eloquent-collection-exists>`
38- - :ref:`<laravel-eloquent-indexes>`
39-
40- .. note::
41-
42- The {+odm-short+} supports managing indexes and collections, but
43- excludes support for MongoDB JSON schemas for data validation. To learn
44- more about JSON schema validation, see :manual:`Schema Validation </core/schema-validation/>`
45- in the {+server-docs-name+}.
37+ - :ref:`laravel-eloquent-migrations`
38+ - :ref:`laravel-eloquent-schema-validation`
39+ - :ref:`laravel-eloquent-collection-exists`
40+ - :ref:`laravel-eloquent-indexes`
4641
4742.. _laravel-eloquent-migrations:
4843
@@ -117,6 +112,63 @@ To learn more about Laravel migrations, see
117112`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
118113in the Laravel documentation.
119114
115+ .. _laravel-eloquent-schema-validation:
116+
117+ Implement Schema Validation
118+ ---------------------------
119+
120+ You can use the ``jsonSchema()`` method to implement **:manual:`schema
121+ validation </core/schema-validation/>`** when using the following schema
122+ builder methods:
123+
124+ - ``Schema::create()``: When creating a new collection.
125+ - ``Schema::table()``: When updating collection properties.
126+
127+ After you implement schema validation, the server allows you to run only
128+ those write operations which follow the validation rules. Use schema
129+ validation to restrict data types and value ranges of document fields in
130+ a specified collection.
131+
132+ Before creating a collection with schema validation rules, you must
133+ define a JSON schema.
134+
135+ The schema is a JSON object that contains key-value pairs
136+ specifying the validation rules for your collection. At the top level,
137+ this object must include a ``$jsonSchema`` object. The ``$jsonSchema``
138+ object includes the following fields:
139+
140+ - ``title``: Sets an optional description for the schema.
141+ - ``required``: Specifies a list of required fields for each document in your collection.
142+ - ``properties``: Sets property requirements for individual fields.
143+
144+ For a full list of JSON schema object fields, see :manual:`JSON Schema
145+ </reference/operator/query/jsonSchema/#json-schema>` in the {+server-docs-name+}.
146+
147+ You can optionally pass the following parameters to ``jsonSchema()``:
148+
149+ - ``validationLevel``: Sets the level of validation enforcement.
150+ Accepted values are ``"strict"`` and ``"moderate"``.
151+ - ``validationAction``: Specifies the action to take when invalid
152+ operations are attempted. Accepted values are ``"error"`` and
153+ ``"warn"``.
154+
155+ This example demonstrates how to pass a JSON schema to the
156+ ``jsonSchema()`` method when creating a collection. The schema
157+ validation specifies that documents in the ``pilots`` collection must
158+ contain the ``license_number`` field with an integer value between
159+ ``1000`` and ``9999``.
160+
161+ .. literalinclude:: /includes/schema-builder/flights_migration.php
162+ :language: php
163+ :dedent:
164+ :start-after: begin-json-schema
165+ :end-before: end-json-schema
166+
167+ If you attempt to insert a document into the ``pilots`` collection that
168+ violates the schema validation rule, {+odm-long+} returns a
169+ :php:`mongodb-driver-exception-bulkwriteexception` because the
170+ validation level is set to ``"error"``.
171+
120172.. _laravel-eloquent-collection-exists:
121173
122174Check Whether a Collection Exists
0 commit comments