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
*/
@@ -147,6 +161,7 @@ public void addUriTranslator(URITranslator uriTranslator) {
147
161
148
162
/**
149
163
* @deprecated Use {@code getUriTranslator()} instead
164
+ * @return Map of public, normally internet accessible schema URLs
150
165
*/
151
166
@ Deprecated
152
167
public Map <String , String > getUriMappings () {
@@ -156,6 +171,7 @@ public Map<String, String> getUriMappings() {
156
171
157
172
/**
158
173
* @deprecated Use {@code addUriTranslator()} instead
174
+ * @param uriMappings Map of public, normally internet accessible schema URLs
159
175
*/
160
176
@ Deprecated
161
177
public void setUriMappings (Map <String , String > uriMappings ) {
@@ -267,6 +283,7 @@ public void setLosslessNarrowing(boolean losslessNarrowing) {
267
283
268
284
/**
269
285
* Indicates whether OpenAPI 3 style discriminators should be supported
286
+ *
270
287
* @return true in case discriminators are enabled
271
288
* @since 1.0.51
272
289
*/
@@ -275,26 +292,36 @@ public boolean isOpenAPI3StyleDiscriminators() {
275
292
}
276
293
277
294
/**
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:
295
+ * When enabled, the validation of <code>anyOf</code> and <code>allOf</code> in
296
+ * polymorphism will respect OpenAPI 3 style discriminators as described in the
297
+ * <a href=
298
+ * "https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#discriminatorObject">OpenAPI
299
+ * 3.0.3 spec</a>. The presence of a discriminator configuration on the schema
300
+ * will lead to the following changes in the behavior:
282
301
* <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>
302
+ * <li>for <code>oneOf</code> the spec is unfortunately very vague. Whether
303
+ * <code>oneOf</code> semantics should be affected by discriminators or not is
304
+ * not even 100% clear within the members of the OAS steering committee.
305
+ * Therefore <code>oneOf</code> at the moment ignores discriminators</li>
306
+ * <li>for <code>anyOf</code> the validation will choose one of the candidate
307
+ * schemas for validation based on the discriminator property value and will
308
+ * pass validation when this specific schema passes. This is in particular
309
+ * useful when the payload could match multiple candidates in the
310
+ * <code>anyOf</code> list and could lead to ambiguity. Example: type B has all
311
+ * mandatory properties of A and adds more mandatory ones. Whether the payload
312
+ * is an A or B is determined via the discriminator property name. A payload
313
+ * indicating it is an instance of B then requires passing the validation of B
314
+ * and passing the validation of A would not be sufficient anymore.</li>
315
+ * <li>for <code>allOf</code> use cases with discriminators defined on the
316
+ * copied-in parent type, it is possible to automatically validate against a
317
+ * subtype. Example: some schema specifies that there is a field of type A. A
318
+ * carries a discriminator field and B inherits from A. Then B is automatically
319
+ * a candidate for validation as well and will be chosen in case the
320
+ * discriminator property matches</li>
296
321
* </ul>
297
- * @param openAPI3StyleDiscriminators whether or not discriminators should be used. Defaults to <code>false</code>
322
+ *
323
+ * @param openAPI3StyleDiscriminators whether or not discriminators should be
324
+ * used. Defaults to <code>false</code>
298
325
* @since 1.0.51
299
326
*/
300
327
public void setOpenAPI3StyleDiscriminators (boolean openAPI3StyleDiscriminators ) {
@@ -316,4 +343,18 @@ public boolean isResetCollectorContext() {
316
343
public void setResetCollectorContext (boolean resetCollectorContext ) {
317
344
this .resetCollectorContext = resetCollectorContext ;
318
345
}
319
- }
346
+
347
+ public boolean isWriteMode () {
348
+ return writeMode ;
349
+ }
350
+
351
+ /**
352
+ *
353
+ * When set to true considers that schema is used to write data then ReadOnlyValidator is activated. Default true.
354
+ *
355
+ * @param writeMode true if schema is used to write data
356
+ */
357
+ public void setWriteMode (boolean writeMode ) {
358
+ this .writeMode = writeMode ;
359
+ }
360
+ }
0 commit comments