Skip to content

Instantly share code, notes, and snippets.

@darichey
Last active June 10, 2019 01:49
Show Gist options
  • Save darichey/90baa0c9c8430063373967c4f0ae34da to your computer and use it in GitHub Desktop.
Save darichey/90baa0c9c8430063373967c4f0ae34da to your computer and use it in GitHub Desktop.
Justify reference issue
{
"baz": {
"qux": 3
}
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "bar.schema.json",
"type": "object",
"properties": {
"baz": {
"$ref": "baz.schema.json"
}
}
}
{
"qux": 3
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "bar.schema.json",
"type": "object",
"properties": {
"qux": {
"type": "integer"
}
},
"required": ["qux"]
}
{
"bar": {
"baz": {
"qux": 3
}
}
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "foo.schema.json",
"type": "object",
"properties": {
"bar": {
"$ref": "bar.schema.json"
}
}
}
import org.leadpony.justify.api.*;
import javax.json.JsonReader;
import javax.json.JsonValue;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test {
public static void main(String[] args) {
test("baz");
test("bar");
test("foo");
}
private static final JsonValidationService service = JsonValidationService.newInstance();
private static void test(String name) {
JsonSchema schema = readSchema(Paths.get("schema", name + ".schema.json"));
ProblemHandler problemHandler = service.createProblemPrinter(System.out::println);
Path instance = Paths.get("sample", name + ".json");
try (JsonReader reader = service.createReader(instance, schema, problemHandler)) {
JsonValue value = reader.readValue();
System.out.println(value);
}
}
private static JsonSchema readSchema(Path path) {
JsonSchemaReaderFactory factory = service.createSchemaReaderFactoryBuilder()
.withSchemaResolver(id -> service.readSchema(Paths.get("schema", id.getPath())))
.build();
try (JsonSchemaReader reader = factory.createSchemaReader(path)) {
return reader.read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment