|
1 | 1 | ## Quick Start
|
2 | 2 |
|
3 |
| -To use the validator, we need to have both the `JsonSchema` object and `JsonNode` object constructed. |
4 |
| -There are many ways to do that. |
5 |
| -Here is base test class, that shows several ways to construct these from `String`, `Stream`, `Url`, and `JsonNode`. |
6 |
| -Please pay attention to the `JsonSchemaFactory` class as it is the way to construct the `JsonSchema` object. |
| 3 | +To use the validator, we need to have the `JsonSchema` loaded and cached. |
7 | 4 |
|
8 |
| -```java |
9 |
| -public class BaseJsonSchemaValidatorTest { |
| 5 | +For simplicity the following test loads a schema from a `String` or `JsonNode`. Note that loading a schema in this manner is not recommended as a relative `$ref` will not be properly resolved as there is no base IRI. |
10 | 6 |
|
11 |
| - private ObjectMapper mapper = new ObjectMapper(); |
| 7 | +The preferred method of loading a schema is by using a `SchemaLocation` and by configuring the appropriate `SchemaMapper` and `SchemaLoader` on the `JsonSchemaFactory`. |
12 | 8 |
|
13 |
| - protected JsonNode getJsonNodeFromClasspath(String name) throws IOException { |
14 |
| - InputStream is1 = Thread.currentThread().getContextClassLoader() |
15 |
| - .getResourceAsStream(name); |
16 |
| - return mapper.readTree(is1); |
17 |
| - } |
| 9 | +```java |
| 10 | +package com.example; |
18 | 11 |
|
19 |
| - protected JsonNode getJsonNodeFromStringContent(String content) throws IOException { |
20 |
| - return mapper.readTree(content); |
21 |
| - } |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
22 | 13 |
|
23 |
| - protected JsonNode getJsonNodeFromUrl(String url) throws IOException { |
24 |
| - return mapper.readTree(new URL(url)); |
25 |
| - } |
| 14 | +import java.util.Set; |
26 | 15 |
|
27 |
| - protected JsonSchema getJsonSchemaFromClasspath(String name) { |
28 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
29 |
| - InputStream is = Thread.currentThread().getContextClassLoader() |
30 |
| - .getResourceAsStream(name); |
31 |
| - return factory.getSchema(is); |
32 |
| - } |
| 16 | +import org.junit.jupiter.api.Test; |
33 | 17 |
|
34 |
| - protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) { |
35 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
36 |
| - return factory.getSchema(schemaContent); |
37 |
| - } |
| 18 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | +import com.fasterxml.jackson.databind.JsonMappingException; |
| 20 | +import com.fasterxml.jackson.databind.JsonNode; |
| 21 | +import com.networknt.schema.*; |
| 22 | +import com.networknt.schema.serialization.JsonMapperFactory; |
38 | 23 |
|
39 |
| - protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException { |
| 24 | +public class SampleTest { |
| 25 | + @Test |
| 26 | + void schemaFromString() throws JsonMappingException, JsonProcessingException { |
40 | 27 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
41 |
| - return factory.getSchema(SchemaLocation.of(uri)); |
| 28 | + /* |
| 29 | + * This should be cached for performance. |
| 30 | + * |
| 31 | + * Loading from a String is not recommended as there is no base IRI to use for |
| 32 | + * resolving relative $ref. |
| 33 | + */ |
| 34 | + JsonSchema schemaFromString = factory |
| 35 | + .getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); |
| 36 | + Set<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON); |
| 37 | + assertEquals(1, errors.size()); |
42 | 38 | }
|
43 | 39 |
|
44 |
| - protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) { |
| 40 | + @Test |
| 41 | + void schemaFromJsonNode() throws JsonMappingException, JsonProcessingException { |
45 | 42 | JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
|
46 |
| - return factory.getSchema(jsonNode); |
47 |
| - } |
48 |
| - |
49 |
| - // Automatically detect version for given JsonNode |
50 |
| - protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) { |
51 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode)); |
52 |
| - return factory.getSchema(jsonNode); |
53 |
| - } |
54 |
| - |
55 |
| -} |
56 |
| -``` |
57 |
| -And the following is one of the test cases in one of the test classes that extend from the above base class. As you can see, it constructs `JsonSchema` and `JsonNode` from `String`. |
58 |
| - |
59 |
| -```java |
60 |
| -class Sample extends BaseJsonSchemaValidatorTest { |
61 |
| - |
62 |
| - void test() { |
63 |
| - JsonSchema schema = getJsonSchemaFromStringContent("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); |
64 |
| - JsonNode node = getJsonNodeFromStringContent("7"); |
65 |
| - Set<ValidationMessage> errors = schema.validate(node); |
66 |
| - assertThat(errors.size(), is(1)); |
67 |
| - |
68 |
| - // With automatic version detection |
69 |
| - JsonNode schemaNode = getJsonNodeFromStringContent( |
70 |
| - "{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}"); |
71 |
| - JsonSchema schema = getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode); |
72 |
| - |
73 |
| - schema.initializeValidators(); // by default all schemas are loaded lazily. You can load them eagerly via |
74 |
| - // initializeValidators() |
75 |
| - |
76 |
| - JsonNode node = getJsonNodeFromStringContent("{\"id\": \"2\"}"); |
77 |
| - Set<ValidationMessage> errors = schema.validate(node); |
78 |
| - assertThat(errors.size(), is(1)); |
| 43 | + JsonNode schemaNode = JsonMapperFactory.getInstance().readTree( |
| 44 | + "{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}"); |
| 45 | + /* |
| 46 | + * This should be cached for performance. |
| 47 | + * |
| 48 | + * Loading from a JsonNode is not recommended as there is no base IRI to use for |
| 49 | + * resolving relative $ref. |
| 50 | + * |
| 51 | + * Note that the V4 from the factory is the default version if $schema is not |
| 52 | + * specified. As $schema is specified in the data, V6 is used. |
| 53 | + */ |
| 54 | + JsonSchema schemaFromNode = factory.getSchema(schemaNode); |
| 55 | + /* |
| 56 | + * By default all schemas are preloaded eagerly but ref resolve failures are not |
| 57 | + * thrown. You check if there are issues with ref resolving using |
| 58 | + * initializeValidators() |
| 59 | + */ |
| 60 | + schemaFromNode.initializeValidators(); |
| 61 | + Set<ValidationMessage> errors = schemaFromNode.validate("{\"id\": \"2\"}", InputFormat.JSON); |
| 62 | + assertEquals(1, errors.size()); |
79 | 63 | }
|
80 |
| - |
81 | 64 | }
|
82 |
| - |
83 | 65 | ```
|
0 commit comments