From 89c87effe9f3c1d8f6f497cc925e300888681665 Mon Sep 17 00:00:00 2001 From: Sebastian Toepfer <61313468+sebastian-toepfer@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:38:19 +0200 Subject: [PATCH] add annotation for patternProerties keyword --- .../PatternPropertiesKeywordType.java | 17 ++++++++++- .../PatternPropertiesKeywordTypeTest.java | 28 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java index 642707b3..d68f27f7 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordType.java @@ -23,12 +23,16 @@ */ package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator; +import static jakarta.json.stream.JsonCollectors.toJsonArray; + 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.JsonObject; import jakarta.json.JsonValue; import java.util.Collection; @@ -50,7 +54,7 @@ public Keyword createKeyword(final JsonSchema schema, final JsonValue value) { return new PatternPropertiesKeyword(value.asJsonObject()); } - private class PatternPropertiesKeyword implements Applicator { + private class PatternPropertiesKeyword implements Applicator, Annotation { private final Map properties; @@ -93,5 +97,16 @@ private boolean propertyMatches(final Map.Entry property) { .map(schema -> schema.validator().validate(property.getValue()).isEmpty()) .orElse(true); } + + @Override + public JsonValue valueFor(final JsonValue value) { + return value + .asJsonObject() + .keySet() + .stream() + .filter(name -> properties.keySet().stream().anyMatch(p -> p.matcher(name).find())) + .map(Json::createValue) + .collect(toJsonArray()); + } } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java index f9fcadd1..aa6c7f8e 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/PatternPropertiesKeywordTypeTest.java @@ -30,7 +30,9 @@ import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory; import io.github.sebastiantoepfer.jsonschema.keyword.Keyword; import jakarta.json.Json; +import jakarta.json.JsonString; import jakarta.json.JsonValue; +import java.math.BigDecimal; import org.junit.jupiter.api.Test; class PatternPropertiesKeywordTypeTest { @@ -117,4 +119,30 @@ void should_be_valid_if_properties_not_covered() { is(true) ); } + + @Test + void should_return_the_matching_property_names() { + assertThat( + new PatternPropertiesKeywordType() + .createKeyword( + new DefaultJsonSchemaFactory().create(JsonValue.TRUE), + Json.createObjectBuilder().add("f.o", JsonValue.TRUE).build() + ) + .asAnnotation() + .valueFor( + Json + .createObjectBuilder() + .add("foo", BigDecimal.ONE) + .add("test", BigDecimal.ONE) + .add("fao", 1) + .build() + ) + .asJsonArray() + .stream() + .map(JsonString.class::cast) + .map(JsonString::getString) + .toList(), + containsInAnyOrder("foo", "fao") + ); + } }