Skip to content

Commit 3156bb4

Browse files
Added schema version detection based sample.
Updated structure and text to be more descriptive. Uniform code block style.
1 parent 52e606a commit 3156bb4

File tree

2 files changed

+55
-66
lines changed

2 files changed

+55
-66
lines changed

doc/quickstart.md

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,79 @@
11
## Quick Start
22

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.
47

58
```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-
*/
339
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 {
3514
InputStream is1 = Thread.currentThread().getContextClassLoader()
3615
.getResourceAsStream(name);
37-
38-
ObjectMapper mapper = new ObjectMapper();
39-
JsonNode node = mapper.readTree(is1);
40-
return node;
16+
return mapper.readTree(is1);
4117
}
4218

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);
4721
}
4822

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));
5325
}
5426

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);
5729
InputStream is = Thread.currentThread().getContextClassLoader()
5830
.getResourceAsStream(name);
59-
JsonSchema schema = factory.getSchema(is);
60-
return schema;
31+
return factory.getSchema(is);
6132
}
6233

34+
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
35+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
36+
return factory.getSchema(schemaContent);
37+
}
6338

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));
6842
}
6943

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);
7447
}
7548

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);
8053
}
81-
}
8254

55+
}
8356
```
8457
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.
8558

8659
```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+
}
9178

9279
```

src/test/java/com/networknt/schema/BaseJsonSchemaValidatorTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import com.fasterxml.jackson.databind.JsonNode;
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121

22+
import java.io.IOException;
2223
import java.io.InputStream;
2324
import java.net.URI;
25+
import java.net.URISyntaxException;
2426
import java.net.URL;
2527

2628
/**
@@ -30,17 +32,17 @@ public class BaseJsonSchemaValidatorTest {
3032

3133
private ObjectMapper mapper = new ObjectMapper();
3234

33-
protected JsonNode getJsonNodeFromClasspath(String name) throws Exception {
35+
protected JsonNode getJsonNodeFromClasspath(String name) throws IOException {
3436
InputStream is1 = Thread.currentThread().getContextClassLoader()
3537
.getResourceAsStream(name);
3638
return mapper.readTree(is1);
3739
}
3840

39-
protected JsonNode getJsonNodeFromStringContent(String content) throws Exception {
41+
protected JsonNode getJsonNodeFromStringContent(String content) throws IOException {
4042
return mapper.readTree(content);
4143
}
4244

43-
protected JsonNode getJsonNodeFromUrl(String url) throws Exception {
45+
protected JsonNode getJsonNodeFromUrl(String url) throws IOException {
4446
return mapper.readTree(new URL(url));
4547
}
4648

@@ -56,7 +58,7 @@ protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
5658
return factory.getSchema(schemaContent);
5759
}
5860

59-
protected JsonSchema getJsonSchemaFromUrl(String uri) throws Exception {
61+
protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException {
6062
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
6163
return factory.getSchema(new URI(uri));
6264
}

0 commit comments

Comments
 (0)