|
1 | 1 | ## Quick Start
|
2 | 2 |
|
3 |
| -To use the validator, you need to have both the JsonSchema object and JsonNode object constructed, and there are many ways to do that. Here is my base test class that shows you several ways to construct these from String, Stream, Url, and JsonNode. 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 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. |
4 | 7 |
|
5 | 8 | ```java
|
6 |
| -/* |
7 |
| - * Copyright (c) 2016 Network New Technologies Inc. |
8 |
| - * |
9 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
10 |
| - * you may not use this file except in compliance with the License. |
11 |
| - * You may obtain a copy of the License at |
12 |
| - * |
13 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
14 |
| - * |
15 |
| - * Unless required by applicable law or agreed to in writing, software |
16 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
17 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 |
| - * See the License for the specific language governing permissions and |
19 |
| - * limitations under the License. |
20 |
| - */ |
21 |
| - |
22 |
| -package com.networknt.schema; |
23 |
| - |
24 |
| -import com.fasterxml.jackson.databind.JsonNode; |
25 |
| -import com.fasterxml.jackson.databind.ObjectMapper; |
26 |
| - |
27 |
| -import java.io.InputStream; |
28 |
| -import java.net.URL; |
29 |
| - |
30 |
| -/** |
31 |
| - * Created by steve on 22/10/16. |
32 |
| - */ |
33 | 9 | public class BaseJsonSchemaValidatorTest {
|
34 |
| - protected JsonNode getJsonNodeFromClasspath(String name) throws Exception { |
| 10 | + |
| 11 | + private ObjectMapper mapper = new ObjectMapper(); |
| 12 | + |
| 13 | + protected JsonNode getJsonNodeFromClasspath(String name) throws IOException { |
35 | 14 | InputStream is1 = Thread.currentThread().getContextClassLoader()
|
36 | 15 | .getResourceAsStream(name);
|
37 |
| - |
38 |
| - ObjectMapper mapper = new ObjectMapper(); |
39 |
| - JsonNode node = mapper.readTree(is1); |
40 |
| - return node; |
| 16 | + return mapper.readTree(is1); |
41 | 17 | }
|
42 | 18 |
|
43 |
| - protected JsonNode getJsonNodeFromStringContent(String content) throws Exception { |
44 |
| - ObjectMapper mapper = new ObjectMapper(); |
45 |
| - JsonNode node = mapper.readTree(content); |
46 |
| - return node; |
| 19 | + protected JsonNode getJsonNodeFromStringContent(String content) throws IOException { |
| 20 | + return mapper.readTree(content); |
47 | 21 | }
|
48 | 22 |
|
49 |
| - protected JsonNode getJsonNodeFromUrl(String url) throws Exception { |
50 |
| - ObjectMapper mapper = new ObjectMapper(); |
51 |
| - JsonNode node = mapper.readTree(new URL(url)); |
52 |
| - return node; |
| 23 | + protected JsonNode getJsonNodeFromUrl(String url) throws IOException { |
| 24 | + return mapper.readTree(new URL(url)); |
53 | 25 | }
|
54 | 26 |
|
55 |
| - protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception { |
56 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); |
| 27 | + protected JsonSchema getJsonSchemaFromClasspath(String name) { |
| 28 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
57 | 29 | InputStream is = Thread.currentThread().getContextClassLoader()
|
58 | 30 | .getResourceAsStream(name);
|
59 |
| - JsonSchema schema = factory.getSchema(is); |
60 |
| - return schema; |
| 31 | + return factory.getSchema(is); |
61 | 32 | }
|
62 | 33 |
|
| 34 | + protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) { |
| 35 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
| 36 | + return factory.getSchema(schemaContent); |
| 37 | + } |
63 | 38 |
|
64 |
| - protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception { |
65 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); |
66 |
| - JsonSchema schema = factory.getSchema(schemaContent); |
67 |
| - return schema; |
| 39 | + protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException { |
| 40 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
| 41 | + return factory.getSchema(new URI(uri)); |
68 | 42 | }
|
69 | 43 |
|
70 |
| - protected JsonSchema getJsonSchemaFromUrl(String url) throws Exception { |
71 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); |
72 |
| - JsonSchema schema = factory.getSchema(new URL(url)); |
73 |
| - return schema; |
| 44 | + protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) { |
| 45 | + JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); |
| 46 | + return factory.getSchema(jsonNode); |
74 | 47 | }
|
75 | 48 |
|
76 |
| - protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception { |
77 |
| - JsonSchemaFactory factory = JsonSchemaFactory.getInstance(); |
78 |
| - JsonSchema schema = factory.getSchema(jsonNode); |
79 |
| - return schema; |
| 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); |
80 | 53 | }
|
81 |
| -} |
82 | 54 |
|
| 55 | +} |
83 | 56 | ```
|
84 | 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.
|
85 | 58 |
|
86 | 59 | ```java
|
87 |
| - JsonSchema schema = getJsonSchemaFromStringContent("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); |
88 |
| - JsonNode node = getJsonNodeFromStringContent("7"); |
89 |
| - Set<ValidationMessage> errors = schema.validate(node); |
90 |
| - assertThat(errors.size(), is(1)); |
| 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 | + JsonNode node = getJsonNodeFromStringContent("{\"id\": \"2\"}"); |
| 73 | + Set<ValidationMessage> errors = schema.validate(node); |
| 74 | + assertThat(errors.size(), is(1)); |
| 75 | + } |
| 76 | + |
| 77 | +} |
91 | 78 |
|
92 | 79 | ```
|
0 commit comments