From a685c81b5d0f832a4a5530d63b80baa0c966fc95 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:11:29 +0200 Subject: [PATCH] improve keyword --- .../core/vocab/core/DefsKeywordType.java | 25 +++++++++++++++++-- .../core/vocab/core/DefsKeywordTypeTest.java | 22 ++++++++++------ 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java index dfea7f9f..a640231e 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordType.java @@ -23,11 +23,14 @@ */ 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.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import io.github.sebastiantoepfer.jsonschema.keyword.ReservedLocation; +import jakarta.json.JsonObject; import jakarta.json.JsonValue; +import java.util.Objects; /** * @@ -42,6 +45,24 @@ public String name() { @Override public Keyword createKeyword(final JsonSchema schema, final JsonValue value) { - return new DefaultAnnotation(name(), value); + if (InstanceType.OBJECT.isInstance(value)) { + return new DefsKeyword(value.asJsonObject()); + } else { + throw new IllegalArgumentException("must be an object!"); + } + } + + private class DefsKeyword implements ReservedLocation { + + private final JsonObject defs; + + private DefsKeyword(final JsonObject defs) { + this.defs = defs; + } + + @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/DefsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java index 10131d5b..1343f69e 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/core/DefsKeywordTypeTest.java @@ -25,20 +25,28 @@ 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 io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; import org.junit.jupiter.api.Test; class DefsKeywordTypeTest { @Test - void should_create_keyword_with_name() { - assertThat( - new DefsKeywordType() - .createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT) - .hasName("$defs"), - is(true) - ); + void should_not_be_creatable_from_non_objects() { + final DefsKeywordType schema = new DefsKeywordType(); + + assertThrows(IllegalArgumentException.class, () -> schema.createKeyword(null, JsonValue.FALSE)); + } + + @Test + void should_know_his_name() { + final Keyword defs = new DefsKeywordType() + .createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_OBJECT); + + assertThat(defs.hasName("$defs"), is(true)); + assertThat(defs.hasName("test"), is(false)); } }