@@ -44,6 +44,91 @@ Add the following to your `pom.xml`:
44
44
45
45
## Quickstart
46
46
47
+ To use the validator, you need to have both JsonSchema object and JsonNode object constructed and there are many ways to do that. Here is my
48
+ base test class that shows you several way to construct these from String, Stream, Url and JsonNode. Pay attention on JsonSchemaFactory class
49
+ as it is the way to construct JsonSchema object.
50
+
51
+
52
+ ```
53
+ /*
54
+ * Copyright (c) 2016 Network New Technologies Inc.
55
+ *
56
+ * Licensed under the Apache License, Version 2.0 (the "License");
57
+ * You may not use this file except in compliance with the License.
58
+ * You may obtain a copy of the License at
59
+ *
60
+ * http://www.apache.org/licenses/LICENSE-2.0
61
+ *
62
+ * Unless required by applicable law or agreed to in writing, software
63
+ * distributed under the License is distributed on an "AS IS" BASIS,
64
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
65
+ * See the License for the specific language governing permissions and
66
+ * limitations under the License.
67
+ */
68
+
69
+ package com.networknt.schema;
70
+
71
+ import com.fasterxml.jackson.databind.JsonNode;
72
+ import com.fasterxml.jackson.databind.ObjectMapper;
73
+
74
+ import java.io.InputStream;
75
+ import java.net.URL;
76
+
77
+ /**
78
+ * Created by steve on 22/10/16.
79
+ */
80
+ public class BaseJsonSchemaValidatorTest {
81
+ protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
82
+ InputStream is1 = Thread.currentThread().getContextClassLoader()
83
+ .getResourceAsStream(name);
84
+
85
+ ObjectMapper mapper = new ObjectMapper();
86
+ JsonNode node = mapper.readTree(is1);
87
+ return node;
88
+ }
89
+
90
+ protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
91
+ ObjectMapper mapper = new ObjectMapper();
92
+ JsonNode node = mapper.readTree(content);
93
+ return node;
94
+ }
95
+
96
+ protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
97
+ ObjectMapper mapper = new ObjectMapper();
98
+ JsonNode node = mapper.readTree(new URL(url));
99
+ return node;
100
+ }
101
+
102
+ protected JsonSchema getJsonSchemaFromClasspath(String name) throws Exception {
103
+ JsonSchemaFactory factory = new JsonSchemaFactory();
104
+ InputStream is = Thread.currentThread().getContextClassLoader()
105
+ .getResourceAsStream(name);
106
+ JsonSchema schema = factory.getSchema(is);
107
+ return schema;
108
+ }
109
+
110
+ protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) throws Exception {
111
+ JsonSchemaFactory factory = new JsonSchemaFactory();
112
+ JsonSchema schema = factory.getSchema(schemaContent);
113
+ return schema;
114
+ }
115
+
116
+ protected JsonSchema getJsonSchemaFromUrl(String url) throws Exception {
117
+ JsonSchemaFactory factory = new JsonSchemaFactory();
118
+ JsonSchema schema = factory.getSchema(new URL(url));
119
+ return schema;
120
+ }
121
+
122
+ protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) throws Exception {
123
+ JsonSchemaFactory factory = new JsonSchemaFactory();
124
+ JsonSchema schema = factory.getSchema(jsonNode);
125
+ return schema;
126
+ }
127
+ }
128
+
129
+ ```
130
+ And the following is one of the test case in one of the test class extends from above base class. As you can see, it constructs JsonSchema
131
+ and JsonNode from String.
47
132
48
133
```
49
134
JsonSchema schema = getJsonSchemaFromStringContent("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}");
0 commit comments