JAVA-13734 Update article Introduction to JSON Schema in Java (#12565)

* JAVA-13734 Update article Introduction to JSON Schema in Java

* JAVA-13734 Corrected the code formatting
This commit is contained in:
anuragkumawat 2022-08-16 22:16:34 +05:30 committed by GitHub
parent e96af43f40
commit 2122565082
2 changed files with 34 additions and 28 deletions

View File

@ -15,17 +15,11 @@
</parent>
<dependencies>
<dependency>
<groupId>org.everit.json</groupId>
<artifactId>org.everit.json.schema</artifactId>
<version>${everit.json.schema.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>json-schema-validator</artifactId>
<version>${networknt.json.schema.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
@ -72,7 +66,7 @@
</dependencies>
<properties>
<everit.json.schema.version>1.4.1</everit.json.schema.version>
<networknt.json.schema.version>1.0.72</networknt.json.schema.version>
<jsonb-api.version>1.0</jsonb-api.version>
<yasson.version>1.0.1</yasson.version>
<json.version>20211205</json.version>

View File

@ -1,32 +1,44 @@
package com.baeldung.json.schema;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.util.Set;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
public class JSONSchemaUnitTest {
@Test(expected = ValidationException.class)
public void givenInvalidInput_whenValidating_thenInvalid() {
private ObjectMapper mapper = new ObjectMapper();
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_invalid.json")));
@Test
public void givenInvalidInput_whenValidating_thenInvalid() throws IOException {
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
JsonSchema jsonSchema = factory.getSchema(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json"));
JsonNode jsonNode = mapper.readTree(JSONSchemaUnitTest.class.getResourceAsStream("/product_invalid.json"));
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
assertThat(errors).isNotEmpty()
.asString()
.contains("price: must have a minimum value of 0");
}
@Test
public void givenValidInput_whenValidating_thenValid() {
public void givenValidInput_whenValidating_thenValid() throws IOException {
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json")));
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json")));
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
JsonSchema jsonSchema = factory.getSchema(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json"));
Schema schema = SchemaLoader.load(jsonSchema);
schema.validate(jsonSubject);
JsonNode jsonNode = mapper.readTree(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json"));
Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
assertThat(errors).isEmpty();
}
}