From 59c085222c64ac74ccc3f40d25c1621b636b81ef Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Thu, 18 Jan 2024 19:56:57 +0100 Subject: [PATCH] smaller refactoring --- .../codition/MappingConditionAdapter.java | 41 +++++++++++++++++++ .../keyword/type/StringArrayKeywordType.java | 8 ++-- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/MappingConditionAdapter.java diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/MappingConditionAdapter.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/MappingConditionAdapter.java new file mode 100644 index 00000000..22b86f24 --- /dev/null +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/codition/MappingConditionAdapter.java @@ -0,0 +1,41 @@ +/* + * 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.codition; + +import java.util.Objects; +import java.util.function.Function; + +public final class MappingConditionAdapter implements Condition { + + private final Function check; + + public MappingConditionAdapter(final Condition condition, final Function map) { + this.check = Objects.requireNonNull(map).andThen(condition::isFulfilledBy); + } + + @Override + public boolean isFulfilledBy(final T value) { + return check.apply(value); + } +} diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/StringArrayKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/StringArrayKeywordType.java index e8734081..e70da5f6 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/StringArrayKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/StringArrayKeywordType.java @@ -31,6 +31,7 @@ import io.github.sebastiantoepfer.jsonschema.core.codition.AllOfCondition; import io.github.sebastiantoepfer.jsonschema.core.codition.CollectionElementsCondtion; import io.github.sebastiantoepfer.jsonschema.core.codition.JsonPropertyCondition; +import io.github.sebastiantoepfer.jsonschema.core.codition.MappingConditionAdapter; import io.github.sebastiantoepfer.jsonschema.core.codition.OfTypeCondition; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType; @@ -74,9 +75,10 @@ private Keyword createKeyword(final JsonObject schema) { jsonContext.createPointer(String.format("/%s", name)), new AllOfCondition<>( new OfTypeCondition(InstanceType.ARRAY), - json -> - new CollectionElementsCondtion(new OfTypeCondition(InstanceType.STRING)) - .isFulfilledBy(json.asJsonArray()) + new MappingConditionAdapter<>( + new CollectionElementsCondtion<>(new OfTypeCondition(InstanceType.STRING)), + JsonValue::asJsonArray + ) ) ) .isFulfilledBy(schema)