xref: /aosp_15_r20/external/json-schema-validator/doc/config.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1*78c4dd6aSAndroid Build Coastguard Worker### Configuration
2*78c4dd6aSAndroid Build Coastguard Worker
3*78c4dd6aSAndroid Build Coastguard WorkerTo control the behavior of the library, we have introduced SchemaValidatorsConfig recently. It gives users great flexibility when using the library in different contexts.
4*78c4dd6aSAndroid Build Coastguard Worker
5*78c4dd6aSAndroid Build Coastguard WorkerFor some users, it is just a JSON schema validator implemented mainly based on v4 with some additions from v5 to v7.
6*78c4dd6aSAndroid Build Coastguard Worker
7*78c4dd6aSAndroid Build Coastguard WorkerFor others, it is used as a critical component in the REST API frameworks to validate the request or response. The library was developed as part of the [light-4j](https://github.com/networknt/light-4j) framework in the beginning.
8*78c4dd6aSAndroid Build Coastguard Worker
9*78c4dd6aSAndroid Build Coastguard WorkerMost of the configuration flags are used to control the difference between Swagger/OpenAPI specification and JSON schema specification as they are not the same. The future of the OpenAPI version might resolve this problem, but the release date is not set yet.
10*78c4dd6aSAndroid Build Coastguard Worker
11*78c4dd6aSAndroid Build Coastguard Worker#### How to use config
12*78c4dd6aSAndroid Build Coastguard Worker
13*78c4dd6aSAndroid Build Coastguard WorkerWhen you create a `JsonSchema` instance from the `JsonSchemaFactory`, you can pass an object of SchemaValidatorsConfig as the second parameter.
14*78c4dd6aSAndroid Build Coastguard Worker
15*78c4dd6aSAndroid Build Coastguard Worker```java
16*78c4dd6aSAndroid Build Coastguard WorkerSchemaValidatorsConfig config = new SchemaValidatorsConfig();
17*78c4dd6aSAndroid Build Coastguard Workerconfig.setTypeLoose(false);
18*78c4dd6aSAndroid Build Coastguard WorkerJsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
19*78c4dd6aSAndroid Build Coastguard Worker```
20*78c4dd6aSAndroid Build Coastguard Worker
21*78c4dd6aSAndroid Build Coastguard Worker#### Configurations
22*78c4dd6aSAndroid Build Coastguard Worker
23*78c4dd6aSAndroid Build Coastguard Worker* typeLoose
24*78c4dd6aSAndroid Build Coastguard Worker
25*78c4dd6aSAndroid Build Coastguard WorkerWhen typeLoose is true, the validator will convert strings to different types to match the type defined in the schema. This is mostly used to validate the JSON request or response for headers, query parameters, path parameters, and cookies. For the HTTP protocol, these are all strings and might be defined as other types in the schema. For example, the page number might be an integer in the schema but passed as a query parameter in string. When it comes to validating arrays note that any item can also be interpreted as a size 1 array of that item so the item will be validated against the type defined for the array.
26*78c4dd6aSAndroid Build Coastguard Worker
27*78c4dd6aSAndroid Build Coastguard Worker* strictness
28*78c4dd6aSAndroid Build Coastguard WorkerThis is a map of keywords to whether the keyword's validators should perform a strict or permissive analysis. When strict is true, validators will perform strict checking against the schema.
29*78c4dd6aSAndroid Build Coastguard WorkerThis is the default behavior. When set to false, validators are free to relax some constraints but not required. Each validator has its own understanding of what constitutes strict and
30*78c4dd6aSAndroid Build Coastguard Workerpermissive.
31*78c4dd6aSAndroid Build Coastguard Worker
32*78c4dd6aSAndroid Build Coastguard Worker* failFast
33*78c4dd6aSAndroid Build Coastguard Worker
34*78c4dd6aSAndroid Build Coastguard WorkerWhen set to true, the validation process stops immediately when the first error occurs. This mostly used on microservices that is designed to [fail-fast](https://www.networknt.com/architecture/fail-fast/), or users don't want to see hundreds of errors for a big payload. Please be aware that the validator throws an exception in the case the first error occurs. To learn how to use it, please follow the [test case](https://github.com/networknt/json-schema-validator/blob/master/src/test/java/com/networknt/schema/V4JsonSchemaTest.java#L352).
35*78c4dd6aSAndroid Build Coastguard Worker
36*78c4dd6aSAndroid Build Coastguard Worker* handleNullableField
37*78c4dd6aSAndroid Build Coastguard Worker
38*78c4dd6aSAndroid Build Coastguard WorkerWhen a field is set as nullable in the OpenAPI specification, the schema validator validates that it is nullable; however, it continues with validation against the nullable field.
39*78c4dd6aSAndroid Build Coastguard Worker
40*78c4dd6aSAndroid Build Coastguard WorkerIf handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed
41*78c4dd6aSAndroid Build Coastguard Worker
42*78c4dd6aSAndroid Build Coastguard WorkerIf handleNullableField is set to false && incoming field is nullable && value is field: null --> it is up to the type validator using the SchemaValidator to handle it.
43*78c4dd6aSAndroid Build Coastguard Worker
44*78c4dd6aSAndroid Build Coastguard WorkerThe default value is true in the SchemaValidatorsConfig object.
45*78c4dd6aSAndroid Build Coastguard Worker
46*78c4dd6aSAndroid Build Coastguard WorkerFor more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/183).
47*78c4dd6aSAndroid Build Coastguard Worker
48*78c4dd6aSAndroid Build Coastguard Worker* javaSemantics
49*78c4dd6aSAndroid Build Coastguard Worker
50*78c4dd6aSAndroid Build Coastguard WorkerWhen set to true, use Java-specific semantics rather than native JavaScript semantics.
51*78c4dd6aSAndroid Build Coastguard Worker
52*78c4dd6aSAndroid Build Coastguard WorkerFor example, if the node type is `number` per JS semantics where the value can be losslesly interpreted as `java.lang.Long`, the validator would use `integer` as the node type instead of `number`. This is useful when schema type is `integer`, since validation would fail otherwise.
53*78c4dd6aSAndroid Build Coastguard Worker
54*78c4dd6aSAndroid Build Coastguard WorkerFor more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/334).
55*78c4dd6aSAndroid Build Coastguard Worker
56*78c4dd6aSAndroid Build Coastguard Worker* losslessNarrowing
57*78c4dd6aSAndroid Build Coastguard Worker
58*78c4dd6aSAndroid Build Coastguard WorkerWhen set to true, can interpret round doubles as integers.
59*78c4dd6aSAndroid Build Coastguard Worker
60*78c4dd6aSAndroid Build Coastguard WorkerNote that setting `javaSemantics = true` will achieve the same functionality at this time.
61*78c4dd6aSAndroid Build Coastguard Worker
62*78c4dd6aSAndroid Build Coastguard WorkerFor more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/344).
63*78c4dd6aSAndroid Build Coastguard Worker
64*78c4dd6aSAndroid Build Coastguard Worker* pathType
65*78c4dd6aSAndroid Build Coastguard Worker
66*78c4dd6aSAndroid Build Coastguard WorkerThis defines how path expressions are defined and returned once validation is performed through `ValidationMessage` instances. This can either be set to `PathType.JSON_POINTER` for [JSONPointer](https://www.rfc-editor.org/rfc/rfc6901.html) expressions,
67*78c4dd6aSAndroid Build Coastguard Workeror to `PathType.JSON_PATH` for [JSONPath](https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/) expressions. Doing so allows you to report the path for each finding and to potentially lookup nodes
68*78c4dd6aSAndroid Build Coastguard Worker(see [here](https://github.com/networknt/json-schema-validator/blob/c41df270a71f8423c63cfaa379d2e9b3f570b73e/doc/yaml-line-numbers.md#scenario-2---validationmessage-line-locations) for an example). By default, path expressions use a
69*78c4dd6aSAndroid Build Coastguard Worker`PathType.LEGACY` format which is close to JSONPath but does not escape reserved characters.
70