|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package example.springdata.mongodb.schema; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.springframework.data.mongodb.core.query.Criteria.*; |
| 20 | +import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.*; |
| 21 | + |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.dao.DataIntegrityViolationException; |
| 28 | +import org.springframework.data.mongodb.core.CollectionOptions; |
| 29 | +import org.springframework.data.mongodb.core.MongoOperations; |
| 30 | +import org.springframework.data.mongodb.core.schema.MongoJsonSchema; |
| 31 | +import org.springframework.data.mongodb.core.validation.Validator; |
| 32 | +import org.springframework.test.context.junit4.SpringRunner; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Christoph Strobl |
| 36 | + */ |
| 37 | +@RunWith(SpringRunner.class) |
| 38 | +@SpringBootTest |
| 39 | +public class DocumentValidation { |
| 40 | + |
| 41 | + static final String COLLECTION = "star-wars"; |
| 42 | + |
| 43 | + @Autowired MongoOperations mongoOps; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() { |
| 47 | + mongoOps.dropCollection(COLLECTION); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * MongoDB (as of version 3.2) supports validating documents against a given structure described by a query. The |
| 52 | + * structure can be built from {@link org.springframework.data.mongodb.core.query.Criteria} objects in the same way as |
| 53 | + * they are used for defining queries. |
| 54 | + * |
| 55 | + * <pre> |
| 56 | + * <code> |
| 57 | + * { |
| 58 | + * name : { |
| 59 | + * $exists : true, |
| 60 | + * $ne : null, |
| 61 | + * $type : 2 |
| 62 | + * }, |
| 63 | + * age : { |
| 64 | + * $exists : true, |
| 65 | + * $ne : null, |
| 66 | + * $type : 16, |
| 67 | + * $gte : 0, |
| 68 | + * $lte : 125 |
| 69 | + * } |
| 70 | + * } |
| 71 | + * </code> |
| 72 | + * </pre> |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void criteriaValidator() { |
| 76 | + |
| 77 | + Validator validator = Validator.criteria( // |
| 78 | + where("name").exists(true).ne(null).type(2) // non null String |
| 79 | + .and("age").exists(true).ne(null).type(16).gte(0).lte(125)) // non null int between 0 and 125 |
| 80 | + ; |
| 81 | + |
| 82 | + mongoOps.createCollection(Jedi.class, CollectionOptions.empty().validator(validator)); |
| 83 | + |
| 84 | + assertThat(mongoOps.save(new Jedi("luke", "luke", "skywalker", 25))).isNotNull(); |
| 85 | + |
| 86 | + assertThatExceptionOfType(DataIntegrityViolationException.class) |
| 87 | + .isThrownBy(() -> mongoOps.save(new Jedi("yoda", "yoda", null, 900))); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * As of version 3.6, MongoDB supports collections that validate documents against a provided JSON Schema that |
| 92 | + * complies to the JSON schema specification (draft 4). |
| 93 | + * |
| 94 | + * <pre> |
| 95 | + * <code> |
| 96 | + * { |
| 97 | + * "type": "object", |
| 98 | + * "required": [ "name", "age" ], |
| 99 | + * "properties": { |
| 100 | + * "name": { |
| 101 | + * "type": "string", |
| 102 | + * "minLength": 1 |
| 103 | + * }, |
| 104 | + * "age": { |
| 105 | + * "type": "int", |
| 106 | + * "minimum" : 0, |
| 107 | + * "exclusiveMinimum" : false, |
| 108 | + * "maximum" : 125, |
| 109 | + * "exclusiveMaximum" : false |
| 110 | + * } |
| 111 | + * } |
| 112 | + * } |
| 113 | + * </code> |
| 114 | + * </pre> |
| 115 | + */ |
| 116 | + @Test |
| 117 | + public void schemaValidator() { |
| 118 | + |
| 119 | + Validator validator = Validator.schema(MongoJsonSchema.builder() // |
| 120 | + .required("name", "age") // |
| 121 | + .properties( // |
| 122 | + string("name").minLength(1), // |
| 123 | + int32("age").gte(0).lte(125) // |
| 124 | + ).build()); |
| 125 | + mongoOps.createCollection(Jedi.class, CollectionOptions.empty().validator(validator)); |
| 126 | + |
| 127 | + assertThat(mongoOps.save(new Jedi("luke", "luke", "skywalker", 25))).isNotNull(); |
| 128 | + |
| 129 | + assertThatExceptionOfType(DataIntegrityViolationException.class) |
| 130 | + .isThrownBy(() -> mongoOps.save(new Jedi("yoda", "yoda", null, 900))); |
| 131 | + } |
| 132 | +} |
0 commit comments