Skip to content

Commit d865d10

Browse files
author
Matthias Sattel
committed
2 parents 21d80d2 + 3b50c53 commit d865d10

31 files changed

+785
-45
lines changed

CHANGELOG.md

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

1010
### Changed
1111

12+
## 1.0.29 - 2019-12-16
13+
14+
### Added
15+
16+
### Changed
17+
18+
- Update description in pom.xml to match readme.md. Thanks @reftel
19+
- fixes #232 update meta schema URI to https
20+
- fixes #229 move the remotes to resource from draftv4
21+
- fixes #228 support boolean schema in the dependencies validator
22+
- enable const validator test for v6
23+
- fixes #224 support boolean schema for the item validator
24+
- fixes #222 add document for URL mapping
25+
26+
## 1.0.28 - 2019-11-25
27+
28+
### Added
29+
30+
### Changed
31+
32+
- fixes #219 Fix for oneOf when not all properties are matched. Thanks @aznan2
33+
1234
## 1.0.27 - 2019-11-18
1335

1436
### Added

README.md

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,167 @@
1-
## This is a fork of https://github.com/networknt/json-schema-validator to support Android 6 API >=23
1+
[Stack Overflow](https://stackoverflow.com/questions/tagged/light-4j) |
2+
[Google Group](https://groups.google.com/forum/#!forum/light-4j) |
3+
[Gitter Chat](https://gitter.im/networknt/light-rest-4j) |
4+
[Subreddit](https://www.reddit.com/r/lightapi/) |
5+
[Youtube](https://www.youtube.com/channel/UCHCRMWJVXw8iB7zKxF55Byw) |
6+
[Documentation](https://doc.networknt.com/library/json-schema-validator/) |
7+
[Javadocs](https://www.javadoc.io/doc/com.networknt/json-schema-validator) |
8+
[Contribution Guide](https://doc.networknt.com/contribute/) |
9+
10+
[![Build Status](https://travis-ci.org/networknt/json-schema-validator.svg?branch=master)](https://travis-ci.org/networknt/json-schema-validator) [![codecov.io](https://codecov.io/github/networknt/json-schema-validator/coverage.svg?branch=master)](https://codecov.io/github/networknt/json-schema-validator?branch=master)
11+
12+
13+
This is a Java implementation of the [JSON Schema Core Draft v4, v6, v7 and v2019-09](http://json-schema.org/latest/json-schema-core.html) specification for JSON schema validation. In addition, it also works for OpenAPI 3.0 request/response validation with some [configuration flags](doc/config.md). The default JSON parser is the [Jackson](https://github.com/FasterXML/jackson) that is the most popular one. As it is a key component in our [light-4j](https://github.com/networknt/light-4j) microservices framework to validate request/response against OpenAPI specification for [light-rest-4j](http://www.networknt.com/style/light-rest-4j/) and RPC schema for [light-hybrid-4j](http://www.networknt.com/style/light-hybrid-4j/) at runtime, performance is the most important aspect in the design.
14+
15+
## Why this library
16+
17+
#### Performance
18+
19+
It is the fastest Java JSON Schema Validator as far as I know. Here is the testing result compare with the other two open-source implementations. It is about 32 times faster than the Fge and five times faster than the Everit.
20+
21+
fge: 7130ms
22+
23+
everit-org: 1168ms
24+
25+
networknt: 223ms
26+
27+
You can run the performance tests for three libraries from [https://github.com/networknt/json-schema-validator-perftest](https://github.com/networknt/json-schema-validator-perftest)
28+
29+
#### Parser
30+
31+
It uses Jackson that is the most popular JSON parser in Java. If you are using Jackson parser already in your project, it is natural to choose this library over others for schema validation.
32+
33+
#### YAML Support
34+
35+
The library works with JSON and YAML on both schema definitions and input data.
36+
37+
#### OpenAPI Support
38+
39+
The OpenAPI 3.0 specification is using JSON schema to validate the request/response, but there are some differences. With a configuration file, you can enable the library to work with OpenAPI 3.0 validation.
40+
41+
#### Dependency
42+
43+
Following the design principle of the Light Platform, this library has minimum dependencies to ensure there are no dependency conflicts when using it.
44+
45+
Here are the dependencies.
46+
47+
```
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>
63+
```
64+
65+
#### Community
66+
67+
This library is very active with a lot of contributors. New features and bug fixes are handled quickly by the team members. Because it is an essential dependency of the [light-4j](https://github.com/networknt/light-4j) framework in the same GitHub organization, it will be evolved and maintained along with the framework.
68+
69+
## Prerequisite
70+
71+
The library supports Java 8 and up. If you want to build from the source code, you need to install JDK 8 locally. To support multiple version of JDK, you can use [SDKMAN](https://www.networknt.com/tool/sdk/)
72+
73+
## Dependency
74+
75+
This package is available on Maven central.
76+
77+
Maven:
78+
79+
```xml
80+
<dependency>
81+
<groupId>com.networknt</groupId>
82+
<artifactId>json-schema-validator</artifactId>
83+
<version>1.0.29</version>
84+
</dependency>
85+
```
86+
87+
Gradle:
88+
89+
```
90+
dependencies {
91+
compile(group: "com.networknt", name: "json-schema-validator", version: "1.0.29");
92+
}
93+
```
94+
95+
For the latest version, please check the [release](https://github.com/networknt/json-schema-validator/releases) page.
96+
97+
## [Quick Start](doc/quickstart.md)
98+
99+
## [Validators](doc/validators.md)
100+
101+
## [Configuration](doc/config.md)
102+
103+
## [Specification Version](doc/specversion.md)
104+
105+
## [YAML Validation](doc/yaml.md)
106+
107+
## [Schema Mapping](doc/schema-map.md)
108+
109+
## [Customized URIFetcher](doc/cust-fetcher.md)
110+
111+
## [Customized MetaSchema](doc/cust-meta.md)
112+
113+
114+
## Known issues
115+
116+
I have just updated the test suites from the [official website](https://github.com/json-schema-org/JSON-Schema-Test-Suite) as the old ones were copied from another Java validator. Now there are several issues that need to be addressed. All of them are edge cases, in my opinion, but need to be investigated. As my old test suites were inherited from another Java JSON Schema Validator, I guess other Java Validator would have the same issues as these issues are in the Java language itself.
117+
118+
[#7](https://github.com/networknt/json-schema-validator/issues/7)
119+
120+
[#5](https://github.com/networknt/json-schema-validator/issues/5)
121+
122+
## Contributors
123+
124+
Thanks to the following people who have contributed to this project. If you are using this library, please consider to be a sponsor for one of the contributors.
125+
126+
[@stevehu](https://github.com/sponsors/stevehu)
127+
128+
[@jiachen1120](https://github.com/jiachen1120)
129+
130+
[@BalloonWen](https://github.com/BalloonWen)
131+
132+
[@eskabetxe](https://github.com/eskabetxe)
133+
134+
[@ddobrin](https://github.com/ddobrin)
135+
136+
[@ehrmann](https://github.com/ehrmann)
137+
138+
[@rhwood](https://github.com/rhwood)
139+
140+
[@nitin1891](https://github.com/nitin1891)
141+
142+
[@jawaff](https://github.com/jawaff)
143+
144+
[@kosty](https://github.com/kosty)
145+
146+
[@chenyan71](https://github.com/chenyan71)
147+
148+
[@chrisken](https://github.com/chrisken)
149+
150+
[@NicholasAzar](https://github.com/NicholasAzar)
151+
152+
[@basinilya](https://github.com/basinilya)
153+
154+
For all contributors, please visit https://github.com/networknt/json-schema-validator/graphs/contributors
155+
156+
If you are a contributor, please join the [GitHub Sponsors](https://github.com/sponsors) and swithch the link to your sponsors dashboard via a PR.
157+
158+
## Sponsors
159+
160+
161+
### Individual Sponsors
162+
163+
164+
### Corporation Sponsors
165+
166+
167+

doc/cust_fetcher.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The default URIFetcher implementation uses JDK connection/socket without handling network exceptions. It works in most of the cases; however, if you want to have a customized implementation, you can do so. One user has his implementation with urirest to handle the timeout. A detailed discussion can be found in this [issue](https://github.com/networknt/json-schema-validator/issues/240)

doc/cust_meta.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
How to use your customized meta schema?
2+
3+
The library supports customized meta schema with this builder method. We have provided default instances for v4, v6, v7, and v2019-09 but users can always create their own JsonMetaSchema instance with customized meta schema.
4+
5+
https://github.com/networknt/json-schema-validator/blob/master/src/main/java/com/networknt/schema/JsonMetaSchema.java#L188
6+

doc/schema-map.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
While working with JSON schema validation, we have to use external references sometimes. However, there are two issues to have references to schemas on the Internet.
2+
3+
* Some applications are running inside a corporate network without Internet access.
4+
* Some of the Internet resources are not reliable
5+
6+
One solution is to change all the external reference to internal in JSON schemas, but this is error-prone and hard to maintain in a long run.
7+
8+
A smart solution is to map the external references to internal ones in a configuration file. This allows us to use the resources as they are without any modification. In the JSON schema specification, it is not allowed to use local filesystem resource directly. With the mapping, we can use the local resources without worrying about breaking the specification as the references are still in URL format in schemas. In addition, the mapped URL can be a different external URL, or embbeded within a JAR file with a lot more flexibility.
9+
10+
Note that when using a mapping, the local copy is always used, and the external reference is not queried.
11+
12+
### Usage
13+
14+
Basically, you can specify a mapping in the builder. For more details, please take a look at the test cases and the [PR](https://github.com/networknt/json-schema-validator/pull/125).
15+
16+
17+
### Real Example
18+
19+
https://github.com/JMRI/JMRI/blob/master/java/src/jmri/server/json/schema-map.json

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.networknt</groupId>
2121
<artifactId>json-schema-validator</artifactId>
22-
<version>1.0.27-CGM</version>
22+
<version>1.0.29</version>
2323
<packaging>bundle</packaging>
24-
<description>A json schema validator that supports draft v4</description>
24+
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
2525
<url>https://github.com/networknt/json-schema-validator</url>
2626
<name>JsonSchemaValidator</name>
2727
<developers>

src/main/java/com/networknt/schema/DependenciesValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public DependenciesValidator(String schemaPath, JsonNode schemaNode, JsonSchema
4343
for (int i = 0; i < pvalue.size(); i++) {
4444
depsProps.add(pvalue.get(i).asText());
4545
}
46-
} else if (pvalue.isObject()) {
46+
} else if (pvalue.isObject() || pvalue.isBoolean()) {
4747
schemaDeps.put(pname, new JsonSchema(validationContext, pname, parentSchema.getCurrentUri(), pvalue, parentSchema));
4848
}
4949
}

src/main/java/com/networknt/schema/ItemsValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ItemsValidator extends BaseJsonValidator implements JsonValidator {
3737

3838
public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) {
3939
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext);
40-
if (schemaNode.isObject()) {
40+
if (schemaNode.isObject() || schemaNode.isBoolean()) {
4141
schema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
4242
} else {
4343
tupleSchema = new ArrayList<JsonSchema>();

src/main/java/com/networknt/schema/JsonMetaSchema.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public static JsonMetaSchema getInstance() {
151151
}
152152

153153
private static class V201909 {
154-
private static String URI = "http://json-schema.org/draft/2019-09/schema#";
154+
private static String URI = "https://json-schema.org/draft/2019-09/schema";
155155
private static final String ID = "$id";
156156

157157
public static final List<Format> BUILTIN_FORMATS = new ArrayList<Format>(JsonMetaSchema.COMMON_BUILTIN_FORMATS);
@@ -172,7 +172,8 @@ public static JsonMetaSchema getInstance() {
172172
new NonValidationKeyword("title"),
173173
new NonValidationKeyword("description"),
174174
new NonValidationKeyword("default"),
175-
new NonValidationKeyword("definitions")
175+
new NonValidationKeyword("definitions"),
176+
new NonValidationKeyword("$defs") // newly added in 2018-09 release.
176177
))
177178
.build();
178179
}

src/main/java/com/networknt/schema/MultipleOfValidator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4848
// convert to BigDecimal since double type is not accurate enough to do the division and multiple
4949
BigDecimal accurateDividend = new BigDecimal(String.valueOf(nodeValue));
5050
BigDecimal accurateDivisor = new BigDecimal(String.valueOf(divisor));
51-
if (Math.abs(accurateDividend.divideAndRemainder(accurateDivisor)[1].doubleValue()) > 1e-12) {
51+
if (accurateDividend.divideAndRemainder(accurateDivisor)[1].abs().compareTo(BigDecimal.ZERO) > 0) {
5252
return Collections.singleton(buildValidationMessage(at, "" + divisor));
5353
}
5454
}

0 commit comments

Comments
 (0)