File tree Expand file tree Collapse file tree
java/org/baeldung/json/schema Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <project xmlns =" http://maven.apache.org/POM/4.0.0" xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2+ xsi : schemaLocation =" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <groupId >org.baeldung</groupId >
5+ <artifactId >json</artifactId >
6+ <version >0.0.1-SNAPSHOT</version >
7+
8+ <dependencies >
9+
10+ <dependency >
11+ <groupId >org.everit.json</groupId >
12+ <artifactId >org.everit.json.schema</artifactId >
13+ <version >1.3.0</version >
14+ </dependency >
15+
16+ <dependency >
17+ <groupId >junit</groupId >
18+ <artifactId >junit</artifactId >
19+ <version >4.12</version >
20+ <scope >test</scope >
21+ </dependency >
22+
23+ </dependencies >
24+
25+ </project >
Original file line number Diff line number Diff line change 1+ package org .baeldung .json .schema ;
2+
3+ import org .everit .json .schema .Schema ;
4+ import org .everit .json .schema .loader .SchemaLoader ;
5+
6+ import org .json .JSONObject ;
7+ import org .json .JSONTokener ;
8+ import org .junit .Test ;
9+
10+ public class JSONSchemaTest {
11+
12+ @ Test
13+ public void givenInvalidInput_whenValidating_thenInvalid () {
14+
15+ JSONObject jsonSchema = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/schema.json" )));
16+ JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product_invalid.json" )));
17+
18+ Schema schema = SchemaLoader .load (jsonSchema );
19+ schema .validate (jsonSubject );
20+ }
21+
22+ @ Test
23+ public void givenValidInput_whenValidating_thenValid () {
24+
25+ JSONObject jsonSchema = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/schema.json" )));
26+ JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product_valid.json" )));
27+
28+ Schema schema = SchemaLoader .load (jsonSchema );
29+ schema .validate (jsonSubject );
30+ }
31+ }
Original file line number Diff line number Diff line change 1+ {
2+ "id" : 1 ,
3+ "name" : " Lampshade" ,
4+ "price" : 0
5+ }
Original file line number Diff line number Diff line change 1+ {
2+ "id" : 1 ,
3+ "name" : " Lampshade" ,
4+ "price" : 10
5+ }
Original file line number Diff line number Diff line change 1+ {
2+ "$schema" : " http://json-schema.org/draft-04/schema#" ,
3+ "title" : " Product" ,
4+ "description" : " A product from the catalog" ,
5+ "type" : " object" ,
6+ "properties" : {
7+ "id" : {
8+ "description" : " The unique identifier for a product" ,
9+ "type" : " integer"
10+ },
11+ "name" : {
12+ "description" : " Name of the product" ,
13+ "type" : " string"
14+ },
15+ "price" : {
16+ "type" : " number" ,
17+ "minimum" : 0 ,
18+ "exclusiveMinimum" : true
19+ }
20+ },
21+ "required" : [" id" , " name" , " price" ]
22+ }
You can’t perform that action at this time.
0 commit comments