Add JSON Schema validation

This commit is contained in:
Ivan Paolillo
2016-06-21 18:02:14 +02:00
parent 737848c9be
commit 3d0b89a66a
6 changed files with 139 additions and 0 deletions
@@ -0,0 +1,32 @@
package org.baeldung.json.schema;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;
public class JSONSchemaTest {
@Test
public void validateJSON() {
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product.json")));
Schema schema = SchemaLoader.load(jsonSchema);
try {
schema.validate(jsonSubject);
}
catch (ValidationException e) {
System.out.println(e.getMessage());
e.getCausingExceptions().stream().map(ValidationException::getMessage).forEach(System.out::println);
}
}
}