diff --git a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java index 98ffbcb4..5d559418 100644 --- a/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java +++ b/api/src/main/java/io/github/sebastiantoepfer/jsonschema/JsonSchema.java @@ -23,8 +23,12 @@ */ package io.github.sebastiantoepfer.jsonschema; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; +import java.util.Optional; public interface JsonSchema extends JsonValue { public Validator validator(); + + Optional keywordByName(String name); } diff --git a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java index c294fd20..f909b7ae 100644 --- a/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java +++ b/api/src/test/java/io/github/sebastiantoepfer/jsonschema/FakeJsonSchemaFactory.java @@ -23,8 +23,10 @@ */ package io.github.sebastiantoepfer.jsonschema; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory; import jakarta.json.JsonValue; +import java.util.Optional; public final class FakeJsonSchemaFactory implements JsonSchemaFactory { @@ -40,6 +42,11 @@ public Validator validator() { public JsonValue.ValueType getValueType() { throw new UnsupportedOperationException("Not supported yet."); } + + @Override + public Optional keywordByName(String name) { + throw new UnsupportedOperationException("Not supported yet."); + } }; } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchema.java index b211f356..8160a61f 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchema.java @@ -40,9 +40,9 @@ import jakarta.json.JsonValue; import java.util.Collection; import java.util.List; -import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; +import java.util.stream.Stream; final class DefaultJsonSchema extends AbstractJsonValueSchema { @@ -55,9 +55,7 @@ public DefaultJsonSchema(final JsonObject value) { @Override public Validator validator() { - return asJsonObject() - .entrySet() - .stream() + return keywords() .map(this::asContraint) .flatMap(Optional::stream) .collect( @@ -65,6 +63,11 @@ public Validator validator() { ); } + @Override + public Optional keywordByName(final String name) { + return keywords().filter(k -> k.hasName(name)).findFirst(); + } + private Collection vocabulary() { return new KeywordSearch(new VocabularyKeywordType()) .searchForKeywordIn(this) @@ -75,8 +78,11 @@ private Collection vocabulary() { .toList(); } - private Optional> asContraint(final Entry property) { - final Keyword keyword = keywords.createKeywordFor(this, property); + private Stream keywords() { + return asJsonObject().entrySet().stream().map(property -> keywords.createKeywordFor(this, property)); + } + + private Optional> asContraint(final Keyword keyword) { final Constraint result; if (keyword.hasCategory(Keyword.KeywordCategory.ASSERTION)) { result = new AssertionConstraint(keyword.asAssertion()); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java index 17a2e73b..59d4a79b 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/EmptyJsonSchema.java @@ -25,7 +25,9 @@ import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.constraint.NoConstraint; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; +import java.util.Optional; final class EmptyJsonSchema extends AbstractJsonValueSchema { @@ -37,4 +39,9 @@ public EmptyJsonSchema() { public Validator validator() { return new DefaultValidator(new NoConstraint<>()); } + + @Override + public Optional keywordByName(final String name) { + return Optional.empty(); + } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java index eee71fe6..42b46b2c 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/FalseJsonSchema.java @@ -25,7 +25,9 @@ import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.constraint.UnfulfillableConstraint; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; +import java.util.Optional; final class FalseJsonSchema extends AbstractJsonValueSchema { @@ -37,4 +39,9 @@ public FalseJsonSchema() { public Validator validator() { return new DefaultValidator(new UnfulfillableConstraint<>()); } + + @Override + public Optional keywordByName(final String name) { + return Optional.empty(); + } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java index 8cc4b777..525f2154 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/TrueJsonSchema.java @@ -25,7 +25,9 @@ import io.github.sebastiantoepfer.jsonschema.Validator; import io.github.sebastiantoepfer.jsonschema.core.constraint.NoConstraint; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonValue; +import java.util.Optional; final class TrueJsonSchema extends AbstractJsonValueSchema { @@ -37,4 +39,9 @@ public TrueJsonSchema() { public Validator validator() { return new DefaultValidator(new NoConstraint<>()); } + + @Override + public Optional keywordByName(final String name) { + return Optional.empty(); + } } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java index ff10d6ee..856aad95 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java @@ -37,6 +37,7 @@ public ApplicatorVocabulary() { this.vocab = new DefaultVocabulary( URI.create("https://json-schema.org/draft/2020-12/vocab/applicator"), + new PrefixItemsKeywordType(), new ItemsKeywordType() ); } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java index 52b1a360..7635198b 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordType.java @@ -33,10 +33,12 @@ import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import jakarta.json.JsonArray; +import jakarta.json.JsonNumber; import jakarta.json.JsonValue; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Optional; final class ItemsKeywordType implements KeywordType { @@ -75,6 +77,11 @@ public ValueType getValueType() { return schema.getValueType(); } + @Override + public Optional keywordByName(final String name) { + return schema.keywordByName(name); + } + @Override public boolean hasName(final String name) { return Objects.equals(name(), name); @@ -87,7 +94,17 @@ public Collection categories() { @Override public JsonValue value() { - return JsonValue.TRUE; + final JsonValue result; + if (appliesToAny()) { + result = JsonValue.TRUE; + } else { + result = JsonValue.FALSE; + } + return result; + } + + private boolean appliesToAny() { + return startIndex() < 0; } @Override @@ -97,7 +114,18 @@ public boolean applyTo(final JsonValue instance) { private boolean matchesSchema(final JsonArray items) { final Validator itemValidator = validator(); - return items.stream().map(itemValidator::validate).allMatch(Collection::isEmpty); + return items.stream().skip(startIndex() + 1).map(itemValidator::validate).allMatch(Collection::isEmpty); + } + + private long startIndex() { + return owner() + .keywordByName("prefixItems") + .map(Keyword::asAnnotation) + .map(Annotation::value) + .filter(InstanceType.INTEGER::isInstance) + .map(JsonNumber.class::cast) + .map(JsonNumber::longValue) + .orElse(-1L); } } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java index b660641b..e8744b8c 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/AbstractJsonValueSchemaTest.java @@ -28,9 +28,11 @@ import static org.hamcrest.Matchers.is; import io.github.sebastiantoepfer.jsonschema.Validator; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.JsonArray; import jakarta.json.JsonObject; import jakarta.json.JsonValue; +import java.util.Optional; import org.junit.jupiter.api.Test; class AbstractJsonValueSchemaTest { @@ -65,5 +67,10 @@ public MyJsonValueSchema(JsonValue value) { public Validator validator() { throw new UnsupportedOperationException("Not supported yet."); } + + @Override + public Optional keywordByName(final String name) { + return Optional.empty(); + } } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchemaTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchemaTest.java index cd2ec22d..8dec8483 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchemaTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/DefaultJsonSchemaTest.java @@ -23,6 +23,8 @@ */ package io.github.sebastiantoepfer.jsonschema.core; +import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty; +import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; @@ -76,4 +78,38 @@ void should_not_be_loadable_without_mandantory_core_vocabulary() { Assertions.assertThrows(Exception.class, () -> new DefaultJsonSchema(invalidSchema)); } + + @Test + void should_find_keyword_by_name() { + assertThat( + new DefaultJsonSchema( + Json + .createObjectBuilder() + .add( + "$vocabulary", + Json.createObjectBuilder().add("https://json-schema.org/draft/2020-12/vocab/core", true) + ) + .build() + ) + .keywordByName("$vocabulary"), + isPresent() + ); + } + + @Test + void should_return_empty_for_non_existing_keyword() { + assertThat( + new DefaultJsonSchema( + Json + .createObjectBuilder() + .add( + "$vocabulary", + Json.createObjectBuilder().add("https://json-schema.org/draft/2020-12/vocab/core", true) + ) + .build() + ) + .keywordByName("type"), + isEmpty() + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordTypeTest.java index eca41eea..c224d091 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ItemsKeywordTypeTest.java @@ -23,6 +23,8 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; +import static com.github.npathai.hamcrestopt.OptionalMatchers.isEmpty; +import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; @@ -116,4 +118,79 @@ void should_produces_true_if_is_applied_to_any_instance() { is(JsonValue.TRUE) ); } + + @Test + void should_find_know_keyword() { + assertThat( + ((JsonSubSchema) new ItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createObjectBuilder().add("type", "string").build() + )).keywordByName("type"), + isPresent() + ); + } + + @Test + void should_retrun_empty_for_non_existing_keyword() { + assertThat( + ((JsonSubSchema) new ItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createObjectBuilder().add("type", "string").build() + )).keywordByName("properties"), + isEmpty() + ); + } + + @Test + void should_return_false_if_not_applies_to_any_item() { + assertThat( + new ItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json.createObjectBuilder().add("prefixItems", Json.createArrayBuilder().add(true)).build() + ), + JsonValue.FALSE + ) + .asAnnotation() + .value(), + is(JsonValue.FALSE) + ); + } + + @Test + void should_be_valid_if_invaliditem_is_already_checked_by_prefixItems() { + assertThat( + new ItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json.createObjectBuilder().add("prefixItems", Json.createArrayBuilder().add(true)).build() + ), + JsonValue.FALSE + ) + .asApplicator() + .applyTo(Json.createArrayBuilder().add("1").build()), + is(true) + ); + } + + @Test + void should_be_invalid_if_invaliditem_is_not_already_checked_by_prefixItems() { + assertThat( + new ItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory() + .create( + Json.createObjectBuilder().add("prefixItems", Json.createArrayBuilder().add(true)).build() + ), + JsonValue.FALSE + ) + .asApplicator() + .applyTo(Json.createArrayBuilder().add("1").add("2").build()), + is(false) + ); + } }