Skip to content

Commit

Permalink
Merge pull request #1 from networknt/master
Browse files Browse the repository at this point in the history
Merge Pull Request
  • Loading branch information
prashanthjos committed Apr 28, 2020
2 parents 133f8e4 + 51e18fd commit 2af4fd9
Show file tree
Hide file tree
Showing 57 changed files with 1,316 additions and 598 deletions.
42 changes: 42 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

## 1.0.38 - 2020-04-12

### Added

### Changed

- fixes #281 EmailValidator use ValidatorTypeCode Datetime

## 1.0.37 - 2020-04-06

### Added

### Changed

- fixes #280 NullPointerException in regex pattern validation if no SchemaValidatorsConfig is passed. Thanks @waizuwolf

## 1.0.36 - 2020-03-22

### Added

### Changed

- fixes #273 make the getInstance() deprecated
- fixes #258 Cyclic dependencies result in StackOverflowError. Thanks @francesc79

## 1.0.35 - 2020-03-13

### Added

### Changed

- fixes #272 Use ECMA-262 validator when requested. Thanks @eirnym

## 1.0.34 - 2020-03-12

### Added

### Changed

- fixes #268 Collector Context changes to handle simple Objects. Thanks @prashanthjos
- fixes #266 reformat the code and resolve javadoc warnnings

## 1.0.33 - 2020-03-09

### Added
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ Maven:
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.33</version>
<version>1.0.38</version>
</dependency>
```

Gradle:

```
dependencies {
compile(group: "com.networknt", name: "json-schema-validator", version: "1.0.33");
compile(group: "com.networknt", name: "json-schema-validator", version: "1.0.38");
}
```

Expand All @@ -112,6 +112,7 @@ For the latest version, please check the [release](https://github.com/networknt/

## [Collector Context](doc/collector_context.md)

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

## Known issues

Expand Down Expand Up @@ -155,7 +156,7 @@ Thanks to the following people who have contributed to this project. If you are

For all contributors, please visit https://github.com/networknt/json-schema-validator/graphs/contributors

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.
If you are a contributor, please join the [GitHub Sponsors](https://github.com/sponsors) and switch the link to your sponsors dashboard via a PR.

## Sponsors

Expand Down
2 changes: 1 addition & 1 deletion doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ When typeLoose is true, the validator will convert strings to different types to

* failFast

When set to true, the validation process stops immediately when the first error occurs. This mostly used on microservices that is designed to [fail-fast](https://www.networknt.com/architecture/fail-fast/), or users don't want to see hundreds of errors for a big payload. Please be aware that the validator throws an exception in the case the first error occurs. To learn how to use it, please follow the [test case](https://github.com/networknt/json-schema-validator/blob/master/src/test/java/com/networknt/schema/JsonSchemaTest.java#L352).
When set to true, the validation process stops immediately when the first error occurs. This mostly used on microservices that is designed to [fail-fast](https://www.networknt.com/architecture/fail-fast/), or users don't want to see hundreds of errors for a big payload. Please be aware that the validator throws an exception in the case the first error occurs. To learn how to use it, please follow the [test case](https://github.com/networknt/json-schema-validator/blob/master/src/test/java/com/networknt/schema/V4JsonSchemaTest.java#L352).

* handleNullableField

Expand Down
29 changes: 29 additions & 0 deletions doc/ecma-262.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
For the pattern validator, we now have two options for regex in the library. The default one is java.util. regex; however, you can use the ECMA-262 standard library org.jruby.joni by configuration.

As we know, the JSON schema is designed based on the Javascript language and its regex. The Java internal implementation has some differences which don't comply with the standard. For most users, these edge cases are not the issue as they are not using them anyway. Even when they are using it, they are expecting the Java regex result as the application is built on the Java platform. For users who want to ensure that they are using 100% standard patter validator, we have provided an option to override the default regex library with org.jruby.joni that is complying with the ECMA-262 standard.

### Which one to choose?

If you want a faster regex lib and don't care about the slight difference between Java and Javascript regex, then you don't need to do anything. The default regex lib is the java.util.regex.

If you want to ensure full compliance, use the org.jruby.joni. It is 1.5 times slower then java.util.regex. Depending on your use case, it might not be an issue.

### How to switch?

Here is the test case that shows how to pass a config object to use the ECMA-262 library.

```
@Test(expected = JsonSchemaException.class)
public void testInvalidPatternPropertiesValidatorECMA262() throws Exception {
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setEcma262Validator(true);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
JsonSchema schema = factory.getSchema("{\"patternProperties\":6}", config);
JsonNode node = getJsonNodeFromStringContent("");
Set<ValidationMessage> errors = schema.validate(node);
Assert.assertEquals(errors.size(), 0);
}
```


5 changes: 5 additions & 0 deletions doc/schema-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ Basically, you can specify a mapping in the builder. For more details, please ta
### Real Example

https://github.com/JMRI/JMRI/blob/master/java/src/jmri/server/json/schema-map.json

In case you provide the schema through an InputStream or a String to resolve $ref with URN (relative path), you need to provide the URNFactory to the JsonSchemaFactory.Builder.
URNFactory interface will allow you to resolve URN to URI.

please take a look at the test cases and the [PR](https://github.com/networknt/json-schema-validator/pull/274).
2 changes: 1 addition & 1 deletion doc/validators.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ The `if`, `then` and `else` keywords allow the application of a subschema based

If `if` is valid, `then` must also be valid (and `else` is ignored.) If `if` is invalid, `else` must also be valid (and `then` is ignored).

For usage, please refer to the test cases at https://github.com/networknt/json-schema-validator/blob/master/src/test/resources/tests/if.json
For usage, please refer to the test cases at https://github.com/networknt/json-schema-validator/blob/master/src/test/resources/draft7/if-then-else.json

10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>1.0.33</version>
<version>1.0.38</version>
<packaging>bundle</packaging>
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
<url>https://github.com/networknt/json-schema-validator</url>
Expand Down Expand Up @@ -65,11 +65,12 @@
<version.jackson>2.10.0</version.jackson>
<version.slf4j>1.7.25</version.slf4j>
<version.common-lang3>3.5</version.common-lang3>
<version.joni>2.1.31</version.joni>
<version.logback>1.2.3</version.logback>
<version.junit>4.12</version.junit>
<version.mockito>2.7.21</version.mockito>
<version.hamcrest>1.3</version.hamcrest>
<version.undertow>2.0.28.Final</version.undertow>
<version.undertow>2.0.29.Final</version.undertow>
</properties>
<dependencies>
<dependency>
Expand All @@ -87,6 +88,11 @@
<artifactId>commons-lang3</artifactId>
<version>${version.common-lang3}</version>
</dependency>
<dependency>
<groupId>org.jruby.joni</groupId>
<artifactId>joni</artifactId>
<version>${version.joni}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
25 changes: 20 additions & 5 deletions src/main/java/com/networknt/schema/AbstractCollector.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/*
* Copyright (c) 2020 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.networknt.schema;

public abstract class AbstractCollector<E> implements Collector<E>{
public abstract class AbstractCollector<E> implements Collector<E> {

@Override
public void combine(Object object) {
// Do nothing. This is the default Implementation.
}
@Override
public void combine(Object object) {
// Do nothing. This is the default Implementation.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public AdditionalPropertiesValidator(String schemaPath, JsonNode schemaNode, Jso
additionalPropertiesSchema = null;
} else if (schemaNode.isObject()) {
allowAdditionalProperties = true;
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema);
additionalPropertiesSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode, parentSchema)
.initialize();
} else {
allowAdditionalProperties = false;
additionalPropertiesSchema = null;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/AllOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public AllOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ALL_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
.initialize());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public AnyOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ANY_OF, validationContext);
int size = schemaNode.size();
for (int i = 0; i < size; i++) {
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema));
schemas.add(new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), schemaNode.get(i), parentSchema)
.initialize());
}
}

Expand Down
44 changes: 31 additions & 13 deletions src/main/java/com/networknt/schema/Collector.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2020 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.schema;

/**
Expand All @@ -9,20 +25,22 @@
public interface Collector<E> {


/**
* This method should be called by the intermediate touch points that want to
* combine the data being collected by this collector. This is an optional
* method and could be used when the same collector is used for collecting data
* at multiple touch points or accumulating data at same touch point.
*/
public void combine(Object object);
/**
* This method should be called by the intermediate touch points that want to
* combine the data being collected by this collector. This is an optional
* method and could be used when the same collector is used for collecting data
* at multiple touch points or accumulating data at same touch point.
* @param object Object
*/
public void combine(Object object);

/**
* Final method called by the framework that returns the actual collected data.
* If the collector is not accumulating data or being used to collect data at
* multiple touch points, only this method can be implemented.
*/
public E collect();
/**
* Final method called by the framework that returns the actual collected data.
* If the collector is not accumulating data or being used to collect data at
* multiple touch points, only this method can be implemented.
* @return E element
*/
public E collect();


}
Loading

0 comments on commit 2af4fd9

Please sign in to comment.