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

View File

@ -1,32 +1,44 @@
package com.baeldung.json.schema; package com.baeldung.json.schema;
import org.everit.json.schema.Schema; import static org.assertj.core.api.Assertions.assertThat;
import org.everit.json.schema.ValidationException;
import org.everit.json.schema.loader.SchemaLoader; import java.io.IOException;
import java.util.Set;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Test; 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 { public class JSONSchemaUnitTest {
@Test(expected = ValidationException.class) private ObjectMapper mapper = new ObjectMapper();
public void givenInvalidInput_whenValidating_thenInvalid() {
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json"))); @Test
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_invalid.json"))); public void givenInvalidInput_whenValidating_thenInvalid() throws IOException {
Schema schema = SchemaLoader.load(jsonSchema); JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
schema.validate(jsonSubject); 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 @Test
public void givenValidInput_whenValidating_thenValid() { public void givenValidInput_whenValidating_thenValid() throws IOException {
JSONObject jsonSchema = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json"))); JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
JSONObject jsonSubject = new JSONObject(new JSONTokener(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json"))); JsonSchema jsonSchema = factory.getSchema(JSONSchemaUnitTest.class.getResourceAsStream("/schema.json"));
Schema schema = SchemaLoader.load(jsonSchema); JsonNode jsonNode = mapper.readTree(JSONSchemaUnitTest.class.getResourceAsStream("/product_valid.json"));
schema.validate(jsonSubject); Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
assertThat(errors).isEmpty();
} }
} }