xref: /aosp_15_r20/external/json-schema-validator/doc/quickstart.md (revision 78c4dd6aa35290980cdcd1623a7e337e8d021c7c)
1## Quick Start
2
3To use the validator, we need to have the `JsonSchema` loaded and cached.
4
5For simplicity the following test loads a schema from a `String` or `JsonNode`. Note that loading a schema in this manner is not recommended as a relative `$ref` will not be properly resolved as there is no base IRI.
6
7The preferred method of loading a schema is by using a `SchemaLocation` and by configuring the appropriate `SchemaMapper` and `SchemaLoader` on the `JsonSchemaFactory`.
8
9```java
10package com.example;
11
12import static org.junit.jupiter.api.Assertions.assertEquals;
13
14import java.util.Set;
15
16import org.junit.jupiter.api.Test;
17
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonMappingException;
20import com.fasterxml.jackson.databind.JsonNode;
21import com.networknt.schema.*;
22import com.networknt.schema.serialization.JsonMapperFactory;
23
24public class SampleTest {
25    @Test
26    void schemaFromString() throws JsonMappingException, JsonProcessingException {
27        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
28        /*
29         * This should be cached for performance.
30         *
31         * Loading from a String is not recommended as there is no base IRI to use for
32         * resolving relative $ref.
33         */
34        JsonSchema schemaFromString = factory
35                .getSchema("{\"enum\":[1, 2, 3, 4],\"enumErrorCode\":\"Not in the list\"}");
36        Set<ValidationMessage> errors = schemaFromString.validate("7", InputFormat.JSON);
37        assertEquals(1, errors.size());
38    }
39
40    @Test
41    void schemaFromJsonNode() throws JsonMappingException, JsonProcessingException {
42        JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
43        JsonNode schemaNode = JsonMapperFactory.getInstance().readTree(
44                "{\"$schema\": \"http://json-schema.org/draft-06/schema#\", \"properties\": { \"id\": {\"type\": \"number\"}}}");
45        /*
46         * This should be cached for performance.
47         *
48         * Loading from a JsonNode is not recommended as there is no base IRI to use for
49         * resolving relative $ref.
50         *
51         * Note that the V4 from the factory is the default version if $schema is not
52         * specified. As $schema is specified in the data, V6 is used.
53         */
54        JsonSchema schemaFromNode = factory.getSchema(schemaNode);
55        /*
56         * By default all schemas are preloaded eagerly but ref resolve failures are not
57         * thrown. You check if there are issues with ref resolving using
58         * initializeValidators()
59         */
60        schemaFromNode.initializeValidators();
61        Set<ValidationMessage> errors = schemaFromNode.validate("{\"id\": \"2\"}", InputFormat.JSON);
62        assertEquals(1, errors.size());
63    }
64}
65```
66