29
29
30
30
public class SchemaValidatorsConfig {
31
31
/**
32
- * when validate type, if TYPE_LOOSE = true, will try to convert string to different types to match the type defined in
33
- * schema.
32
+ * when validate type, if TYPE_LOOSE = true, will try to convert string to
33
+ * different types to match the type defined in schema.
34
34
*/
35
35
private boolean typeLoose ;
36
36
37
37
/**
38
- * When set to true, validator process is stop immediately when a very first validation error is discovered.
38
+ * When set to true, validator process is stop immediately when a very first
39
+ * validation error is discovered.
39
40
*/
40
41
private boolean failFast ;
41
42
42
43
/**
43
- * When set to true, walker sets nodes that are missing or NullNode to the default value, if any, and mutate the input json.
44
+ * When set to true, walker sets nodes that are missing or NullNode to the
45
+ * default value, if any, and mutate the input json.
44
46
*/
45
47
private ApplyDefaultsStrategy applyDefaultsStrategy ;
46
48
@@ -50,7 +52,8 @@ public class SchemaValidatorsConfig {
50
52
private boolean ecma262Validator ;
51
53
52
54
/**
53
- * When set to true, use Java-specific semantics rather than native JavaScript semantics
55
+ * When set to true, use Java-specific semantics rather than native JavaScript
56
+ * semantics
54
57
*/
55
58
private boolean javaSemantics ;
56
59
@@ -60,40 +63,49 @@ public class SchemaValidatorsConfig {
60
63
private boolean losslessNarrowing ;
61
64
62
65
/**
63
- * When set to true, support for discriminators is enabled for validations of oneOf, anyOf and allOf as described
64
- * on <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject">GitHub</a>.
66
+ * When set to true, support for discriminators is enabled for validations of
67
+ * oneOf, anyOf and allOf as described on <a href=
68
+ * "https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject">GitHub</a>.
65
69
*/
66
70
private boolean openAPI3StyleDiscriminators = false ;
67
71
68
72
/**
69
- * Map of public, normally internet accessible schema URLs to alternate locations; this allows for offline
70
- * validation of schemas that refer to public URLs. This is merged with any mappings the {@link JsonSchemaFactory}
71
- * may have been built with.
73
+ * Map of public, normally internet accessible schema URLs to alternate
74
+ * locations; this allows for offline validation of schemas that refer to public
75
+ * URLs. This is merged with any mappings the {@link JsonSchemaFactory} may have
76
+ * been built with.
72
77
*/
73
78
private Map <String , String > uriMappings = new HashMap <String , String >();
74
79
75
80
private CompositeURITranslator uriTranslators = new CompositeURITranslator ();
76
81
77
82
/**
78
- * When a field is set as nullable in the OpenAPI specification, the schema validator validates that it is nullable
79
- * however continues with validation against the nullable field
83
+ * When a field is set as nullable in the OpenAPI specification, the schema
84
+ * validator validates that it is nullable however continues with validation
85
+ * against the nullable field
80
86
* <p>
81
- * If handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed
82
- * If handleNullableField is set to false && incoming field is nullable && value is field: null --> it is up to the type
83
- * validator using the SchemaValidator to handle it.
87
+ * If handleNullableField is set to true && incoming field is nullable && value
88
+ * is field: null --> succeed If handleNullableField is set to false && incoming
89
+ * field is nullable && value is field: null --> it is up to the type validator
90
+ * using the SchemaValidator to handle it.
84
91
*/
85
92
private boolean handleNullableField = true ;
86
93
87
94
/**
88
- * When set to true resets the {@link CollectorContext} by calling {@link CollectorContext#reset()}.
95
+ * When set to true resets the {@link CollectorContext} by calling
96
+ * {@link CollectorContext#reset()}.
89
97
*/
90
98
private boolean resetCollectorContext = true ;
91
99
100
+ /**
101
+ * When set to true considers that schema is used to write data then ReadOnlyValidator is activated. Default true.
102
+ */
103
+ private boolean writeMode = true ;
104
+
92
105
// This is just a constant for listening to all Keywords.
93
106
public static final String ALL_KEYWORD_WALK_LISTENER_KEY = "com.networknt.AllKeywordWalkListener" ;
94
107
95
- private final Map <String , List <JsonSchemaWalkListener >> keywordWalkListenersMap = new HashMap <String ,
96
- List <JsonSchemaWalkListener >>();
108
+ private final Map <String , List <JsonSchemaWalkListener >> keywordWalkListenersMap = new HashMap <String , List <JsonSchemaWalkListener >>();
97
109
98
110
private final List <JsonSchemaWalkListener > propertyWalkListeners = new ArrayList <JsonSchemaWalkListener >();
99
111
@@ -112,9 +124,11 @@ public void setTypeLoose(boolean typeLoose) {
112
124
}
113
125
114
126
/**
115
- * When enabled, {@link JsonValidator#validate(JsonNode, JsonNode, String)}
116
- * or {@link JsonValidator#validate(JsonNode)} doesn't return any {@link Set}<{@link ValidationMessage}>,
117
- * instead a {@link JsonSchemaException} is thrown as soon as a validation errors is discovered.
127
+ * When enabled, {@link JsonValidator#validate(JsonNode, JsonNode, String)} or
128
+ * {@link JsonValidator#validate(JsonNode)} doesn't return any
129
+ * {@link Set}<{@link ValidationMessage}>, instead a
130
+ * {@link JsonSchemaException} is thrown as soon as a validation errors is
131
+ * discovered.
118
132
*
119
133
* @param failFast boolean
120
134
*/
@@ -267,6 +281,7 @@ public void setLosslessNarrowing(boolean losslessNarrowing) {
267
281
268
282
/**
269
283
* Indicates whether OpenAPI 3 style discriminators should be supported
284
+ *
270
285
* @return true in case discriminators are enabled
271
286
* @since 1.0.51
272
287
*/
@@ -275,26 +290,36 @@ public boolean isOpenAPI3StyleDiscriminators() {
275
290
}
276
291
277
292
/**
278
- * When enabled, the validation of <code>anyOf</code> and <code>allOf</code> in polymorphism will respect
279
- * OpenAPI 3 style discriminators as described in the
280
- * <a href="https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject">OpenAPI 3.0.3 spec</a>.
281
- * The presence of a discriminator configuration on the schema will lead to the following changes in the behavior:
293
+ * When enabled, the validation of <code>anyOf</code> and <code>allOf</code> in
294
+ * polymorphism will respect OpenAPI 3 style discriminators as described in the
295
+ * <a href=
296
+ * "https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject">OpenAPI
297
+ * 3.0.3 spec</a>. The presence of a discriminator configuration on the schema
298
+ * will lead to the following changes in the behavior:
282
299
* <ul>
283
- * <li>for <code>oneOf</code> the spec is unfortunately very vague. Whether <code>oneOf</code> semantics should be
284
- * affected by discriminators or not is not even 100% clear within the members of the OAS steering committee. Therefore
285
- * <code>oneOf</code> at the moment ignores discriminators</li>
286
- * <li>for <code>anyOf</code> the validation will choose one of the candidate schemas for validation based on the
287
- * discriminator property value and will pass validation when this specific schema passes. This is in particular useful
288
- * when the payload could match multiple candidates in the <code>anyOf</code> list and could lead to ambiguity. Example:
289
- * type B has all mandatory properties of A and adds more mandatory ones. Whether the payload is an A or B is determined
290
- * via the discriminator property name. A payload indicating it is an instance of B then requires passing the validation
291
- * of B and passing the validation of A would not be sufficient anymore.</li>
292
- * <li>for <code>allOf</code> use cases with discriminators defined on the copied-in parent type, it is possible to
293
- * automatically validate against a subtype. Example: some schema specifies that there is a field of type A. A carries
294
- * a discriminator field and B inherits from A. Then B is automatically a candidate for validation as well and will be
295
- * chosen in case the discriminator property matches</li>
300
+ * <li>for <code>oneOf</code> the spec is unfortunately very vague. Whether
301
+ * <code>oneOf</code> semantics should be affected by discriminators or not is
302
+ * not even 100% clear within the members of the OAS steering committee.
303
+ * Therefore <code>oneOf</code> at the moment ignores discriminators</li>
304
+ * <li>for <code>anyOf</code> the validation will choose one of the candidate
305
+ * schemas for validation based on the discriminator property value and will
306
+ * pass validation when this specific schema passes. This is in particular
307
+ * useful when the payload could match multiple candidates in the
308
+ * <code>anyOf</code> list and could lead to ambiguity. Example: type B has all
309
+ * mandatory properties of A and adds more mandatory ones. Whether the payload
310
+ * is an A or B is determined via the discriminator property name. A payload
311
+ * indicating it is an instance of B then requires passing the validation of B
312
+ * and passing the validation of A would not be sufficient anymore.</li>
313
+ * <li>for <code>allOf</code> use cases with discriminators defined on the
314
+ * copied-in parent type, it is possible to automatically validate against a
315
+ * subtype. Example: some schema specifies that there is a field of type A. A
316
+ * carries a discriminator field and B inherits from A. Then B is automatically
317
+ * a candidate for validation as well and will be chosen in case the
318
+ * discriminator property matches</li>
296
319
* </ul>
297
- * @param openAPI3StyleDiscriminators whether or not discriminators should be used. Defaults to <code>false</code>
320
+ *
321
+ * @param openAPI3StyleDiscriminators whether or not discriminators should be
322
+ * used. Defaults to <code>false</code>
298
323
* @since 1.0.51
299
324
*/
300
325
public void setOpenAPI3StyleDiscriminators (boolean openAPI3StyleDiscriminators ) {
@@ -316,4 +341,18 @@ public boolean isResetCollectorContext() {
316
341
public void setResetCollectorContext (boolean resetCollectorContext ) {
317
342
this .resetCollectorContext = resetCollectorContext ;
318
343
}
319
- }
344
+
345
+ public boolean isWriteMode () {
346
+ return writeMode ;
347
+ }
348
+
349
+ /**
350
+ *
351
+ * When set to true considers that schema is used to write data then ReadOnlyValidator is activated. Default true.
352
+ *
353
+ * @param writeMode
354
+ */
355
+ public void setWriteMode (boolean writeMode ) {
356
+ this .writeMode = writeMode ;
357
+ }
358
+ }
0 commit comments