Skip to content

Commit b717e50

Browse files
authored
Merge pull request #309 from Subhajitdas298/master
Updated documentation
2 parents a8bec51 + e0e3c88 commit b717e50

File tree

11 files changed

+229
-190
lines changed

11 files changed

+229
-190
lines changed

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ Following the design principle of the Light Platform, this library has minimum d
4545
Here are the dependencies.
4646

4747
```
48-
<dependency>
49-
<groupId>com.fasterxml.jackson.core</groupId>
50-
<artifactId>jackson-databind</artifactId>
51-
<version>${version.jackson}</version>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.slf4j</groupId>
55-
<artifactId>slf4j-api</artifactId>
56-
<version>${version.slf4j}</version>
57-
</dependency>
58-
<dependency>
59-
<groupId>org.apache.commons</groupId>
60-
<artifactId>commons-lang3</artifactId>
61-
<version>${version.common-lang3}</version>
62-
</dependency>
48+
<dependency>
49+
<groupId>com.fasterxml.jackson.core</groupId>
50+
<artifactId>jackson-databind</artifactId>
51+
<version>${version.jackson}</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>org.slf4j</groupId>
56+
<artifactId>slf4j-api</artifactId>
57+
<version>${version.slf4j}</version>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>org.apache.commons</groupId>
62+
<artifactId>commons-lang3</artifactId>
63+
<version>${version.common-lang3}</version>
64+
</dependency>
6365
```
6466

6567
#### Community
@@ -110,7 +112,7 @@ For the latest version, please check the [release](https://github.com/networknt/
110112

111113
## [Customized MetaSchema](doc/cust-meta.md)
112114

113-
## [Collector Context](doc/collector_context.md)
115+
## [Collector Context](doc/collector-context.md)
114116

115117
## [ECMA-262 Regex](doc/ecma-262.md)
116118

File renamed without changes.

doc/config.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ Most of the configuration flags are used to control the difference between Swagg
1313
When you create a JsonSchema instance from the JsonSchemaFactory, you can pass an object of SchemaValidatorsConfig as the second parameter.
1414

1515
```
16-
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
17-
config.setTypeLoose(false);
18-
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
16+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
17+
config.setTypeLoose(false);
18+
JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema, config);
1919
```
2020

2121
#### Configurations
File renamed without changes.
File renamed without changes.

doc/ecma-262.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ If you want to ensure full compliance, use the org.jruby.joni. It is 1.5 times s
1313
Here is the test case that shows how to pass a config object to use the ECMA-262 library.
1414

1515
```
16-
@Test(expected = JsonSchemaException.class)
17-
public void testInvalidPatternPropertiesValidatorECMA262() throws Exception {
18-
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
19-
config.setEcma262Validator(true);
20-
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
21-
JsonSchema schema = factory.getSchema("{\"patternProperties\":6}", config);
22-
23-
JsonNode node = getJsonNodeFromStringContent("");
24-
Set<ValidationMessage> errors = schema.validate(node);
25-
Assert.assertEquals(errors.size(), 0);
26-
}
16+
@Test(expected = JsonSchemaException.class)
17+
public void testInvalidPatternPropertiesValidatorECMA262() throws Exception {
18+
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
19+
config.setEcma262Validator(true);
20+
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
21+
JsonSchema schema = factory.getSchema("{\"patternProperties\":6}", config);
22+
23+
JsonNode node = getJsonNodeFromStringContent("");
24+
Set<ValidationMessage> errors = schema.validate(node);
25+
Assert.assertEquals(errors.size(), 0);
26+
}
2727
```
2828

2929

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
```

0 commit comments

Comments
 (0)