To use the validator, the Schema first needs to be loaded. For performance it is recommended that the Schema is cached.
The following examples demonstrate loading the Schema in the following manner.
SchemaLocationwith the value of the$idof the schema which is mapped using theSchemaIdResolversto the retrieval IRI which is on the classpathSchemaLocationwith the value of the$idof the schema where the content of the schema is supplied using theSchemaLoaderSchemaLocationwith the value of the retrieval IRI which is on the classpathStringwith the content of the schemaJsonNodewith the content of the schema
The preferred method of loading a schema is by using a SchemaLocation and by configuring the appropriate SchemaIdResolver and SchemaLoader on the SchemaRegistry. The SchemaMapper is use to map the $id to the retrieval IRI. The SchemaLoader is used to actually load the content of the schema.
Loading a schema from a String or JsonNode is not recommended as a relative $ref will not be properly resolved as there is no base IRI.
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.serialization.JsonMapperFactory;
/**
* Sample test.
*/
public class SampleTest {
@Test
void schemaFromSchemaLocationMapping() {
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
builder -> builder.schemaIdResolvers(schemaIdResolvers -> schemaIdResolvers
.mapPrefix("https://www.example.com/schema", "classpath:schema")));
/*
* This should be cached for performance.
*/
Schema schemaFromSchemaLocation = schemaRegistry
.getSchema(SchemaLocation.of("https://www.example.com/schema/example-ref.json"));
/*
* By default all schemas are preloaded eagerly but ref resolve failures are not
* thrown. You check if there are issues with ref resolving using
* initializeValidators()
*/
schemaFromSchemaLocation.initializeValidators();
List<Error> errors = schemaFromSchemaLocation.validate("{\"id\": \"2\"}", InputFormat.JSON,
executionContext -> executionContext
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
assertEquals(1, errors.size());
}
@Test
void schemaFromSchemaLocationContent() {
String schemaData = "{\"enum\":[1, 2, 3, 4]}";
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12,
builder -> builder.schemas(
Collections.singletonMap("https://www.example.com/schema/example-ref.json", schemaData)));
/*
* This should be cached for performance.
*/
Schema schemaFromSchemaLocation = schemaRegistry
.getSchema(SchemaLocation.of("https://www.example.com/schema/example-ref.json"));
/*
* By default all schemas are preloaded eagerly but ref resolve failures are not
* thrown. You check if there are issues with ref resolving using
* initializeValidators()
*/
schemaFromSchemaLocation.initializeValidators();
List<Error> errors = schemaFromSchemaLocation.validate("{\"id\": \"2\"}", InputFormat.JSON,
executionContext -> executionContext
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
assertEquals(1, errors.size());
}
@Test
void schemaFromClasspath() {
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
/*
* This should be cached for performance.
*
* Loading from using the retrieval IRI is not recommended as it may cause
* confusing when resolving relative $ref when $id is also used.
*/
Schema schemaFromClasspath = schemaRegistry.getSchema(SchemaLocation.of("classpath:schema/example-ref.json"));
/*
* By default all schemas are preloaded eagerly but ref resolve failures are not
* thrown. You check if there are issues with ref resolving using
* initializeValidators()
*/
schemaFromClasspath.initializeValidators();
List<Error> errors = schemaFromClasspath.validate("{\"id\": \"2\"}", InputFormat.JSON,
executionContext -> executionContext
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
assertEquals(1, errors.size());
}
@Test
void schemaFromString() {
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
/*
* This should be cached for performance.
*
* Loading from a String is not recommended as there is no base IRI to use for
* resolving relative $ref.
*/
Schema schemaFromString = schemaRegistry.getSchema("{\"enum\":[1, 2, 3, 4]}");
List<Error> errors = schemaFromString.validate("7", InputFormat.JSON, executionContext -> executionContext
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
assertEquals(1, errors.size());
}
@Test
void schemaFromJsonNode() throws JsonProcessingException {
SchemaRegistry schemaRegistry = SchemaRegistry.withDefaultDialect(SpecificationVersion.DRAFT_2020_12);
JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(
"{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}");
/*
* This should be cached for performance.
*
* Loading from a JsonNode is not recommended as there is no base IRI to use for
* resolving relative $ref.
*
* Note that the V202012 from the schemaRegistry is the default version if $schema is
* not specified. As $schema is specified in the data, V6 is used.
*/
Schema schemaFromNode = schemaRegistry.getSchema(schemaNode);
/*
* By default all schemas are preloaded eagerly but ref resolve failures are not
* thrown. You check if there are issues with ref resolving using
* initializeValidators()
*/
schemaFromNode.initializeValidators();
List<Error> errors = schemaFromNode.validate("{\"id\": \"2\"}", InputFormat.JSON,
executionContext -> executionContext
.executionConfig(executionConfig -> executionConfig.formatAssertionsEnabled(true)));
assertEquals(1, errors.size());
}
}