Skip to content

Commit

Permalink
Merge pull request #30 from sebastian-toepfer/annotation_for_keyword_…
Browse files Browse the repository at this point in the history
…patternproperties

add annotation for patternProerties keyword
  • Loading branch information
sebastian-toepfer authored Oct 9, 2023
2 parents c6789a0 + 89c87ef commit 92a6488
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Pattern, JsonValue> properties;

Expand Down Expand Up @@ -93,5 +97,16 @@ private boolean propertyMatches(final Map.Entry<String, JsonValue> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
);
}
}

0 comments on commit 92a6488

Please sign in to comment.