|
1 | 1 | Validation of Documents
|
2 | 2 | =======================
|
3 | 3 |
|
| 4 | +Validation of Documents - Application Side |
| 5 | +------------------------------------------ |
| 6 | + |
4 | 7 | .. sectionauthor:: Benjamin Eberlei <[email protected]>
|
5 | 8 |
|
6 | 9 | Doctrine does not ship with any internal validators, the reason
|
@@ -127,4 +130,124 @@ instances. This was already discussed in the previous blog post on
|
127 | 130 | the Versionable extension, which requires another type of event
|
128 | 131 | called "onFlush".
|
129 | 132 |
|
130 |
| -Further readings: :doc:`Lifecycle Events <../reference/events>` |
| 133 | +Further readings: :doc:`Lifecycle Events <../reference/events>` |
| 134 | + |
| 135 | +Validation of Documents - Database Side |
| 136 | +--------------------------------------- |
| 137 | + |
| 138 | +.. sectionauthor:: Alexandre Abrioux <[email protected]> |
| 139 | + |
| 140 | +.. note:: |
| 141 | + |
| 142 | + This feature has been introduced in version 2.3.0 |
| 143 | + |
| 144 | +MongoDB ≥ 3.6 offers the capability to validate documents during |
| 145 | +insertions and updates through a schema associated to the collection |
| 146 | +(cf. `MongoDB documentation <https://docs.mongodb.com/manual/core/schema-validation/>`_). |
| 147 | + |
| 148 | +Doctrine MongoDB ODM now provides a way to take advantage of this functionality |
| 149 | +thanks to the new :doc:`@Validation <../reference/annotations-reference#validation>` |
| 150 | +annotation and its properties (also available with XML mapping): |
| 151 | + |
| 152 | +- |
| 153 | + ``validator`` - The schema that will be used to validate documents. |
| 154 | + It is a string representing a BSON document under the |
| 155 | + `Extended JSON specification <https://github.com/mongodb/specifications/blob/master/source/extended-json.rst>`_. |
| 156 | +- |
| 157 | + ``action`` - The behavior followed by MongoDB to handle documents that |
| 158 | + violate the validation rules. |
| 159 | +- |
| 160 | + ``level`` - The threshold used by MongoDB to filter operations that |
| 161 | + will get validated. |
| 162 | + |
| 163 | +Once defined, those options will be added to the collection after running |
| 164 | +the ``odm:schema:create`` or ``odm:schema:update`` command. |
| 165 | + |
| 166 | +.. configuration-block:: |
| 167 | + |
| 168 | + .. code-block:: php |
| 169 | +
|
| 170 | + <?php |
| 171 | +
|
| 172 | + namespace Documents; |
| 173 | +
|
| 174 | + use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
| 175 | + use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
| 176 | +
|
| 177 | + /** |
| 178 | + * @ODM\Document |
| 179 | + * @ODM\Validation( |
| 180 | + * validator=SchemaValidated::VALIDATOR, |
| 181 | + * action=ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN, |
| 182 | + * level=ClassMetadata::SCHEMA_VALIDATION_LEVEL_MODERATE, |
| 183 | + * ) |
| 184 | + */ |
| 185 | + class SchemaValidated |
| 186 | + { |
| 187 | + public const VALIDATOR = <<<'EOT' |
| 188 | + { |
| 189 | + "$jsonSchema": { |
| 190 | + "required": ["name"], |
| 191 | + "properties": { |
| 192 | + "name": { |
| 193 | + "bsonType": "string", |
| 194 | + "description": "must be a string and is required" |
| 195 | + } |
| 196 | + } |
| 197 | + }, |
| 198 | + "$or": [ |
| 199 | + { "phone": { "$type": "string" } }, |
| 200 | + { "email": { "$regex": { "$regularExpression" : { "pattern": "@mongodb\\.com$", "options": "" } } } }, |
| 201 | + { "status": { "$in": [ "Unknown", "Incomplete" ] } } |
| 202 | + ] |
| 203 | + } |
| 204 | + EOT; |
| 205 | +
|
| 206 | + /** @ODM\Id */ |
| 207 | + private $id; |
| 208 | +
|
| 209 | + /** @ODM\Field(type="string") */ |
| 210 | + private $name; |
| 211 | +
|
| 212 | + /** @ODM\Field(type="string") */ |
| 213 | + private $phone; |
| 214 | +
|
| 215 | + /** @ODM\Field(type="string") */ |
| 216 | + private $email; |
| 217 | +
|
| 218 | + /** @ODM\Field(type="string") */ |
| 219 | + private $status; |
| 220 | + } |
| 221 | +
|
| 222 | + .. code-block:: xml |
| 223 | +
|
| 224 | + <?xml version="1.0" encoding="UTF-8"?> |
| 225 | + <doctrine-mongo-mapping xmlns="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping" |
| 226 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 227 | + xsi:schemaLocation="http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping |
| 228 | + http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd"> |
| 229 | +
|
| 230 | + <document name="SchemaValidated"> |
| 231 | + <schema-validation action="warn" level="moderate"> |
| 232 | + { |
| 233 | + "$jsonSchema": { |
| 234 | + "required": ["name"], |
| 235 | + "properties": { |
| 236 | + "name": { |
| 237 | + "bsonType": "string", |
| 238 | + "description": "must be a string and is required" |
| 239 | + } |
| 240 | + } |
| 241 | + }, |
| 242 | + "$or": [ |
| 243 | + { "phone": { "$type": "string" } }, |
| 244 | + { "email": { "$regex": { "$regularExpression" : { "pattern": "@mongodb\\.com$", "options": "" } } } }, |
| 245 | + { "status": { "$in": [ "Unknown", "Incomplete" ] } } |
| 246 | + ] |
| 247 | + } |
| 248 | + </schema-validation> |
| 249 | + </document> |
| 250 | + </doctrine-mongo-mapping> |
| 251 | +
|
| 252 | +Please refer to the :doc:`@Validation <../reference/annotations-reference#document>` annotation reference |
| 253 | +for more details on how to use this feature. |
0 commit comments