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

provide a jsonprovider to const keyword #122

Merged
merged 1 commit into from
May 30, 2024
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 @@ -27,15 +27,22 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.BiFunction;

public class AnyKeywordType implements KeywordType {

private final JsonProvider jsonContext;
private final String name;
private final Function<JsonValue, Keyword> keywordCreator;
private final BiFunction<JsonProvider, JsonValue, Keyword> keywordCreator;

public AnyKeywordType(final String name, final Function<JsonValue, Keyword> keywordCreator) {
public AnyKeywordType(
final JsonProvider jsonContext,
final String name,
final BiFunction<JsonProvider, JsonValue, Keyword> keywordCreator
) {
this.jsonContext = Objects.requireNonNull(jsonContext);
this.name = Objects.requireNonNull(name);
this.keywordCreator = Objects.requireNonNull(keywordCreator);
}
Expand All @@ -47,6 +54,6 @@ public String name() {

@Override
public Keyword createKeyword(final JsonSchema schema) {
return keywordCreator.apply(schema.asJsonObject().getOrDefault(name, JsonValue.NULL));
return keywordCreator.apply(jsonContext, schema.asJsonObject().getOrDefault(name, JsonValue.NULL));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import io.github.sebastiantoepfer.ddd.media.json.JsonObjectPrintable;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import jakarta.json.Json;
import jakarta.json.JsonNumber;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import java.util.Objects;

/**
Expand All @@ -46,9 +46,11 @@
class ConstKeyword implements Assertion {

static final String NAME = "const";
private final JsonProvider jsonContext;
private final JsonValue allowedValue;

public ConstKeyword(final JsonValue allowedValue) {
public ConstKeyword(final JsonProvider jsonContext, final JsonValue allowedValue) {
this.jsonContext = Objects.requireNonNull(jsonContext);
this.allowedValue = Objects.requireNonNull(allowedValue);
}

Expand Down Expand Up @@ -79,6 +81,8 @@ public boolean hasName(final String name) {

@Override
public <T extends Media<T>> T printOn(final T media) {
return new JsonObjectPrintable(Json.createObjectBuilder().add(NAME, allowedValue).build()).printOn(media);
return new JsonObjectPrintable(jsonContext.createObjectBuilder().add(NAME, allowedValue).build()).printOn(
media
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ValidationVocabulary(final JsonProvider jsonContext) {
this.vocab = new DefaultVocabulary(
URI.create("https://json-schema.org/draft/2020-12/vocab/validation"),
new TypeKeywordType(),
new AnyKeywordType(ConstKeyword.NAME, ConstKeyword::new),
new AnyKeywordType(jsonContext, ConstKeyword.NAME, ConstKeyword::new),
new ArrayKeywordType(EnumKeyword.NAME, EnumKeyword::new),
new ObjectKeywordType(DependentRequiredKeyword.NAME, DependentRequiredKeyword::new),
new IntegerKeywordType(jsonContext, MaxPropertiesKeyword.NAME, MaxPropertiesKeyword::new),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import jakarta.json.spi.JsonProvider;
import org.hamcrest.Matcher;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -139,7 +140,7 @@ void should_be_printable() {
}

private static Keyword createKeywordFrom(final JsonObject json) {
return new AnyKeywordType("const", ConstKeyword::new).createKeyword(
return new AnyKeywordType(JsonProvider.provider(), "const", ConstKeyword::new).createKeyword(
new DefaultJsonSchemaFactory().create(json)
);
}
Expand Down