Skip to content

Commit

Permalink
implement items as subschema
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Oct 3, 2023
1 parent 4212c42 commit 0849b17
Show file tree
Hide file tree
Showing 38 changed files with 287 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Validator validator() {
return asJsonObject()
.entrySet()
.stream()
.map(keywords::createKeywordFor)
.map(keyword -> keywords.createKeywordFor(this, keyword))
.filter(Constraint.class::isInstance)
.map(k -> (Constraint<JsonValue>) k)
.collect(
Expand All @@ -60,7 +60,7 @@ public Validator validator() {

private Collection<VocabularyDefinition> vocabulary() {
return new KeywordSearch(new VocabularyKeywordType())
.searchForKeywordIn(asJsonObject())
.searchForKeywordIn(this)
.filter(VocabularyDefinitions.class::isInstance)
.map(VocabularyDefinitions.class::cast)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package io.github.sebastiantoepfer.jsonschema.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonObject;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -37,7 +37,9 @@ public KeywordSearch(final KeywordType keywordType) {
this.keywordType = Objects.requireNonNull(keywordType);
}

public Optional<Keyword> searchForKeywordIn(final JsonObject schema) {
return Optional.ofNullable(schema.get(keywordType.name())).map(keywordType::createKeyword);
public Optional<Keyword> searchForKeywordIn(final JsonSchema schema) {
return Optional
.ofNullable(schema.asJsonObject().get(keywordType.name()))
.map(keywordValue -> keywordType.createKeyword(schema, keywordValue));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.toMap;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.basic.BasicVocabulary;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.CoreLazyVocabulary;
Expand Down Expand Up @@ -80,13 +81,13 @@ public Keywords(final Collection<VocabularyDefinition> vocabDefs) {
);
}

public Keyword createKeywordFor(final Map.Entry<String, JsonValue> property) {
public Keyword createKeywordFor(final JsonSchema schema, final Map.Entry<String, JsonValue> property) {
return vocabularies
.stream()
.map(vocab -> vocab.findKeywordTypeByName(property.getKey()))
.flatMap(Optional::stream)
.findFirst()
.map(keywordType -> keywordType.createKeyword(property.getValue()))
.map(keywordType -> keywordType.createKeyword(schema, property.getValue()))
.orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.NUMBER.isInstance(value)) {
return new ExclusiveMaximumKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.NUMBER.isInstance(value)) {
return new ExclusiveMinimumKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSchemas;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
Expand All @@ -46,16 +47,33 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
return new ItemsKeyword(JsonSchemas.load(value));
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new ItemsKeyword(schema, JsonSchemas.load(value));
}

private class ItemsKeyword implements ConstraintAssertion {
private class ItemsKeyword implements ConstraintAssertion, JsonSubSchema {

private final JsonSchema owner;
private final JsonSchema schema;

public ItemsKeyword(final JsonSchema schema) {
this.schema = schema;
public ItemsKeyword(final JsonSchema owner, final JsonSchema schema) {
this.owner = Objects.requireNonNull(owner);
this.schema = Objects.requireNonNull(schema);
}

@Override
public JsonSchema owner() {
return owner;
}

@Override
public Validator validator() {
return schema.validator();
}

@Override
public ValueType getValueType() {
return schema.getValueType();
}

@Override
Expand All @@ -75,7 +93,7 @@ public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
}

private boolean matchesSchema(final JsonArray items) {
final Validator itemValidator = schema.validator();
final Validator itemValidator = validator();
return items.stream().map(itemValidator::validate).allMatch(Collection::isEmpty);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.INTEGER.isInstance(value)) {
return new MaxLengthKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.NUMBER.isInstance(value)) {
return new MaximumKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.INTEGER.isInstance(value)) {
return new MinLengthKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.NUMBER.isInstance(value)) {
return new MinimumKeyword((JsonNumber) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -44,7 +45,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
if (InstanceType.STRING.isInstance(value)) {
return new PatternKeyword((JsonString) value);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.constraint.AnyConstraint;
import io.github.sebastiantoepfer.jsonschema.core.constraint.Constraint;
import io.github.sebastiantoepfer.jsonschema.core.vocab.ConstraintAssertion;
Expand All @@ -52,7 +53,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new TypeKeyword(value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.basic;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -43,7 +44,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -40,7 +41,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -40,7 +41,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -41,7 +42,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new Applicator() {
@Override
public boolean applyTo(final JsonValue instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -40,7 +41,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -41,7 +42,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new Applicator() {
@Override
public boolean applyTo(final JsonValue instance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand All @@ -40,7 +41,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
return new DefaultAnnotation(name(), value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.core;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
Expand All @@ -47,7 +48,7 @@ public String name() {
}

@Override
public Keyword createKeyword(final JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
final Keyword result;
if (InstanceType.OBJECT.isInstance(value)) {
result = new VocabularyKeyword(value);
Expand Down
Loading

0 comments on commit 0849b17

Please sign in to comment.