@@ -21,8 +21,9 @@ Overview
21
21
--------
22
22
23
23
Laravel 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.
26
27
27
28
The {+odm-short+} supports a subset of the index and collection management methods
28
29
in the Laravel ``Schema`` facade.
@@ -33,16 +34,10 @@ in the Laravel documentation.
33
34
The following sections describe the Laravel schema builder features available
34
35
in the {+odm-short+} and show examples of how to use them:
35
36
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`
46
41
47
42
.. _laravel-eloquent-migrations:
48
43
@@ -117,6 +112,63 @@ To learn more about Laravel migrations, see
117
112
`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
118
113
in the Laravel documentation.
119
114
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
+
120
172
.. _laravel-eloquent-collection-exists:
121
173
122
174
Check Whether a Collection Exists
0 commit comments