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/pom.xml b/core/pom.xml index 82fe7707..cd65e331 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -111,6 +111,7 @@ **/tests/draft2020-12/maximum.json **/tests/draft2020-12/exclusiveMaximum.json **/tests/draft2020-12/multipleOf.json + **/tests/draft2020-12/prefixItems.json 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/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java new file mode 100644 index 00000000..303e752c --- /dev/null +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java @@ -0,0 +1,99 @@ +/* + * The MIT License + * + * Copyright 2023 sebastian. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; + +import io.github.sebastiantoepfer.jsonschema.InstanceType; +import io.github.sebastiantoepfer.jsonschema.JsonSchema; +import io.github.sebastiantoepfer.jsonschema.JsonSchemas; +import io.github.sebastiantoepfer.jsonschema.keyword.Annotation; +import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; +import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import jakarta.json.Json; +import jakarta.json.JsonArray; +import jakarta.json.JsonValue; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +/** + * https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.1 + * https://www.learnjsonschema.com/2020-12/applicator/prefixitems/ + */ +final class PrefixItemsKeywordType implements KeywordType { + + @Override + public String name() { + return "prefixItems"; + } + + @Override + public Keyword createKeyword(final JsonSchema schema, final JsonValue value) { + if (InstanceType.ARRAY.isInstance(value)) { + return new PrefixItemsKeyword(value.asJsonArray()); + } else { + throw new IllegalArgumentException("must be a non empty array!"); + } + } + + private class PrefixItemsKeyword implements Annotation, Applicator { + + private final List schemas; + + public PrefixItemsKeyword(final JsonArray schemas) { + this.schemas = schemas.stream().map(JsonSchemas::load).toList(); + } + + @Override + public Collection categories() { + return List.of(KeywordCategory.ANNOTATION, KeywordCategory.APPLICATOR); + } + + @Override + public boolean hasName(final String name) { + return Objects.equals(name(), name); + } + + @Override + public JsonValue value() { + return Json.createValue(schemas.size() - 1); + } + + @Override + public boolean applyTo(final JsonValue instance) { + return !InstanceType.ARRAY.isInstance(instance) || matchesSchemas(instance.asJsonArray()); + } + + private boolean matchesSchemas(final JsonArray instance) { + boolean result = true; + for (int i = 0; i < Math.min(schemas.size(), instance.size()); i++) { + result &= schemas.get(i).validator().validate(instance.get(i)).isEmpty(); + if (!result) { + break; + } + } + return result; + } + } +} 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) + ); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java new file mode 100644 index 00000000..c1c2855f --- /dev/null +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java @@ -0,0 +1,129 @@ +/* + * The MIT License + * + * Copyright 2023 sebastian. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import io.github.sebastiantoepfer.jsonschema.JsonSchema; +import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; +import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; +import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; +import jakarta.json.Json; +import jakarta.json.JsonValue; +import org.junit.jupiter.api.Test; + +class PrefixItemsKeywordTypeTest { + + @Test + void should_know_his_name() { + final Keyword items = new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createArrayBuilder().add(JsonValue.TRUE).build() + ); + + assertThat(items.hasName("prefixItems"), is(true)); + assertThat(items.hasName("test"), is(false)); + } + + @Test + void should_not_be_createbale_from_non_array() { + final KeywordType keywordType = new PrefixItemsKeywordType(); + final JsonSchema schema = new DefaultJsonSchemaFactory().create(JsonValue.TRUE); + + assertThrows( + IllegalArgumentException.class, + () -> keywordType.createKeyword(schema, JsonValue.EMPTY_JSON_OBJECT) + ); + } + + @Test + void should_return_one_as_value() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createArrayBuilder().add(JsonValue.TRUE).build() + ) + .asAnnotation() + .value(), + is(Json.createValue(0)) + ); + } + + @Test + void should_be_valid_for_non_arrays() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createArrayBuilder().add(JsonValue.FALSE).build() + ) + .asApplicator() + .applyTo(Json.createValue(1)), + is(true) + ); + } + + @Test + void should_be_valid_if_first_item_match_schema() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createArrayBuilder().add(JsonValue.TRUE).build() + ) + .asApplicator() + .applyTo(Json.createArrayBuilder().add(1).build()), + is(true) + ); + } + + @Test + void should_be_invalid_if_second_item_does_not_match_schema() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createArrayBuilder().add(JsonValue.TRUE).add(JsonValue.FALSE).build() + ) + .asApplicator() + .applyTo(Json.createArrayBuilder().add(1).add(3).build()), + is(false) + ); + } + + @Test + void should_be_applicator_and_annotation() { + assertThat( + new PrefixItemsKeywordType() + .createKeyword(new DefaultJsonSchemaFactory().create(JsonValue.TRUE), JsonValue.EMPTY_JSON_ARRAY) + .categories(), + containsInAnyOrder(Keyword.KeywordCategory.APPLICATOR, Keyword.KeywordCategory.ANNOTATION) + ); + } +}