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