|
| 1 | +package com.networknt.schema; |
| 2 | +import com.fasterxml.jackson.databind.JsonNode; |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.networknt.schema.JsonSchema; |
| 5 | +import com.networknt.schema.JsonSchemaFactory; |
| 6 | +import com.networknt.schema.SpecVersion; |
| 7 | +import com.networknt.schema.ValidationMessage; |
| 8 | +import com.networknt.schema.uri.URIFactory; |
| 9 | +import com.networknt.schema.uri.URIFetcher; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.net.URI; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +import static org.hamcrest.CoreMatchers.is; |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | + |
| 22 | +public class CustomUriTest { |
| 23 | + @Test |
| 24 | + public void customUri() throws Exception { |
| 25 | + /* Given */ |
| 26 | + final JsonSchemaFactory factory = buildJsonSchemaFactory(); |
| 27 | + final JsonSchema schema = factory.getSchema( |
| 28 | + "{\"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\"type\": \"object\",\"additionalProperties\": false,\"properties\": {\"customAnyOf\": {\"anyOf\": [{\"type\": \"null\"},{\"$ref\": \"custom:date\"}]},\"customOneOf\": {\"oneOf\": [{\"type\": \"null\"},{\"$ref\": \"custom:date\"}]}}}"); |
| 29 | + final ObjectMapper mapper = new ObjectMapper(); |
| 30 | + final JsonNode value = mapper.readTree("{\"customAnyOf\": null,\"customOneOf\": null}"); |
| 31 | + |
| 32 | + /* When */ |
| 33 | + final Set<ValidationMessage> errors = schema.validate(value); |
| 34 | + |
| 35 | + /* Then */ |
| 36 | + assertThat(errors.isEmpty(), is(true)); |
| 37 | + } |
| 38 | + |
| 39 | + private JsonSchemaFactory buildJsonSchemaFactory() { |
| 40 | + return JsonSchemaFactory.builder(JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V201909)) |
| 41 | + .uriFetcher(new CustomUriFetcher(), "custom").uriFactory(new CustomUriFactory(), "custom").build(); |
| 42 | + } |
| 43 | + |
| 44 | + private static class CustomUriFetcher implements URIFetcher { |
| 45 | + private static final String SCHEMA = "{\"$schema\": \"https://json-schema.org/draft/2019-09/schema\",\"$id\":\"custom:date\",\"type\":\"string\",\"format\":\"date\"}"; |
| 46 | + |
| 47 | + @Override |
| 48 | + public InputStream fetch(final URI uri) throws IOException { |
| 49 | + return new ByteArrayInputStream(SCHEMA.getBytes(StandardCharsets.UTF_8)); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static class CustomUriFactory implements URIFactory { |
| 54 | + @Override |
| 55 | + public URI create(final String uri) { |
| 56 | + return URI.create(uri); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public URI create(final URI baseURI, final String segment) { |
| 61 | + return baseURI.resolve(segment); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments