Skip to content

Commit

Permalink
improve creation of properties keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Dec 3, 2023
1 parent 4c70470 commit bef87a4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator;

import static jakarta.json.stream.JsonCollectors.toJsonArray;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toMap;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Annotation;
Expand All @@ -52,17 +53,28 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return new PropertiesKeyword(schema, schema.asJsonObject().getJsonObject(name()));
final DefaultJsonSchemaFactory factory = new DefaultJsonSchemaFactory();
return schema
.asJsonObject()
.getJsonObject(name())
.entrySet()
.stream()
.map(entry ->
Map.entry(
entry.getKey(),
factory.tryToCreateSchemaFrom(entry.getValue()).orElseThrow(IllegalArgumentException::new)
)
)
.map(entry -> Map.entry(entry.getKey(), new DefaultJsonSubSchema(schema, entry.getValue())))
.collect(collectingAndThen(toMap(Map.Entry::getKey, Map.Entry::getValue), PropertiesKeyword::new));
}

private class PropertiesKeyword implements Applicator, Annotation {

private final JsonSchema schema;
private final JsonObject schemas;
private final Map<String, JsonSchema> schemas;

public PropertiesKeyword(final JsonSchema schema, final JsonObject schemas) {
this.schema = schema;
this.schemas = schemas;
public PropertiesKeyword(final Map<String, JsonSchema> schemas) {
this.schemas = Map.copyOf(schemas);
}

@Override
Expand All @@ -87,25 +99,18 @@ private boolean propertiesMatches(final JsonObject instance) {
private boolean propertyMatches(final Map.Entry<String, JsonValue> property) {
return Optional
.ofNullable(schemas.get(property.getKey()))
.flatMap(this::toSubSchema)
.map(JsonSchema::validator)
.map(validator -> validator.isValid(property.getValue()))
.orElse(true);
}

private Optional<JsonSubSchema> toSubSchema(final JsonValue value) {
return new DefaultJsonSchemaFactory()
.tryToCreateSchemaFrom(value)
.map(subSchema -> new DefaultJsonSubSchema(schema, subSchema));
}

@Override
public JsonValue valueFor(final JsonValue instance) {
return instance
.asJsonObject()
.keySet()
.stream()
.filter(schemas.asJsonObject()::containsKey)
.filter(schemas::containsKey)
.map(Json::createValue)
.collect(toJsonArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,70 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.DefaultJsonSchemaFactory;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.Json;
import jakarta.json.JsonValue;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

class PropertiesKeywordTypeTest {

@Test
void should_not_be_createable_with_array_in_schemas() {
final JsonSchema schema = new DefaultJsonSchemaFactory()
.create(
Json
.createObjectBuilder()
.add(
"properties",
Json
.createObjectBuilder()
.add("test", JsonValue.TRUE)
.add("invalid", JsonValue.EMPTY_JSON_ARRAY)
)
.build()
);
final KeywordType keywordType = new PropertiesKeywordType();
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
}

@Test
void should_not_be_createable_with_string_in_schemas() {
final JsonSchema schema = new DefaultJsonSchemaFactory()
.create(
Json
.createObjectBuilder()
.add(
"properties",
Json.createObjectBuilder().add("test", JsonValue.TRUE).add("invalid", Json.createValue("value"))
)
.build()
);
final KeywordType keywordType = new PropertiesKeywordType();
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
}

@Test
void should_not_be_createable_with_number_in_schemas() {
final JsonSchema schema = new DefaultJsonSchemaFactory()
.create(
Json
.createObjectBuilder()
.add(
"properties",
Json.createObjectBuilder().add("test", JsonValue.TRUE).add("invalid", Json.createValue(3.14))
)
.build()
);
final KeywordType keywordType = new PropertiesKeywordType();
assertThrows(IllegalArgumentException.class, () -> keywordType.createKeyword(schema));
}

@Test
void should_be_know_his_name() {
final Keyword keyword = new PropertiesKeywordType()
Expand Down

0 comments on commit bef87a4

Please sign in to comment.