From b4ada205c74606fafaf373cdc0116a984db14fe4 Mon Sep 17 00:00:00 2001
From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com>
Date: Tue, 26 Sep 2023 21:26:20 +0200
Subject: [PATCH] add support for prefixItems keyword
---
core/pom.xml | 1 +
.../applicator/PrefixItemsKeywordType.java | 99 ++++++++++++++
.../PrefixItemsKeywordTypeTest.java | 129 ++++++++++++++++++
3 files changed, 229 insertions(+)
create mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordType.java
create mode 100644 core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTypeTest.java
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/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/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)
+ );
+ }
+}