-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add annotation support refactor keywords to use annotations implement…
… output formats (#942) * Support annotations * Refactor recursive ref * Fix IllegalStateException on recursive call in computeIfAbsent * Refactor * Refactor results * Refactor * Remove scope * Refactor * Add can short circuit logic * Fix performance * Fix * Refactor * Enable disabled tests * Fix contains * Fix fast fail * Refactor * Update docs * Add schemaNode and instanceNode to validation message and configure serialization * Fail fast based on execution context config * Fix fail fast * Refactor annotation config. * Update doc * Fix 939 and add 940 test * Fix 936 * Fix 935 * Fix * Add convenience method for schema loader * Add convenience method for schema mappers * Support annotation collection for reporting * Support output formatting * Refactor * Refactor * Collect format annotations * Refactor * Refactor * Refactor * Refactor * Redesign and fix fail fast logic * Support hierarchical output * Update docs * Throw specific exceptions if ref cannot be resolved * Update docs * Add 857 test * Javadoc * Add 927 test * Move out non suite tests * Update to latest test suite * Fix os line ending difference * Fix os line ending difference * Fix javadoc * Fix id handling * Add tests for format output formatting * Add test for type union
- Loading branch information
1 parent
e95642c
commit 9c95c06
Showing
215 changed files
with
7,013 additions
and
2,429 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,65 @@ | ||
## Quick Start | ||
|
||
To use the validator, we need to have both the `JsonSchema` object and `JsonNode` object constructed. | ||
There are many ways to do that. | ||
Here is base test class, that shows several ways to construct these from `String`, `Stream`, `Url`, and `JsonNode`. | ||
Please pay attention to the `JsonSchemaFactory` class as it is the way to construct the `JsonSchema` object. | ||
To use the validator, we need to have the `JsonSchema` loaded and cached. | ||
|
||
```java | ||
public class BaseJsonSchemaValidatorTest { | ||
For simplicity the following test loads a schema from a `String` or `JsonNode`. Note that loading a schema in this manner is not recommended as a relative `$ref` will not be properly resolved as there is no base IRI. | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
The preferred method of loading a schema is by using a `SchemaLocation` and by configuring the appropriate `SchemaMapper` and `SchemaLoader` on the `JsonSchemaFactory`. | ||
|
||
protected JsonNode getJsonNodeFromClasspath(String name) throws IOException { | ||
InputStream is1 = Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream(name); | ||
return mapper.readTree(is1); | ||
} | ||
```java | ||
package com.example; | ||
|
||
protected JsonNode getJsonNodeFromStringContent(String content) throws IOException { | ||
return mapper.readTree(content); | ||
} | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
protected JsonNode getJsonNodeFromUrl(String url) throws IOException { | ||
return mapper.readTree(new URL(url)); | ||
} | ||
import java.util.Set; | ||
|
||
protected JsonSchema getJsonSchemaFromClasspath(String name) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); | ||
InputStream is = Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream(name); | ||
return factory.getSchema(is); | ||
} | ||
import org.junit.jupiter.api.Test; | ||
|
||
protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); | ||
return factory.getSchema(schemaContent); | ||
} | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.networknt.schema.*; | ||
import com.networknt.schema.serialization.JsonMapperFactory; | ||
|
||
protected JsonSchema getJsonSchemaFromUrl(String uri) throws URISyntaxException { | ||
public class SampleTest { | ||
@Test | ||
void schemaFromString() throws JsonMappingException, JsonProcessingException { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); | ||
return factory.getSchema(SchemaLocation.of(uri)); | ||
/* | ||
* This should be cached for performance. | ||
* | ||
* Loading from a String is not recommended as there is no base IRI to use for | ||
* resolving relative $ref. | ||
*/ | ||
JsonSchema schemaFromString = factory | ||
.getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); | ||
Set<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON); | ||
assertEquals(1, errors.size()); | ||
} | ||
|
||
protected JsonSchema getJsonSchemaFromJsonNode(JsonNode jsonNode) { | ||
@Test | ||
void schemaFromJsonNode() throws JsonMappingException, JsonProcessingException { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4); | ||
return factory.getSchema(jsonNode); | ||
} | ||
|
||
// Automatically detect version for given JsonNode | ||
protected JsonSchema getJsonSchemaFromJsonNodeAutomaticVersion(JsonNode jsonNode) { | ||
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersionDetector.detect(jsonNode)); | ||
return factory.getSchema(jsonNode); | ||
} | ||
|
||
} | ||
``` | ||
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`. | ||
|
||
```java | ||
class Sample extends BaseJsonSchemaValidatorTest { | ||
|
||
void test() { | ||
JsonSchema schema = getJsonSchemaFromStringContent("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}"); | ||
JsonNode node = getJsonNodeFromStringContent("7"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
assertThat(errors.size(), is(1)); | ||
|
||
// With automatic version detection | ||
JsonNode schemaNode = getJsonNodeFromStringContent( | ||
"{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}"); | ||
JsonSchema schema = getJsonSchemaFromJsonNodeAutomaticVersion(schemaNode); | ||
|
||
schema.initializeValidators(); // by default all schemas are loaded lazily. You can load them eagerly via | ||
// initializeValidators() | ||
|
||
JsonNode node = getJsonNodeFromStringContent("{\"id\": \"2\"}"); | ||
Set<ValidationMessage> errors = schema.validate(node); | ||
assertThat(errors.size(), is(1)); | ||
JsonNode schemaNode = JsonMapperFactory.getInstance().readTree( | ||
"{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}"); | ||
/* | ||
* This should be cached for performance. | ||
* | ||
* Loading from a JsonNode is not recommended as there is no base IRI to use for | ||
* resolving relative $ref. | ||
* | ||
* Note that the V4 from the factory is the default version if $schema is not | ||
* specified. As $schema is specified in the data, V6 is used. | ||
*/ | ||
JsonSchema schemaFromNode = factory.getSchema(schemaNode); | ||
/* | ||
* By default all schemas are preloaded eagerly but ref resolve failures are not | ||
* thrown. You check if there are issues with ref resolving using | ||
* initializeValidators() | ||
*/ | ||
schemaFromNode.initializeValidators(); | ||
Set<ValidationMessage> errors = schemaFromNode.validate("{\"id\": \"2\"}", InputFormat.JSON); | ||
assertEquals(1, errors.size()); | ||
} | ||
|
||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.