Skip to content

Commit e41595a

Browse files
authored
Merge pull request #2 from networknt/master
Updating changes to my fork
2 parents 5120bce + 1abc99b commit e41595a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1430
-468
lines changed

CHANGELOG.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,41 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
## 1.0.42 - 2020-06-30
13+
14+
### Added
15+
16+
### Changed
17+
18+
- fixes #311 Split the PatternValidator into 2 classes. Thanks @Buuhuu
19+
20+
## 1.0.41 - 2020-06-25
21+
22+
### Added
23+
24+
### Changed
25+
26+
- fixes #307 Make runtime dependency to org.jruby.joni:joni optional. Thanks @Buuhuu
27+
- fixes #305 Automatically determine schema version from schema file. Thanks @Subhajitdas298
28+
- fixes #297 ValidationContext using is not correct in UUIDValidator. Thanks @qiunju
29+
30+
31+
## 1.0.40 - 2020-05-27
32+
33+
### Added
34+
35+
### Changed
36+
37+
- fixes #294 fixes unknownMetaSchema error with normalized URI
38+
39+
## 1.0.39 - 2020-04-28
40+
41+
### Added
42+
43+
### Changed
44+
45+
- fixes #289 Adding getAll method on CollectorContext class. Thanks @prashanthjos
46+
1247
## 1.0.38 - 2020-04-12
1348

1449
### Added

README.md

Lines changed: 20 additions & 18 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
@@ -80,15 +82,15 @@ Maven:
8082
<dependency>
8183
<groupId>com.networknt</groupId>
8284
<artifactId>json-schema-validator</artifactId>
83-
<version>1.0.38</version>
85+
<version>1.0.42</version>
8486
</dependency>
8587
```
8688

8789
Gradle:
8890

8991
```
9092
dependencies {
91-
compile(group: "com.networknt", name: "json-schema-validator", version: "1.0.38");
93+
compile(group: "com.networknt", name: "json-schema-validator", version: "1.0.42");
9294
}
9395
```
9496

@@ -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/compatibility.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
### Legend
3+
4+
Symbol | Meaning |
5+
:-----:|---------|
6+
🟢 | Fully implemented
7+
🟡 | Partially implemented
8+
🔴 | Not implemented
9+
🚫 | Not defined in Schema Version.
10+
11+
### Compatibility with JSON Schema versions
12+
13+
Validation Keyword/Schema | Draft 4 | Draft 6 | Draft 7 | Draft 2019-09 |
14+
---------------- |:--------------:|:-------: |:-------: |:-------------:|
15+
$ref | 🟢 | 🟢 | 🟢 | 🟢
16+
additionalProperties | 🟢 | 🟢 | 🟢 | 🟢
17+
additionalItems | 🟢 | 🟢 | 🟢 | 🟢
18+
allOf | 🟢 | 🟢 | 🟢 | 🟢
19+
anyOf | 🟢 | 🟢 | 🟢 | 🟢
20+
const | 🚫 | 🟢 | 🟢 | 🟢
21+
contains | 🚫 | 🟢 | 🟢 | 🟢
22+
contentEncoding | 🚫 | 🚫 | 🔴 | 🔴
23+
contentMediaType | 🚫 | 🚫 | 🔴 | 🔴
24+
dependencies | 🟢 | 🟢 |🟢 | 🟢
25+
enum | 🟢 | 🟢 | 🟢 | 🟢
26+
exclusiveMaximum (boolean) | 🟢 | 🚫 | 🚫 | 🚫
27+
exclusiveMaximum (numeric) | 🚫 | 🟢 | 🟢 | 🟢
28+
exclusiveMinimum (boolean) | 🟢 | 🚫 | 🚫 | 🚫
29+
exclusiveMinimum (numeric) | 🚫 | 🟢 | 🟢 | 🟢
30+
items | 🟢 | 🟢 | 🟢 | 🟢
31+
maximum | 🟢 | 🟢 | 🟢 | 🟢
32+
maxItems | 🟢 | 🟢 | 🟢 | 🟢
33+
maxLength | 🟢 | 🟢 | 🟢 | 🟢
34+
maxProperties | 🟢 | 🟢 | 🟢 | 🟢
35+
minimum | 🟢 | 🟢 | 🟢 | 🟢
36+
minItems | 🟢 | 🟢 | 🟢 | 🟢
37+
minLength | 🟢 | 🟢 | 🟢 | 🟢
38+
minProperties | 🟢 | 🟢 | 🟢 | 🟢
39+
multipleOf | 🟢 | 🟢 | 🟢 | 🟢
40+
not | 🟢 | 🟢 | 🟢 | 🟢
41+
oneOf | 🟢 | 🟢 | 🟢 | 🟢
42+
pattern | 🟢 | 🟢 | 🟢 | 🟢
43+
patternProperties | 🟢 | 🟢 | 🟢 | 🟢
44+
properties | 🟢 | 🟢 | 🟢 | 🟢
45+
propertyNames | 🚫 | 🔴 | 🔴 | 🔴
46+
required | 🟢 | 🟢 | 🟢 | 🟢
47+
type | 🟢 | 🟢 | 🟢 | 🟢
48+
uniqueItems | 🟢 | 🟢 | 🟢 | 🟢
49+
50+
### Semantic Validation (Format)
51+
52+
Format | Draft 4 | Draft 6 | Draft 7 | Draft 2019-09 |
53+
-------|---------|---------|---------|---------------|
54+
date |🚫 | 🚫 | 🟢 | 🟢
55+
date-time | 🟢 | 🟢 | 🟢 | 🟢
56+
duration | 🚫 | 🚫 | 🔴 | 🔴
57+
email | 🟢 | 🟢 | 🟢 | 🟢
58+
hostname | 🟢 | 🟢 | 🟢 | 🟢
59+
idn-email | 🚫 | 🚫 | 🔴 | 🔴
60+
idn-hostname | 🚫 | 🚫 | 🔴 | 🔴
61+
ipv4 | 🟢 | 🟢 | 🟢 | 🟢
62+
ipv6 | 🟢 | 🟢 | 🟢 | 🟢
63+
iri | 🚫 | 🚫 | 🔴 | 🔴
64+
iri-reference | 🚫 | 🚫 | 🔴 | 🔴
65+
json-pointer | 🚫 | 🔴 | 🔴 | 🔴
66+
relative-json-pointer | 🚫 | 🔴 | 🔴 | 🔴
67+
regex | 🚫 | 🚫 | 🔴 | 🔴
68+
time | 🚫 | 🚫 | 🟢 | 🟢
69+
uri | 🟢 | 🟢 | 🟢 | 🟢
70+
uri-reference | 🚫 | 🔴 | 🔴 | 🔴
71+
uri-template | 🚫 | 🔴 | 🔴 | 🔴
72+
uuid | 🚫 | 🚫 | 🟢 | 🟢
73+
74+
### Footnotes
75+
1. Note that the validation are only optional for some of the keywords/formats.
76+
2. Refer to the corresponding JSON schema for more information on whether the keyword/format is optional or not.

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)