From 17687fd199fcdf479c88fdf61a6c980282ddf343 Mon Sep 17 00:00:00 2001
From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com>
Date: Wed, 29 May 2024 22:35:04 +0200
Subject: [PATCH] add support for minProperties keyword
---
core/pom.xml | 1 +
.../validation/MinPropertiesKeyword.java | 61 ++++++++++
.../validation/ValidationVocabulary.java | 1 +
.../validation/MinPropertiesKeywordTest.java | 107 ++++++++++++++++++
4 files changed, 170 insertions(+)
create mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java
create mode 100644 core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeywordTest.java
diff --git a/core/pom.xml b/core/pom.xml
index 70740433..068f980c 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -137,6 +137,7 @@
**/tests/draft2020-12/minItems.json
**/tests/draft2020-12/minLength.json
**/tests/draft2020-12/minimum.json
+ **/tests/draft2020-12/minProperties.json
**/tests/draft2020-12/multipleOf.json
**/tests/draft2020-12/pattern.json
**/tests/draft2020-12/patternProperties.json
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
new file mode 100644
index 00000000..5efe0009
--- /dev/null
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeyword.java
@@ -0,0 +1,61 @@
+/*
+ * 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.keyword.Assertion;
+import jakarta.json.JsonObject;
+import jakarta.json.JsonValue;
+import java.math.BigInteger;
+import java.util.Objects;
+
+final class MinPropertiesKeyword implements Assertion {
+
+ static final String NAME = "minProperties";
+ private final BigInteger minProperties;
+
+ public MinPropertiesKeyword(final BigInteger minProperties) {
+ this.minProperties = Objects.requireNonNull(minProperties);
+ }
+
+ @Override
+ public boolean hasName(final String name) {
+ return Objects.equals(NAME, name);
+ }
+
+ @Override
+ public > T printOn(final T media) {
+ return media.withValue(NAME, minProperties);
+ }
+
+ @Override
+ public boolean isValidFor(final JsonValue instance) {
+ return !InstanceType.OBJECT.isInstance(instance) || hasMinProperties(instance.asJsonObject());
+ }
+
+ private boolean hasMinProperties(final JsonObject object) {
+ return object.keySet().size() >= minProperties.intValue();
+ }
+}
diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java
index 8f7f6cc2..d8914192 100644
--- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java
+++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/ValidationVocabulary.java
@@ -50,6 +50,7 @@ public ValidationVocabulary(final JsonProvider jsonContext) {
new ArrayKeywordType(EnumKeyword.NAME, EnumKeyword::new),
new ObjectKeywordType(DependentRequiredKeyword.NAME, DependentRequiredKeyword::new),
new IntegerKeywordType(jsonContext, MaxPropertiesKeyword.NAME, MaxPropertiesKeyword::new),
+ new IntegerKeywordType(jsonContext, MinPropertiesKeyword.NAME, MinPropertiesKeyword::new),
new StringKeywordType(jsonContext, PatternKeyword.NAME, PatternKeyword::new),
new IntegerKeywordType(jsonContext, MinLengthKeyword.NAME, MinLengthKeyword::new),
new IntegerKeywordType(jsonContext, MaxLengthKeyword.NAME, MaxLengthKeyword::new),
diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeywordTest.java
new file mode 100644
index 00000000..9f7597af
--- /dev/null
+++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/validation/MinPropertiesKeywordTest.java
@@ -0,0 +1,107 @@
+/*
+ * 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.core.keyword.type.IntegerKeywordType;
+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 MinPropertiesKeywordTest {
+
+ @Test
+ void should_know_his_name() {
+ final Keyword enumKeyword = createKeywordFrom(Json.createObjectBuilder().add("minProperties", 1).build());
+
+ assertThat(enumKeyword.hasName("minProperties"), is(true));
+ assertThat(enumKeyword.hasName("test"), is(false));
+ }
+
+ @Test
+ void should_be_printable() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("minProperties", 1).build()).printOn(new HashMapMedia()),
+ (Matcher) hasEntry(is("minProperties"), is(BigInteger.valueOf(1)))
+ );
+ }
+
+ @Test
+ void should_be_valid_for_non_objects() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("minProperties", 1).build())
+ .asAssertion()
+ .isValidFor(JsonValue.EMPTY_JSON_ARRAY),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_for_excat_properties_count() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("minProperties", 1).build())
+ .asAssertion()
+ .isValidFor(Json.createObjectBuilder().add("foo", 1).build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_valid_for_more_properties() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("minProperties", 1).build())
+ .asAssertion()
+ .isValidFor(Json.createObjectBuilder().add("foo", 1).add("bar", "hi").build()),
+ is(true)
+ );
+ }
+
+ @Test
+ void should_be_invalid_for_less_properties() {
+ assertThat(
+ createKeywordFrom(Json.createObjectBuilder().add("minProperties", 2).build())
+ .asAssertion()
+ .isValidFor(Json.createObjectBuilder().add("foo", 1).build()),
+ is(false)
+ );
+ }
+
+ private static Keyword createKeywordFrom(final JsonObject json) {
+ return new IntegerKeywordType(
+ JsonProvider.provider(),
+ "minProperties",
+ MinPropertiesKeyword::new
+ ).createKeyword(new DefaultJsonSchemaFactory().create(json));
+ }
+}