Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5fe1f5d

Browse files
DOCSP-50472: schema validation (#3400)
* DOCSP-50472: schema validation * apply phpcbf formatting * small wording fix * fixes * log error * fix int type * wip * PV tech review 1
1 parent 7a0f0bc commit 5fe1f5d

File tree

3 files changed

+82
-14
lines changed

3 files changed

+82
-14
lines changed

‎docs/eloquent-models/schema-builder.txt

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ Overview
2121
--------
2222

2323
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.
2627

2728
The {+odm-short+} supports a subset of the index and collection management methods
2829
in the Laravel ``Schema`` facade.
@@ -33,16 +34,10 @@ in the Laravel documentation.
3334
The following sections describe the Laravel schema builder features available
3435
in 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,60 @@ To learn more about Laravel migrations, see
117112
`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__
118113
in 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+
You can use schema validation to restrict data types and value ranges of
128+
document fields in a specified collection. After you implement schema
129+
validation, the server restricts write operations that don't follow the
130+
validation rules.
131+
132+
You can pass the following parameters to ``jsonSchema()``:
133+
134+
- ``schema``: Array that specifies the validation rules for the
135+
collection. To learn more about constructing a schema, see
136+
the :manual:`$jsonSchema </reference/operator/query/jsonSchema/>`
137+
reference in the {+server-docs-name+}.
138+
139+
- ``validationLevel``: Sets the level of validation enforcement.
140+
Accepted values are ``"strict"`` (default) and ``"moderate"``.
141+
142+
- ``validationAction``: Specifies the action to take when invalid
143+
operations are attempted. Accepted values are ``"error"`` (default) and
144+
``"warn"``.
145+
146+
This example demonstrates how to specify a schema in the
147+
``jsonSchema()`` method when creating a collection. The schema
148+
validation has the following specifications:
149+
150+
- Documents in the ``pilots`` collection must
151+
contain the ``license_number`` field.
152+
153+
- The ``license_number`` field must have an integer value between
154+
``1000`` and ``9999``.
155+
156+
- If you attempt to perform invalid write operations, the server raises
157+
an error.
158+
159+
.. literalinclude:: /includes/schema-builder/flights_migration.php
160+
:language: php
161+
:dedent:
162+
:start-after: begin-json-schema
163+
:end-before: end-json-schema
164+
165+
If you attempt to insert a document into the ``pilots`` collection that
166+
violates the schema validation rule, {+odm-long+} returns a
167+
:php:`BulkWriteException <mongodb-driver-exception-bulkwriteexception>`.
168+
120169
.. _laravel-eloquent-collection-exists:
121170

122171
Check Whether a Collection Exists

‎docs/includes/schema-builder/flights_migration.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ public function up(): void
1919
$collection->unique('mission_id', options: ['name' => 'unique_mission_id_idx']);
2020
});
2121
// end create index
22+
23+
// begin-json-schema
24+
Schema::create('pilots', function (Blueprint $collection) {
25+
$collection->jsonSchema(
26+
schema: [
27+
'bsonType' => 'object',
28+
'required' => ['license_number'],
29+
'properties' => [
30+
'license_number' => [
31+
'bsonType' => 'int',
32+
'minimum' => 1000,
33+
'maximum' => 9999,
34+
],
35+
],
36+
],
37+
validationAction: 'error',
38+
);
39+
});
40+
// end-json-schema
2241
}
2342

2443
public function down(): void

‎docs/quick-start/backend-service-tutorial.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _laravel-tutorial-backend-service:
22

3-
==========================================================
3+
===========================================================
44
Tutorial: Build a Back End Service by Using {+odm-long+}
5-
==========================================================
5+
===========================================================
66

77
.. facet::
88
:name: genre

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /