-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-50472: schema validation #3400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
1c45cb2
aa5e8cf
a6e72f5
737a009
24152bb
1073888
ea1d3a6
84e8c85
e425c9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -21,8 +21,9 @@ Overview | |||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Laravel provides a **facade** to access the schema builder class ``Schema``, | ||||||||||||||||||||||||||||
which lets you create and modify tables. Facades are static interfaces to | ||||||||||||||||||||||||||||
classes that make the syntax more concise and improve testability. | ||||||||||||||||||||||||||||
which lets you create and modify tables, or collections in MongoDB. | ||||||||||||||||||||||||||||
Facades are static interfaces to classes that make the syntax more | ||||||||||||||||||||||||||||
concise and improve testability. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
The {+odm-short+} supports a subset of the index and collection management methods | ||||||||||||||||||||||||||||
in the Laravel ``Schema`` facade. | ||||||||||||||||||||||||||||
|
@@ -33,16 +34,10 @@ in the Laravel documentation. | |||||||||||||||||||||||||||
The following sections describe the Laravel schema builder features available | ||||||||||||||||||||||||||||
in the {+odm-short+} and show examples of how to use them: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- :ref:`<laravel-eloquent-migrations>` | ||||||||||||||||||||||||||||
- :ref:`<laravel-eloquent-collection-exists>` | ||||||||||||||||||||||||||||
- :ref:`<laravel-eloquent-indexes>` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. note:: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
The {+odm-short+} supports managing indexes and collections, but | ||||||||||||||||||||||||||||
excludes support for MongoDB JSON schemas for data validation. To learn | ||||||||||||||||||||||||||||
more about JSON schema validation, see :manual:`Schema Validation </core/schema-validation/>` | ||||||||||||||||||||||||||||
in the {+server-docs-name+}. | ||||||||||||||||||||||||||||
- :ref:`laravel-eloquent-migrations` | ||||||||||||||||||||||||||||
- :ref:`laravel-eloquent-schema-validation` | ||||||||||||||||||||||||||||
- :ref:`laravel-eloquent-collection-exists` | ||||||||||||||||||||||||||||
- :ref:`laravel-eloquent-indexes` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. _laravel-eloquent-migrations: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -117,6 +112,63 @@ To learn more about Laravel migrations, see | |||||||||||||||||||||||||||
`Database: Migrations <https://laravel.com/docs/{+laravel-docs-version+}/migrations>`__ | ||||||||||||||||||||||||||||
in the Laravel documentation. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. _laravel-eloquent-schema-validation: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Implement Schema Validation | ||||||||||||||||||||||||||||
--------------------------- | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
You can use the ``jsonSchema()`` method to implement :manual:`schema | ||||||||||||||||||||||||||||
validation </core/schema-validation/>` when using the following schema | ||||||||||||||||||||||||||||
builder methods: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- ``Schema::create()``: When creating a new collection | ||||||||||||||||||||||||||||
- ``Schema::table()``: When updating collection properties | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
After you implement schema validation, the server allows you to run only | ||||||||||||||||||||||||||||
those write operations which follow the validation rules. Use schema | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||
validation to restrict data types and value ranges of document fields in | ||||||||||||||||||||||||||||
a specified collection. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: i wonder if the connection between these two sentences would be clearer if you switched them. something like:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Before creating a collection with schema validation rules, you must | ||||||||||||||||||||||||||||
define a JSON schema. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
The schema is a JSON object that contains key-value pairs | ||||||||||||||||||||||||||||
specifying the validation rules for your collection. At the top level, | ||||||||||||||||||||||||||||
this object must include a ``$jsonSchema`` object. The ``$jsonSchema`` | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So this documentation may be valid for MongoDB server but not for Laravel usage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh also! Since this is PHP, the schema argument is not an object, but an associative array. |
||||||||||||||||||||||||||||
object can include the following fields: | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S: break up paragraphs differently. could also then remove first 'JSON'
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed this paragraph per PV suggestion |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- ``title``: Sets an optional title for the schema. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we actually include this documentation? Seems to me the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense - I'll cut out some of this text and apply the suggestions from your prev comment. |
||||||||||||||||||||||||||||
- ``required``: Specifies a list of required fields for each document in your collection. | ||||||||||||||||||||||||||||
- ``properties``: Sets property requirements for individual fields. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I: no periods
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removed this text |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
To view a full list of JSON schema object fields, see :manual:`JSON Schema | ||||||||||||||||||||||||||||
</reference/operator/query/jsonSchema/#json-schema>` in the {+server-docs-name+}. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
You can optionally pass the following parameters to ``jsonSchema()``: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- ``validationLevel``: Sets the level of validation enforcement. | ||||||||||||||||||||||||||||
Accepted values are ``"strict"`` (default) and ``"moderate"``. | ||||||||||||||||||||||||||||
- ``validationAction``: Specifies the action to take when invalid | ||||||||||||||||||||||||||||
operations are attempted. Accepted values are ``"error"`` (default) and | ||||||||||||||||||||||||||||
``"warn"``. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
This example demonstrates how to pass a JSON schema to the | ||||||||||||||||||||||||||||
``jsonSchema()`` method when creating a collection. The schema | ||||||||||||||||||||||||||||
validation specifies that documents in the ``pilots`` collection must | ||||||||||||||||||||||||||||
contain the ``license_number`` field with an integer value between | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to a list |
||||||||||||||||||||||||||||
``1000`` and ``9999``. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. literalinclude:: /includes/schema-builder/flights_migration.php | ||||||||||||||||||||||||||||
:language: php | ||||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||||
:start-after: begin-json-schema | ||||||||||||||||||||||||||||
:end-before: end-json-schema | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
If you attempt to insert a document into the ``pilots`` collection that | ||||||||||||||||||||||||||||
violates the schema validation rule, {+odm-long+} returns a | ||||||||||||||||||||||||||||
:php:`BulkWriteException <mongodb-driver-exception-bulkwriteexception>` | ||||||||||||||||||||||||||||
because the validation action is set to ``"error"``. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
.. _laravel-eloquent-collection-exists: | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Check Whether a Collection Exists | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: can you only implement schema validation when using these methods for these specific purposes? or is the info after the colons just a reminder of the method's purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the only two methods in Laravel's
Schema
that accept a callback to alter columns, so you can only implement schema validation on either creating a collection or updating it.