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 1111public class JSONSchemaTest {
1212
1313 @ Test
14- public void validateJSON () {
14+ public void givenInvalidInput_whenValidating_thenInvalid () {
1515
1616 JSONObject jsonSchema = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/schema.json" )));
17- JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product.json" )));
17+ JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product_invalid.json" )));
18+
19+ Schema schema = SchemaLoader .load (jsonSchema );
20+
21+ try {
22+
23+ schema .validate (jsonSubject );
24+ }
25+
26+ catch (ValidationException e ) {
27+
28+ System .out .println (e .getMessage ());
29+ e .getCausingExceptions ().stream ().map (ValidationException ::getMessage ).forEach (System .out ::println );
30+ }
31+ }
32+
33+ @ Test
34+ public void givenValidInput_whenValidating_thenValid () {
35+
36+ JSONObject jsonSchema = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/schema.json" )));
37+ JSONObject jsonSubject = new JSONObject (new JSONTokener (JSONSchemaTest .class .getResourceAsStream ("/product_valid.json" )));
1838
1939 Schema schema = SchemaLoader .load (jsonSchema );
2040
Original file line number Diff line number Diff line change 11{
22 "id" : 1 ,
33 "name" : " Lampshade" ,
4- "price" : 0
4+ "price" : 10
55}
You can’t perform that action at this time.
0 commit comments