|
| 1 | +### Configuration |
| 2 | + |
| 3 | +To control the behavior of the library, we have introduced SchemaValidatorsConfig recently. It gives users great flexibility when using the library in different contexts. |
| 4 | + |
| 5 | +For some users, it is just a JSON schema validator implemented mainly based on v4 with some additions from v5 to v7. |
| 6 | + |
| 7 | +For 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 | + |
| 9 | +Most 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 | + |
| 11 | +#### How to use config |
| 12 | + |
| 13 | +When you create a JsonSchema instance from the JsonSchemaFactory, you can pass an object of SchemaValidatorsConfig as the second parameter. |
| 14 | + |
| 15 | +``` |
| 16 | + SchemaValidatorsConfig config = new SchemaValidatorsConfig(); |
| 17 | + config.setTypeLoose(false); |
| 18 | + JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config); |
| 19 | +``` |
| 20 | + |
| 21 | +#### Configurations |
| 22 | + |
| 23 | +* typeLoose |
| 24 | + |
| 25 | +When 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. |
| 26 | + |
| 27 | +* failFast |
| 28 | + |
| 29 | +When 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/JsonSchemaTest.java#L352). |
| 30 | + |
| 31 | +* handleNullableField |
| 32 | + |
| 33 | +When 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. |
| 34 | + |
| 35 | +If handleNullableField is set to true && incoming field is nullable && value is field: null --> succeed |
| 36 | + |
| 37 | +If 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. |
| 38 | + |
| 39 | +The default value is true in the SchemaValidatorsConfig object. |
| 40 | + |
| 41 | +For more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/183). |
| 42 | + |
| 43 | + |
| 44 | +* uriMappings |
| 45 | + |
| 46 | +Map of public, typically internet-accessible schema URLs to alternate locations; this allows for offline validation of schemas that refer to public URLs. This is merged with any mappings the sonSchemaFactory |
| 47 | +may have been built. |
| 48 | + |
| 49 | +The type for this variable is Map<String, String>. |
| 50 | + |
0 commit comments