commit
ed3e1f4553
|
@ -0,0 +1,25 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.everit.json</groupId>
|
||||
<artifactId>org.everit.json.schema</artifactId>
|
||||
<version>1.3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,31 @@
|
|||
package org.baeldung.json.schema;
|
||||
|
||||
import org.everit.json.schema.Schema;
|
||||
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 givenInvalidInput_whenValidating_thenInvalid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_invalid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidInput_whenValidating_thenValid() {
|
||||
|
||||
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/schema.json")));
|
||||
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaTest.class.getResourceAsStream("/product_valid.json")));
|
||||
|
||||
Schema schema = SchemaLoader.load(jsonSchema);
|
||||
schema.validate(jsonSubject);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"id": 1,
|
||||
"name": "Lampshade",
|
||||
"price": 0
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"id": 1,
|
||||
"name": "Lampshade",
|
||||
"price": 10
|
||||
}
|
|
@ -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"]
|
||||
}
|
Loading…
Reference in New Issue