21
21
22
22
import org .springframework .data .mongodb .core .query .Collation ;
23
23
import org .springframework .data .mongodb .core .schema .MongoJsonSchema ;
24
- import org .springframework .data .mongodb .core .validation .ValidatorDefinition ;
24
+ import org .springframework .data .mongodb .core .validation .Validator ;
25
25
import org .springframework .data .util .Optionals ;
26
26
import org .springframework .lang .Nullable ;
27
27
import org .springframework .util .Assert ;
@@ -43,7 +43,7 @@ public class CollectionOptions {
43
43
private @ Nullable Long size ;
44
44
private @ Nullable Boolean capped ;
45
45
private @ Nullable Collation collation ;
46
- private Validator validator ;
46
+ private ValidationOptions validationOptions ;
47
47
48
48
/**
49
49
* Constructs a new <code>CollectionOptions</code> instance.
@@ -56,17 +56,17 @@ public class CollectionOptions {
56
56
*/
57
57
@ Deprecated
58
58
public CollectionOptions (@ Nullable Long size , @ Nullable Long maxDocuments , @ Nullable Boolean capped ) {
59
- this (size , maxDocuments , capped , null , Validator .none ());
59
+ this (size , maxDocuments , capped , null , ValidationOptions .none ());
60
60
}
61
61
62
62
private CollectionOptions (@ Nullable Long size , @ Nullable Long maxDocuments , @ Nullable Boolean capped ,
63
- @ Nullable Collation collation , Validator validator ) {
63
+ @ Nullable Collation collation , ValidationOptions validationOptions ) {
64
64
65
65
this .maxDocuments = maxDocuments ;
66
66
this .size = size ;
67
67
this .capped = capped ;
68
68
this .collation = collation ;
69
- this .validator = validator ;
69
+ this .validationOptions = validationOptions ;
70
70
}
71
71
72
72
/**
@@ -80,7 +80,7 @@ public static CollectionOptions just(Collation collation) {
80
80
81
81
Assert .notNull (collation , "Collation must not be null!" );
82
82
83
- return new CollectionOptions (null , null , null , collation , Validator .none ());
83
+ return new CollectionOptions (null , null , null , collation , ValidationOptions .none ());
84
84
}
85
85
86
86
/**
@@ -90,7 +90,7 @@ public static CollectionOptions just(Collation collation) {
90
90
* @since 2.0
91
91
*/
92
92
public static CollectionOptions empty () {
93
- return new CollectionOptions (null , null , null , null , Validator .none ());
93
+ return new CollectionOptions (null , null , null , null , ValidationOptions .none ());
94
94
}
95
95
96
96
/**
@@ -101,7 +101,7 @@ public static CollectionOptions empty() {
101
101
* @since 2.0
102
102
*/
103
103
public CollectionOptions capped () {
104
- return new CollectionOptions (size , maxDocuments , true , collation , validator );
104
+ return new CollectionOptions (size , maxDocuments , true , collation , validationOptions );
105
105
}
106
106
107
107
/**
@@ -112,7 +112,7 @@ public CollectionOptions capped() {
112
112
* @since 2.0
113
113
*/
114
114
public CollectionOptions maxDocuments (long maxDocuments ) {
115
- return new CollectionOptions (size , maxDocuments , capped , collation , validator );
115
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
116
116
}
117
117
118
118
/**
@@ -123,7 +123,7 @@ public CollectionOptions maxDocuments(long maxDocuments) {
123
123
* @since 2.0
124
124
*/
125
125
public CollectionOptions size (long size ) {
126
- return new CollectionOptions (size , maxDocuments , capped , collation , validator );
126
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
127
127
}
128
128
129
129
/**
@@ -134,23 +134,31 @@ public CollectionOptions size(long size) {
134
134
* @since 2.0
135
135
*/
136
136
public CollectionOptions collation (@ Nullable Collation collation ) {
137
- return new CollectionOptions (size , maxDocuments , capped , collation , validator );
137
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
138
138
}
139
139
140
140
/**
141
- * Create new {@link CollectionOptions} with already given settings and {@code validator } set to given
141
+ * Create new {@link CollectionOptions} with already given settings and {@code validationOptions } set to given
142
142
* {@link MongoJsonSchema}.
143
143
*
144
144
* @param schema can be {@literal null}.
145
145
* @return new {@link CollectionOptions}.
146
146
* @since 2.1
147
147
*/
148
148
public CollectionOptions schema (@ Nullable MongoJsonSchema schema ) {
149
- return validation ( new Validator (schema , null , validator . validationLevel , validator . validationAction ));
149
+ return validator ( Validator . schema (schema ));
150
150
}
151
151
152
- public CollectionOptions validatorDefinition (@ Nullable ValidatorDefinition definition ) {
153
- return validation (new Validator (null , definition , validator .validationLevel , validator .validationAction ));
152
+ /**
153
+ * /** Create new {@link CollectionOptions} with already given settings and {@code validationOptions} set to given
154
+ * {@link Validator}.
155
+ *
156
+ * @param validator can be {@literal null}.
157
+ * @return new {@link CollectionOptions}.
158
+ * @since 2.1
159
+ */
160
+ public CollectionOptions validator (@ Nullable Validator validator ) {
161
+ return validation (validationOptions .validator (validator ));
154
162
}
155
163
156
164
/**
@@ -219,7 +227,7 @@ public CollectionOptions failOnValidationError() {
219
227
public CollectionOptions schemaValidationLevel (ValidationLevel validationLevel ) {
220
228
221
229
Assert .notNull (validationLevel , "ValidationLevel must not be null!" );
222
- return validation (new Validator ( validator . schema , validator . validatorDefinition , validationLevel , validator . validationAction ));
230
+ return validation (validationOptions . validationLevel ( validationLevel ));
223
231
}
224
232
225
233
/**
@@ -233,20 +241,20 @@ public CollectionOptions schemaValidationLevel(ValidationLevel validationLevel)
233
241
public CollectionOptions schemaValidationAction (ValidationAction validationAction ) {
234
242
235
243
Assert .notNull (validationAction , "ValidationAction must not be null!" );
236
- return validation (new Validator ( validator . schema , validator . validatorDefinition , validator . validationLevel , validationAction ));
244
+ return validation (validationOptions . validationAction ( validationAction ));
237
245
}
238
246
239
247
/**
240
- * Create new {@link CollectionOptions} with the given {@link Validator }.
248
+ * Create new {@link CollectionOptions} with the given {@link ValidationOptions }.
241
249
*
242
- * @param validator must not be {@literal null}. Use {@link Validator #none()} to remove validation.
250
+ * @param validationOptions must not be {@literal null}. Use {@link ValidationOptions #none()} to remove validation.
243
251
* @return new {@link CollectionOptions}.
244
252
* @since 2.1
245
253
*/
246
- public CollectionOptions validation (Validator validator ) {
254
+ public CollectionOptions validation (ValidationOptions validationOptions ) {
247
255
248
- Assert .notNull (validator , "Validator must not be null!" );
249
- return new CollectionOptions (size , maxDocuments , capped , collation , validator );
256
+ Assert .notNull (validationOptions , "ValidationOptions must not be null!" );
257
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
250
258
}
251
259
252
260
/**
@@ -293,47 +301,72 @@ public Optional<Collation> getCollation() {
293
301
* @return {@link Optional#empty()} if not set.
294
302
* @since 2.1
295
303
*/
296
- public Optional <Validator > getValidator () {
297
- return validator .isEmpty () ? Optional .empty () : Optional .of (validator );
304
+ public Optional <ValidationOptions > getValidationOptions () {
305
+ return validationOptions .isEmpty () ? Optional .empty () : Optional .of (validationOptions );
298
306
}
299
307
300
308
/**
301
- * Encapsulation of Validator options.
309
+ * Encapsulation of ValidationOptions options.
302
310
*
303
311
* @author Christoph Strobl
304
312
* @author Andreas Zink
305
313
* @since 2.1
306
314
*/
307
315
@ RequiredArgsConstructor
308
- public static class Validator {
316
+ public static class ValidationOptions {
309
317
310
- private static final Validator NONE = new Validator ( null , null , null , null );
318
+ private static final ValidationOptions NONE = new ValidationOptions ( null , null , null );
311
319
312
- private final @ Nullable MongoJsonSchema schema ;
313
- private final @ Nullable ValidatorDefinition validatorDefinition ;
320
+ private final @ Nullable Validator validator ;
314
321
private final @ Nullable ValidationLevel validationLevel ;
315
322
private final @ Nullable ValidationAction validationAction ;
316
323
317
324
/**
318
- * Create an empty {@link Validator }.
325
+ * Create an empty {@link ValidationOptions }.
319
326
*
320
327
* @return never {@literal null}.
321
328
*/
322
- public static Validator none () {
329
+ public static ValidationOptions none () {
323
330
return NONE ;
324
331
}
325
332
326
333
/**
327
- * Get the {@code $jsonSchema} used for validation.
334
+ * Define the {@link Validator} to be used for document validation.
328
335
*
329
- * @return {@link Optional#empty()} if not set.
336
+ * @param validator can be {@literal null}.
337
+ * @return new instance of {@link ValidationOptions}.
338
+ */
339
+ public ValidationOptions validator (@ Nullable Validator validator ) {
340
+ return new ValidationOptions (validator , validationLevel , validationAction );
341
+ }
342
+
343
+ /**
344
+ * Define the validation level to apply.
345
+ *
346
+ * @param validationLevel can be {@literal null}.
347
+ * @return new instance of {@link ValidationOptions}.
348
+ */
349
+ public ValidationOptions validationLevel (ValidationLevel validationLevel ) {
350
+ return new ValidationOptions (validator , validationLevel , validationAction );
351
+ }
352
+
353
+ /**
354
+ * Define the validation action to take.
355
+ *
356
+ * @param validationAction can be {@literal null}.
357
+ * @return new instance of {@link ValidationOptions}.
330
358
*/
331
- public Optional < MongoJsonSchema > getSchema ( ) {
332
- return Optional . ofNullable ( schema );
359
+ public ValidationOptions validationAction ( ValidationAction validationAction ) {
360
+ return new ValidationOptions ( validator , validationLevel , validationAction );
333
361
}
334
362
335
- public Optional <ValidatorDefinition > getValidatorDefinition () {
336
- return Optional .ofNullable (validatorDefinition );
363
+ /**
364
+ * Get the {@link Validator} to use.
365
+ *
366
+ * @return never {@literal null}.
367
+ */
368
+ public Optional <Validator > getValidator () {
369
+ return Optional .ofNullable (validator );
337
370
}
338
371
339
372
/**
@@ -358,7 +391,7 @@ public Optional<ValidationAction> getValidationAction() {
358
391
* @return {@literal true} if no arguments set.
359
392
*/
360
393
boolean isEmpty () {
361
- return !Optionals .isAnyPresent (getSchema (), getValidatorDefinition (), getValidationAction (), getValidationLevel ());
394
+ return !Optionals .isAnyPresent (getValidator (), getValidationAction (), getValidationLevel ());
362
395
}
363
396
}
364
397
}
0 commit comments