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

Keyword items #21

Merged
merged 3 commits into from
Oct 3, 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
@@ -0,0 +1,32 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema;

/**
*
* see: https://json-schema.org/draft/2020-12/json-schema-core#name-root-schema-and-subschemas-
*/
public interface JsonSubSchema extends JsonSchema {
JsonSchema owner();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.keyword;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import jakarta.json.JsonValue;
import java.util.Objects;

Expand All @@ -33,5 +34,5 @@ default boolean hasName(String name) {

String name();

Keyword createKeyword(JsonValue value);
Keyword createKeyword(JsonSchema schema, JsonValue value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import jakarta.json.JsonValue;
import org.junit.jupiter.api.Test;

Expand All @@ -45,7 +46,7 @@ public String name() {
}

@Override
public Keyword createKeyword(JsonValue value) {
public Keyword createKeyword(final JsonSchema schema, final JsonValue value) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Expand Down
10 changes: 10 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@
<inculde>**/tests/draft2019-09/multipleOf.json</inculde>
<inculde>**/tests/draft2020-12/multipleOf.json</inculde>
<inculde>**/tests/draft-next/multipleOf.json</inculde>

<!-- more than items keyword needed :(
<inculde>**/tests/draft3/items.json</inculde>
<inculde>**/tests/draft4/items.json</inculde>
<inculde>**/tests/draft6/items.json</inculde>
<inculde>**/tests/draft7/items.json</inculde>
<inculde>**/tests/draft2019-09/items.json</inculde>
<inculde>**/tests/draft2020-12/items.json</inculde>
<inculde>**/tests/draft-next/items.json</inculde>
-->
</includes>
</resource>
</resources>
Expand Down
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 @@ -46,7 +46,8 @@ public BasicVocabulary() {
new ExclusiveMinimumKeywordType(),
new MaximumKeywordType(),
new ExclusiveMaximumKeywordType(),
new MultipleOfKeywordType()
new MultipleOfKeywordType(),
new ItemsKeywordType()
);
}

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 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
@@ -0,0 +1,100 @@
/*
* The MIT License
*
* Copyright 2023 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab.basic;

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
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;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import jakarta.json.JsonArray;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

final class ItemsKeywordType implements KeywordType {

@Override
public String name() {
return "items";
}

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

private class ItemsKeyword implements ConstraintAssertion, JsonSubSchema {

private final JsonSchema owner;
private final JsonSchema 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
public boolean hasName(final String name) {
return Objects.equals(name(), name);
}

@Override
public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
final Collection<ConstraintViolation> result;
if (!InstanceType.ARRAY.isInstance(value) || matchesSchema(value.asJsonArray())) {
result = Collections.emptyList();
} else {
result = List.of(new ConstraintViolation());
}
return result;
}

private boolean matchesSchema(final JsonArray items) {
final Validator itemValidator = validator();
return items.stream().map(itemValidator::validate).allMatch(Collection::isEmpty);
}
}
}
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.NUMBER.isInstance(value)) {
return new MultipleOfKeyword((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
Loading