diff --git a/core/pom.xml b/core/pom.xml index b06a3ef3..3b9d48c8 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -117,6 +117,10 @@ **/tests/draft2019-09/additionalItems.json --> **/tests/draft2020-12/additionalProperties.json + **/tests/draft2020-12/boolean_schema.json **/tests/draft2020-12/const.json **/tests/draft2020-12/contains.json diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ArraySubSchemaKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/SchemaArrayKeywordType.java similarity index 92% rename from core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ArraySubSchemaKeywordType.java rename to core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/SchemaArrayKeywordType.java index 4436ae7f..9e0ff95d 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ArraySubSchemaKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/SchemaArrayKeywordType.java @@ -34,12 +34,12 @@ import java.util.Objects; import java.util.function.Function; -public final class ArraySubSchemaKeywordType implements KeywordType { +public final class SchemaArrayKeywordType implements KeywordType { private final String name; private final Function, Keyword> keywordCreator; - public ArraySubSchemaKeywordType(final String name, final Function, Keyword> keywordCreator) { + public SchemaArrayKeywordType(final String name, final Function, Keyword> keywordCreator) { this.name = Objects.requireNonNull(name); this.keywordCreator = Objects.requireNonNull(keywordCreator); } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeyword.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeyword.java new file mode 100644 index 00000000..b78dfe6e --- /dev/null +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeyword.java @@ -0,0 +1,70 @@ +/* + * 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 io.github.sebastiantoepfer.ddd.common.Media; +import io.github.sebastiantoepfer.jsonschema.JsonSchema; +import io.github.sebastiantoepfer.jsonschema.keyword.Applicator; +import jakarta.json.JsonValue; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +/** + * allOf : Array
+ * An instance validates successfully against this keyword if it validates successfully against all schemas defined by + * this keyword’s value.
+ *
+ * kind + *
    + *
  • Applicator
  • + *
+ * + * source: https://www.learnjsonschema.com/2020-12/applicator/allof/ + * spec: https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.1.1 + */ +final class AllOfKeyword implements Applicator { + + static final String NAME = "allOf"; + private final Collection schemas; + + public AllOfKeyword(final Collection schemas) { + this.schemas = List.copyOf(schemas); + } + + @Override + public boolean applyTo(final JsonValue instance) { + return schemas.stream().map(JsonSchema::validator).allMatch(v -> v.isValid(instance)); + } + + @Override + public boolean hasName(final String name) { + return Objects.equals(NAME, name); + } + + @Override + public > T printOn(final T media) { + return media.withValue(NAME, schemas); + } +} 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 ba3a0076..c62122df 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 @@ -25,8 +25,8 @@ import io.github.sebastiantoepfer.jsonschema.Vocabulary; import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectedByKeywordType; -import io.github.sebastiantoepfer.jsonschema.core.keyword.type.ArraySubSchemaKeywordType; import io.github.sebastiantoepfer.jsonschema.core.keyword.type.NamedJsonSchemaKeywordType; +import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SchemaArrayKeywordType; import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SubSchemaKeywordType; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.DefaultVocabulary; @@ -48,11 +48,12 @@ public final class ApplicatorVocabulary implements Vocabulary { public ApplicatorVocabulary() { this.vocab = new DefaultVocabulary( URI.create("https://json-schema.org/draft/2020-12/vocab/applicator"), + new SchemaArrayKeywordType(AllOfKeyword.NAME, AllOfKeyword::new), new NamedJsonSchemaKeywordType(PropertiesKeyword.NAME, PropertiesKeyword::new), 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 SchemaArrayKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new), //normally affeced by minContains and maxContains, but only min has a direct effect! new AffectedByKeywordType( ContainsKeyword.NAME, diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeywordTest.java new file mode 100644 index 00000000..940908cf --- /dev/null +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/AllOfKeywordTest.java @@ -0,0 +1,150 @@ +/* + * 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.hasEntry; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasKey; +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.SchemaArrayKeywordType; +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.junit.jupiter.api.Test; + +class AllOfKeywordTest { + + @Test + void should_know_his_name() { + final Keyword items = createKeywordFrom( + Json.createObjectBuilder().add("allOf", Json.createArrayBuilder().add(JsonValue.TRUE)).build() + ); + + assertThat(items.hasName("allOf"), is(true)); + assertThat(items.hasName("test"), is(false)); + } + + @Test + void should_be_printable() { + assertThat( + createKeywordFrom( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number")) + ) + ) + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18)) + ) + ) + ) + .build() + ).printOn(new HashMapMedia()), + (Matcher) hasEntry(is("allOf"), hasItem((hasKey("allOf")))) + ); + } + + @Test + void should_be_valid_if_all_schemas_applies() { + assertThat( + createKeywordFrom( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number")) + ) + ) + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18)) + ) + ) + ) + .build() + ) + .asApplicator() + .applyTo(Json.createValue(25)), + is(true) + ); + } + + @Test + void should_be_invalid_if_any_schemas_not_apply() { + assertThat( + createKeywordFrom( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder() + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("type", "number")) + ) + ) + .add( + Json.createObjectBuilder() + .add( + "allOf", + Json.createArrayBuilder().add(Json.createObjectBuilder().add("minimum", 18)) + ) + ) + ) + .build() + ) + .asApplicator() + .applyTo(Json.createValue(10)), + is(false) + ); + } + + private static Keyword createKeywordFrom(final JsonObject json) { + return new SchemaArrayKeywordType("allOf", AllOfKeyword::new).createKeyword( + new DefaultJsonSchemaFactory().create(json) + ); + } +} diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTest.java index b625575e..a433f374 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PrefixItemsKeywordTest.java @@ -32,7 +32,7 @@ import io.github.sebastiantoepfer.ddd.media.core.HashMapMedia; import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; -import io.github.sebastiantoepfer.jsonschema.core.keyword.type.ArraySubSchemaKeywordType; +import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SchemaArrayKeywordType; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; import jakarta.json.JsonObject; @@ -137,7 +137,7 @@ void should_be_printable() { } private static Keyword createKeywordFrom(final JsonObject json) { - return new ArraySubSchemaKeywordType("prefixItems", PrefixItemsKeyword::new).createKeyword( + return new SchemaArrayKeywordType("prefixItems", PrefixItemsKeyword::new).createKeyword( new DefaultJsonSchemaFactory().create(json) ); }