From aae3944e7f1888fe4c69f2db392d7168706c02c2 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Tue, 3 Oct 2023 23:32:12 +0200 Subject: [PATCH] improve id keyword --- .../core/vocab/core/IdKeywordType.java | 31 +++++++++++++++++-- .../core/vocab/core/IdKeywordTypeTest.java | 26 +++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java index 67b693b0..59674c01 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordType.java @@ -23,11 +23,15 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.core; +import io.github.sebastiantoepfer.jsonschema.InstanceType; import io.github.sebastiantoepfer.jsonschema.JsonSchema; -import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation; +import io.github.sebastiantoepfer.jsonschema.keyword.Identifier; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import jakarta.json.JsonString; import jakarta.json.JsonValue; +import java.net.URI; +import java.util.Objects; /** * @@ -42,6 +46,29 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema, final JsonValue value) { - return new DefaultAnnotation(name(), value); + if (InstanceType.STRING.isInstance(value)) { + return new IdKeyword((JsonString) value); + } else { + throw new IllegalArgumentException("must be a string!"); + } + } + + private class IdKeyword implements Identifier { + + private final URI uri; + + private IdKeyword(final JsonString uri) { + this.uri = URI.create(uri.getString()); + } + + @Override + public URI asUri() { + return uri; + } + + @Override + public boolean hasName(final String name) { + return Objects.equals(name(), name); + } } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java index e9a76126..7cbd0c2c 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/IdKeywordTypeTest.java @@ -25,20 +25,44 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; +import jakarta.json.Json; import jakarta.json.JsonValue; +import java.net.URI; import org.junit.jupiter.api.Test; class IdKeywordTypeTest { + @Test + void should_not_be_creatable_from_non_string() { + final IdKeywordType schema = new IdKeywordType(); + + assertThrows(IllegalArgumentException.class, () -> schema.createKeyword(null, JsonValue.FALSE)); + } + @Test void should_create_keyword_with_name() { assertThat( new IdKeywordType() - .createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT) + .createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), Json.createValue("/test")) .hasName("$id"), is(true) ); } + + @Test + void should_retun_his_uri() { + assertThat( + new IdKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createValue("https://json-schema.org/draft/2020-12/schema") + ) + .asIdentifier() + .asUri(), + is(URI.create("https://json-schema.org/draft/2020-12/schema")) + ); + } }