From 3d0b89a66a8642c162500f5deac62ec091d3635a Mon Sep 17 00:00:00 2001 From: Ivan Paolillo Date: Tue, 21 Jun 2016 18:02:14 +0200 Subject: [PATCH] Add JSON Schema validation --- json/.classpath | 32 +++++++++++++++++++ json/.project | 23 +++++++++++++ json/pom.xml | 25 +++++++++++++++ .../baeldung/json/schema/JSONSchemaTest.java | 32 +++++++++++++++++++ json/src/test/resources/product.json | 5 +++ json/src/test/resources/schema.json | 22 +++++++++++++ 6 files changed, 139 insertions(+) create mode 100644 json/.classpath create mode 100644 json/.project create mode 100644 json/pom.xml create mode 100644 json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java create mode 100644 json/src/test/resources/product.json create mode 100644 json/src/test/resources/schema.json diff --git a/json/.classpath b/json/.classpath new file mode 100644 index 0000000000..4f9b9b5af7 --- /dev/null +++ b/json/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/json/.project b/json/.project new file mode 100644 index 0000000000..8424469786 --- /dev/null +++ b/json/.project @@ -0,0 +1,23 @@ + + + json + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/json/pom.xml b/json/pom.xml new file mode 100644 index 0000000000..47f1f25aa2 --- /dev/null +++ b/json/pom.xml @@ -0,0 +1,25 @@ + + 4.0.0 + org.baeldung + json + 0.0.1-SNAPSHOT + + + + + org.everit.json + org.everit.json.schema + 1.3.0 + + + + junit + junit + 4.12 + test + + + + + diff --git a/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java b/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java new file mode 100644 index 0000000000..5e8f024ac3 --- /dev/null +++ b/json/src/test/java/org/baeldung/json/schema/JSONSchemaTest.java @@ -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); + } + } +} diff --git a/json/src/test/resources/product.json b/json/src/test/resources/product.json new file mode 100644 index 0000000000..7c55d8c7a5 --- /dev/null +++ b/json/src/test/resources/product.json @@ -0,0 +1,5 @@ +{ + "id": 1, + "name": "Lampshade", + "price": 0 +} diff --git a/json/src/test/resources/schema.json b/json/src/test/resources/schema.json new file mode 100644 index 0000000000..7cf99d76e0 --- /dev/null +++ b/json/src/test/resources/schema.json @@ -0,0 +1,22 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Product", + "description": "A product from the catalog", + "type": "object", + "properties": { + "id": { + "description": "The unique identifier for a product", + "type": "integer" + }, + "name": { + "description": "Name of the product", + "type": "string" + }, + "price": { + "type": "number", + "minimum": 0, + "exclusiveMinimum": true + } + }, + "required": ["id", "name", "price"] +}