Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add annotation for patternProerties keyword #30

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
);
}
}