-
Notifications
You must be signed in to change notification settings - Fork 29
DOCSP-45363: validation #73
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 3 commits
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 | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,344 @@ | ||||||||||||||||||||||||||
.. _mongoid-modeling-validation: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
=================== | ||||||||||||||||||||||||||
Document Validation | ||||||||||||||||||||||||||
=================== | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. facet:: | ||||||||||||||||||||||||||
:name: genre | ||||||||||||||||||||||||||
:values: reference | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. meta:: | ||||||||||||||||||||||||||
:keywords: ruby framework, odm, schema, code example | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. contents:: On this page | ||||||||||||||||||||||||||
:local: | ||||||||||||||||||||||||||
:backlinks: none | ||||||||||||||||||||||||||
:depth: 2 | ||||||||||||||||||||||||||
:class: singlecol | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Overview | ||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
In this guide, you can learn how to define **validation rules** in your | ||||||||||||||||||||||||||
{+odm+} models. After you implement validation into your models, {+odm+} | ||||||||||||||||||||||||||
prevents you from running write operations that violate the validation | ||||||||||||||||||||||||||
rules. Use document validation to restrict data types and value ranges | ||||||||||||||||||||||||||
of document fields in your collections. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
{+odm+} includes ``ActiveModel::Validations`` from Active Record to | ||||||||||||||||||||||||||
provide validation functionality, including an associated and uniqueness | ||||||||||||||||||||||||||
validator. To learn more, see the `Active Record Validations | ||||||||||||||||||||||||||
<https://guides.rubyonrails.org/active_record_validations.html>`__ | ||||||||||||||||||||||||||
Rails guide and `ActiveModel::Validations | ||||||||||||||||||||||||||
<https://api.rubyonrails.org/classes/ActiveModel/Validations.html>`__ | ||||||||||||||||||||||||||
Rails API documentation. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. note:: Comparing {+odm+} and MongoDB Validation | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Validation in {+odm+} applies only in the context of your | ||||||||||||||||||||||||||
application and differs from creating schema validation rules in | ||||||||||||||||||||||||||
MongoDB. This means that your validation rules do not apply to write | ||||||||||||||||||||||||||
operations that are performed outside of your application. To learn | ||||||||||||||||||||||||||
more about MongoDB schema validation, see :manual:`Schema Validation | ||||||||||||||||||||||||||
</core/schema-validation/>` in the {+server-manual+}. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Validation Helpers | ||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
{+odm+} supports Active Record validation helpers that you can use when defining your | ||||||||||||||||||||||||||
model classes. You can use these helpers to set common validation rules | ||||||||||||||||||||||||||
in your application, such as checking for the presence of a field, | ||||||||||||||||||||||||||
comparing a field value to a specified value, or ensuring that a field | ||||||||||||||||||||||||||
has a unique value. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Define a Validation Rule | ||||||||||||||||||||||||||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Use the ``validates`` macro to create a validation rule, then include | ||||||||||||||||||||||||||
the validation helper and the required specifications for the rule. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. tip:: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Each validation helper accepts one or more field names, which allows you | ||||||||||||||||||||||||||
to define the same rule for multiple fields. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The following code demonstrates how to use the ``presence`` validation | ||||||||||||||||||||||||||
helper to require that ``Person`` instances contain a value for the | ||||||||||||||||||||||||||
``name`` field: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. literalinclude:: /includes/data-modeling/validation.rb | ||||||||||||||||||||||||||
:start-after: start-simple-val | ||||||||||||||||||||||||||
:end-before: end-simple-val | ||||||||||||||||||||||||||
:language: ruby | ||||||||||||||||||||||||||
:emphasize-lines: 5 | ||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
You can learn about other useful validation helpers in the | ||||||||||||||||||||||||||
:ref:`mongoid-common-validations` section of this guide. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _mongoid-common-validations: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Common Validations | ||||||||||||||||||||||||||
------------------ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
In this section, you can learn about the following common validation | ||||||||||||||||||||||||||
rules and view examples that use validation helpers: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
- :ref:`mongoid-compare-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-format-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-inclusion-exclusion-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-presence-absence-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-uniqueness-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-association-validation` | ||||||||||||||||||||||||||
- :ref:`mongoid-other-validation` | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _mongoid-compare-validation: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Comparison Rule | ||||||||||||||||||||||||||
~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
You can use the ``comparison`` helper to validate a document based on | ||||||||||||||||||||||||||
the value of a specified field. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The ``comparison`` helper supports the following options: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
- ``greater_than``: The value must be greater than the supplied value. | ||||||||||||||||||||||||||
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. [q] Another Mongoid question 😄 In the linked Active Record docs, they format the options like 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 don't think it's necessary, as we provide an example that shows the symbol syntax! |
||||||||||||||||||||||||||
- ``greater_than_or_equal_to``: The value must be greater than or equal to the supplied value. | ||||||||||||||||||||||||||
- ``equal_to``: The value must be equal to the supplied value. | ||||||||||||||||||||||||||
- ``less_than``: The value must be less than the supplied value. | ||||||||||||||||||||||||||
- ``less_than_or_equal_to``: The value must be less than or equal to the supplied value. | ||||||||||||||||||||||||||
- ``other_than``: The value must be different than the supplied value. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
This example defines the following comparison validation rules on the | ||||||||||||||||||||||||||
``Order`` model: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
- ``delivery_date``: Must be after (greater than) the value of ``order_date`` | ||||||||||||||||||||||||||
- ``quantity``: Must be less than ``5`` | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. literalinclude:: /includes/data-modeling/validation.rb | ||||||||||||||||||||||||||
:start-after: start-comparison | ||||||||||||||||||||||||||
:end-before: end-comparison | ||||||||||||||||||||||||||
:language: ruby | ||||||||||||||||||||||||||
:emphasize-lines: 8-9 | ||||||||||||||||||||||||||
:dedent: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
.. _mongoid-format-validation: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Formatting Rule | ||||||||||||||||||||||||||
~~~~~~~~~~~~~~~ | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
You can use the ``format`` helper to validate a document based on | ||||||||||||||||||||||||||
whether a field value matches a regular expression. Specify the regular | ||||||||||||||||||||||||||
expression to the ``with`` option. | ||||||||||||||||||||||||||
|
whether a field value matches a regular expression. Specify the regular | |
expression to the ``with`` option. | |
whether a field value matches a regular expression. Use the ``with`` option to specify the regular expression. |
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.
updated
Outdated
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.
[s] Not sure if the not in
is necessary here, makes it a little hard to parse:
document based on whether a field value is in or not in a specified list | |
of values. Specify the list to the ``in`` option. | |
document based on whether a field value is in a specified list | |
of values. Specify the list to the ``in`` option. |
Outdated
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.
[s] Suggestion for brevity and to use simple present tense per the style guide:
queries a secondary member of the replica set, there is a possibility | |
that it is reading stale data. | |
queries a secondary member of the replica set, it might read stale data. |
Outdated
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.
condition to add when {+odm+} checks for uniqueness: | |
conditions to add when {+odm+} checks for uniqueness: |
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.
[s] Suggestion to avoid using as
per the style guide:
Don't use the ``validates_associated`` helper on both ends of your | |
associations, as this causes {+odm+} to perform validations in an | |
infinite loop. | |
Don't use the ``validates_associated`` helper on both ends of your | |
associations, because this causes {+odm+} to perform validations in an | |
infinite loop. |
or
Don't use the ``validates_associated`` helper on both ends of your | |
associations, as this causes {+odm+} to perform validations in an | |
infinite loop. | |
Don't use the ``validates_associated`` helper on both ends of your | |
associations. This causes {+odm+} to perform validations in an | |
infinite loop. |
Outdated
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.
You can run validations manually by using the ``valid?()`` method.This | |
You can run validations manually by using the ``valid?()`` method. This |
Outdated
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.
[s] For readability, I think "from" is more commonly used and easier to parse:
{+odm+} behaves differently to Active Record when running ``valid?()`` | |
{+odm+} behaves differently from Active Record when running ``valid?()`` |
Outdated
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] I wasn't sure if this sentence is saying that the way Mongoid runs validations is the optimization, or the documents are in memory as optimization? Maybe add a comma for clarity, or omit:
documents that are in memory as an optimization. | |
documents that are in memory, as an optimization. |
documents that are in memory as an optimization. | |
documents that are in memory. |
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.
improved wording
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.
Decided to make this change after considering what was in that page