diff --git a/core/pom.xml b/core/pom.xml
index 068f980c..469017fd 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -119,6 +119,7 @@
**/tests/draft2020-12/additionalProperties.json
**/tests/draft2020-12/boolean_schema.json
**/tests/draft2020-12/const.json
+ **/tests/draft2020-12/contains.json
**/tests/draft2020-12/dependentRequired.json
**/tests/draft2020-12/enum.json
**/tests/draft2020-12/exclusiveMaximum.json
@@ -130,6 +131,9 @@
+
**/tests/draft2020-12/maxItems.json
**/tests/draft2020-12/maxLength.json
**/tests/draft2020-12/maximum.json
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 1690b2af..0c5b7e6c 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
@@ -50,7 +50,8 @@ public ApplicatorVocabulary() {
new SubSchemaKeywordType(AdditionalPropertiesKeyword.NAME, AdditionalPropertiesKeyword::new),
new NamedJsonSchemaKeywordType(PatternPropertiesKeyword.NAME, PatternPropertiesKeyword::new),
new SubSchemaKeywordType(ItemsKeyword.NAME, ItemsKeyword::new),
- new ArraySubSchemaKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new)
+ new ArraySubSchemaKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
+ new SubSchemaKeywordType(ContainsKeyword.NAME, ContainsKeyword::new)
);
}
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeyword.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeyword.java
new file mode 100644
index 00000000..dcb14df0
--- /dev/null
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeyword.java
@@ -0,0 +1,105 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2024 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 jakarta.json.stream.JsonCollectors.toJsonArray;
+
+import io.github.sebastiantoepfer.ddd.common.Media;
+import io.github.sebastiantoepfer.jsonschema.InstanceType;
+import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
+import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
+import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
+import jakarta.json.JsonArray;
+import jakarta.json.JsonValue;
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * contains : Schema
+ * Validation succeeds if the instance contains an element that validates against this schema.
+ *
+ *
+ * - applicator
+ * - annotation
+ *
+ *
+ * source: https://www.learnjsonschema.com/2020-12/applicator/contains/
+ * spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.3.1.3
+ */
+final class ContainsKeyword implements Applicator, Annotation {
+
+ static final String NAME = "contains";
+ private final JsonSubSchema contains;
+
+ public ContainsKeyword(final JsonSubSchema contains) {
+ this.contains = Objects.requireNonNull(contains);
+ }
+
+ @Override
+ public Collection categories() {
+ return List.of(KeywordCategory.APPLICATOR, KeywordCategory.ANNOTATION);
+ }
+
+ @Override
+ public boolean hasName(final String name) {
+ return Objects.equals(NAME, name);
+ }
+
+ @Override
+ public > T printOn(final T media) {
+ return media.withValue(NAME, contains);
+ }
+
+ @Override
+ public boolean applyTo(final JsonValue instance) {
+ return !InstanceType.ARRAY.isInstance(instance) || contains(instance.asJsonArray());
+ }
+
+ private boolean contains(final JsonArray array) {
+ return array.stream().anyMatch(contains.validator()::isValid);
+ }
+
+ @Override
+ public JsonValue valueFor(final JsonValue value) {
+ final JsonValue result;
+ if (InstanceType.ARRAY.isInstance(value)) {
+ result = valueFor(value.asJsonArray());
+ } else {
+ result = JsonValue.FALSE;
+ }
+ return result;
+ }
+
+ private JsonValue valueFor(final JsonArray values) {
+ final JsonValue result;
+ final JsonArray matchingItems = values.stream().filter(contains.validator()::isValid).collect(toJsonArray());
+ if (matchingItems.size() == values.size()) {
+ result = JsonValue.TRUE;
+ } else {
+ result = matchingItems;
+ }
+ return result;
+ }
+}
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordType.java
new file mode 100644
index 00000000..9b61deed
--- /dev/null
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordType.java
@@ -0,0 +1,116 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2024 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.validation;
+
+import io.github.sebastiantoepfer.ddd.common.Media;
+import io.github.sebastiantoepfer.jsonschema.InstanceType;
+import io.github.sebastiantoepfer.jsonschema.JsonSchema;
+import io.github.sebastiantoepfer.jsonschema.core.keyword.type.IntegerKeywordType;
+import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
+import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
+import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
+import jakarta.json.JsonArray;
+import jakarta.json.JsonValue;
+import jakarta.json.spi.JsonProvider;
+import java.math.BigInteger;
+import java.util.Objects;
+
+/**
+ * minProperties : Integer
+ * An object instance is valid if its number of properties is less than, or equal to, the value of this keyword.
+ * keyword.
+ *
+ *
+ *
+ * source: https://www.learnjsonschema.com/2020-12/validation/maxcontains/
+ * spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.4.4
+ */
+final class MaxContainsKeywordType implements KeywordType {
+
+ private final JsonProvider jsonContext;
+
+ public MaxContainsKeywordType(final JsonProvider jsonContext) {
+ this.jsonContext = jsonContext;
+ }
+
+ @Override
+ public String name() {
+ return "maxContains";
+ }
+
+ @Override
+ public Keyword createKeyword(final JsonSchema schema) {
+ return new IntegerKeywordType(
+ jsonContext,
+ name(),
+ value -> new MaxContainsKeyword(schema, value)
+ ).createKeyword(schema);
+ }
+
+ private final class MaxContainsKeyword implements Assertion {
+
+ private final JsonSchema owner;
+ private final BigInteger maxContains;
+
+ public MaxContainsKeyword(final JsonSchema owner, final BigInteger maxContains) {
+ this.owner = Objects.requireNonNull(owner);
+ this.maxContains = Objects.requireNonNull(maxContains);
+ }
+
+ @Override
+ public boolean hasName(final String name) {
+ return Objects.equals(name(), name);
+ }
+
+ @Override
+ public > T printOn(final T media) {
+ return media.withValue(name(), maxContains);
+ }
+
+ @Override
+ public boolean isValidFor(final JsonValue instance) {
+ return (
+ !InstanceType.ARRAY.isInstance(instance) ||
+ owner
+ .keywordByName("contains")
+ .map(Keyword::asAnnotation)
+ .map(annotation -> annotation.valueFor(instance))
+ .map(contains -> isValidFor(contains, instance.asJsonArray()))
+ .orElse(true)
+ );
+ }
+
+ private boolean isValidFor(final JsonValue containing, final JsonArray values) {
+ final boolean result;
+ if (JsonValue.TRUE.equals(containing)) {
+ result = values.size() <= maxContains.intValue();
+ } else {
+ result = containing.asJsonArray().size() <= maxContains.intValue();
+ }
+ return result;
+ }
+ }
+}
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java
index 5efe0009..926bf863 100644
--- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java
@@ -31,6 +31,18 @@
import java.math.BigInteger;
import java.util.Objects;
+/**
+ * minProperties : Integer
+ * An object instance is valid if its number of properties is less than, or equal to, the value of this keyword.
+ * keyword.
+ *
+ *
+ *
+ * source: https://www.learnjsonschema.com/2020-12/validation/minproperties/
+ * spec: https://json-schema.org/draft/2020-12/json-schema-validation.html#section-6.5.2
+ */
final class MinPropertiesKeyword implements Assertion {
static final String NAME = "minProperties";
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java
new file mode 100644
index 00000000..f307b6c8
--- /dev/null
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ContainsKeywordTest.java
@@ -0,0 +1,196 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2024 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.empty;
+import static org.hamcrest.Matchers.hasEntry;
+import static org.hamcrest.Matchers.is;
+
+import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
+import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
+import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SubSchemaKeywordType;
+import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
+import jakarta.json.Json;
+import jakarta.json.JsonObject;
+import jakarta.json.JsonValue;
+import org.hamcrest.Matcher;
+import org.hamcrest.Matchers;
+import org.junit.jupiter.api.Test;
+
+class ContainsKeywordTest {
+
+ @Test
+ void should_be_printable() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ ).printOn(new HashMapMedia()),
+ (Matcher) hasEntry(is("contains"), hasEntry(is("type"), is("number")))
+ );
+ }
+
+ @Test
+ void should_know_his_name() {
+ final Keyword enumKeyword = createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ );
+
+ assertThat(enumKeyword.hasName("contains"), is(true));
+ assertThat(enumKeyword.hasName("test"), is(false));
+ }
+
+ @Test
+ void should_be_an_applicator_and_annotation() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ ).categories(),
+ Matchers.containsInAnyOrder(Keyword.KeywordCategory.ANNOTATION, Keyword.KeywordCategory.APPLICATOR)
+ );
+ }
+
+ @Test
+ void should_apply_for_non_array() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asApplicator()
+ .applyTo(JsonValue.EMPTY_JSON_OBJECT),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_apply_if_one_item_applies() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asApplicator()
+ .applyTo(
+ Json.createArrayBuilder()
+ .add("foo")
+ .add(3)
+ .add(false)
+ .add(Json.createArrayBuilder().add("bar"))
+ .add(-5)
+ .build()
+ ),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_apply_if_all_item_applies() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "string")).build()
+ )
+ .asApplicator()
+ .applyTo(Json.createArrayBuilder().add("foo").add("bar").add("baz").build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_not_apply_if_non_item_applies() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asApplicator()
+ .applyTo(
+ Json.createArrayBuilder().add("foo").add(false).add(Json.createArrayBuilder().add("bar")).build()
+ ),
+ is(false)
+ );
+ }
+
+ @Test
+ void should_return_false_for_non_array() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asAnnotation()
+ .valueFor(JsonValue.EMPTY_JSON_OBJECT),
+ is(JsonValue.FALSE)
+ );
+ }
+
+ @Test
+ void should_return_empty_array_if_no_item_applies() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asAnnotation()
+ .valueFor(Json.createArrayBuilder().add("foo").build())
+ .asJsonArray(),
+ is(empty())
+ );
+ }
+
+ @Test
+ void should_return_matching_items() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "number")).build()
+ )
+ .asAnnotation()
+ .valueFor(
+ Json.createArrayBuilder()
+ .add("foo")
+ .add(3)
+ .add(false)
+ .add(Json.createArrayBuilder().add("bar"))
+ .add(-5)
+ .build()
+ )
+ .asJsonArray(),
+ containsInAnyOrder(Json.createValue(3), Json.createValue(-5))
+ );
+ }
+
+ @Test
+ void should_return_true_if_all_item_applies() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder().add("contains", Json.createObjectBuilder().add("type", "string")).build()
+ )
+ .asAnnotation()
+ .valueFor(Json.createArrayBuilder().add("foo").add("bar").add("baz").build()),
+ is(JsonValue.TRUE)
+ );
+ }
+
+ private static Keyword createKeywordFrom(final JsonObject json) {
+ return new SubSchemaKeywordType("contains", ContainsKeyword::new).createKeyword(
+ new DefaultJsonSchemaFactory().create(json)
+ );
+ }
+}
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordTypeTest.java
new file mode 100644
index 00000000..01882a5c
--- /dev/null
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MaxContainsKeywordTypeTest.java
@@ -0,0 +1,189 @@
+/*
+ * The MIT License
+ *
+ * Copyright 2024 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.validation;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasEntry;
+import static org.hamcrest.Matchers.is;
+
+import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia;
+import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
+import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
+import jakarta.json.Json;
+import jakarta.json.JsonObject;
+import jakarta.json.JsonValue;
+import jakarta.json.spi.JsonProvider;
+import java.math.BigInteger;
+import org.hamcrest.Matcher;
+import org.junit.jupiter.api.Test;
+
+class MaxContainsKeywordTypeTest {
+
+ @Test
+ void should_know_his_name() {
+ final Keyword enumKeyword = createKeywordFrom(Json.createObjectBuilder().add("maxContains", 2).build());
+
+ assertThat(enumKeyword.hasName("maxContains"), is(true));
+ assertThat(enumKeyword.hasName("test"), is(false));
+ }
+
+ @Test
+ void should_be_printable() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("maxContains", 2).build()).printOn(new HashMapMedia()),
+ (Matcher) hasEntry(is("maxContains"), is(BigInteger.valueOf(2)))
+ );
+ }
+
+ @Test
+ void should_be_valid_for_non_arrays() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("maxContains", 2).build())
+ .asAssertion()
+ .isValidFor(JsonValue.EMPTY_JSON_OBJECT),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_if_no_contains_is_present() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("maxContains", 2).build())
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_if_contains_applies_to_exact_count() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").add("bar").add(1).build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_if_contains_applies_to_less_items() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").add(2).add(3).build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_for_empty_arrays() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(JsonValue.EMPTY_JSON_ARRAY),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_invalid_if_contains_applies_to_more_items() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").add("bar").add(1).add("baz").build()),
+ is(false)
+ );
+ }
+
+ @Test
+ void should_be_valid_if_contains_applies_to_all_and_less_items_in_array() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_if_contains_applies_to_all_and_exact_items_count_in_array() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").add("bar").build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_invalid_if_contains_applies_to_all_and_more_items_in_array() {
+ assertThat(
+ createKeywordFrom(
+ Json.createObjectBuilder()
+ .add("contains", Json.createObjectBuilder().add("type", "string"))
+ .add("maxContains", 2)
+ .build()
+ )
+ .asAssertion()
+ .isValidFor(Json.createArrayBuilder().add("foo").add("bar").add("baz").build()),
+ is(false)
+ );
+ }
+
+ private static Keyword createKeywordFrom(final JsonObject json) {
+ return new MaxContainsKeywordType(JsonProvider.provider()).createKeyword(
+ new DefaultJsonSchemaFactory().create(json)
+ );
+ }
+}