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 support for ref #67

Merged
merged 3 commits into from
Dec 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
9 changes: 8 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
<inculde>**/tests/draft2020-12/enum.json</inculde>
<inculde>**/tests/draft2020-12/exclusiveMaximum.json</inculde>
<inculde>**/tests/draft2020-12/exclusiveMinimum.json</inculde>
<inculde>**/tests/draft2020-12/format.json</inculde>
<!-- how to validate?
<inculde>**/tests/draft2020-12/id.json</inculde>
-->
<!-- more than items keyword needed :(
<inculde>**/tests/draft2020-12/items.json</inculde>
-->
Expand All @@ -128,7 +132,10 @@
<inculde>**/tests/draft2020-12/patternProperties.json</inculde>
<inculde>**/tests/draft2020-12/prefixItems.json</inculde>
<inculde>**/tests/draft2020-12/properties.json</inculde>
<!--inculde>**/tests/draft2020-12/ref.json</inculde-->
<!--
needs allOf, $anchor and dynamicAnchor and some other stuff :(
<inculde>**/tests/draft2020-12/ref.json</inculde>
-->
<inculde>**/tests/draft2020-12/required.json</inculde>
<inculde>**/tests/draft2020-12/type.json</inculde>
<inculde>**/tests/draft2020-12/uniqueItems.json</inculde>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,22 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.codition.AllOfCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.ApplicatorBasedCondtion;
import io.github.sebastiantoepfer.jsonschema.core.codition.AssertionBasedCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.Condition;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.VocabularyKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinition;
import io.github.sebastiantoepfer.jsonschema.vocabulary.spi.VocabularyDefinitions;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

final class DefaultJsonObjectSchema extends AbstractJsonValueSchema {
public final class DefaultJsonObjectSchema extends AbstractJsonValueSchema {

public DefaultJsonObjectSchema(final JsonObject value) {
super(value);
}

@Override
public Validator validator() {
return keywords()
.map(this::asContraint)
.flatMap(Optional::stream)
.collect(
collectingAndThen(toList(), constraints -> new DefaultValidator(new AllOfCondition<>(constraints)))
);
return keywords().collect(collectingAndThen(toList(), KeywordBasedValidator::new));
}

@Override
Expand All @@ -66,42 +50,15 @@ public Optional<Keyword> keywordByName(final String name) {
}

private Stream<Keyword> keywords() {
final Keywords keywords = new Keywords(vocabulary());
final Keywords keywords = new KeywordExtractor(this).createKeywords();
return asJsonObject().keySet().stream().map(propertyName -> keywords.createKeywordFor(this, propertyName));
}

private Collection<VocabularyDefinition> vocabulary() {
final KeywordType keywordType = new VocabularyKeywordType();
return Optional
.ofNullable(asJsonObject().get(keywordType.name()))
.map(keywordValue -> keywordType.createKeyword(this))
.filter(VocabularyDefinitions.class::isInstance)
.map(VocabularyDefinitions.class::cast)
.stream()
.flatMap(VocabularyDefinitions::definitions)
.toList();
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
return Optional
.ofNullable(asJsonObject().get(name))
.filter(value ->
Stream.of(InstanceType.BOOLEAN, InstanceType.OBJECT).anyMatch(type -> type.isInstance(value))
)
.map(new DefaultJsonSchemaFactory()::create)
.flatMap(new DefaultJsonSchemaFactory()::tryToCreateSchemaFrom)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
}

private Optional<Condition<JsonValue>> asContraint(final Keyword keyword) {
final Condition<JsonValue> result;
if (keyword.hasCategory(Keyword.KeywordCategory.ASSERTION)) {
result = new AssertionBasedCondition(keyword.asAssertion());
} else if (keyword.hasCategory(Keyword.KeywordCategory.APPLICATOR)) {
result = new ApplicatorBasedCondtion(keyword.asApplicator());
} else {
result = null;
}
return Optional.ofNullable(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.spi.JsonSchemaFactory;
import jakarta.json.JsonValue;
import java.util.Optional;

public final class DefaultJsonSchemaFactory implements JsonSchemaFactory {

@Override
public JsonSchema create(final JsonValue schema) {
return tryToCreateSchemaFrom(schema).orElseThrow(IllegalArgumentException::new);
}

public Optional<JsonSchema> tryToCreateSchemaFrom(final JsonValue schema) {
final JsonSchema result;
if (schema == JsonValue.TRUE) {
result = new TrueJsonSchema();
} else if (schema == JsonValue.FALSE) {
result = new FalseJsonSchema();
} else if (schema.equals(JsonValue.EMPTY_JSON_OBJECT)) {
result = new EmptyJsonSchema();
} else {
} else if (schema.getValueType() == JsonValue.ValueType.OBJECT) {
result = new DefaultJsonObjectSchema(schema.asJsonObject());
} else {
result = null;
}
return result;
return Optional.ofNullable(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,20 @@
*/
package io.github.sebastiantoepfer.jsonschema.core;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.JsonSubSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

final class DefaultJsonSubSchema implements JsonSubSchema {
public final class DefaultJsonSubSchema implements JsonSubSchema {

private final JsonSchema owner;
private final JsonSchema schema;
Expand All @@ -48,17 +53,42 @@ public JsonSchema owner() {

@Override
public Validator validator() {
return schema.validator();
final Validator result;
if (isJsonObject()) {
result = keywords().collect(collectingAndThen(toList(), KeywordBasedValidator::new));
} else {
result = schema.validator();
}
return result;
}

@Override
public Optional<Keyword> keywordByName(final String name) {
return schema.keywordByName(name);
return keywords().filter(keyword -> keyword.hasName(name)).findAny();
}

private Stream<Keyword> keywords() {
final Stream<Keyword> result;
if (isJsonObject()) {
final Keywords keywords = new KeywordExtractor(schema).createKeywords();
result =
asJsonObject().keySet().stream().map(propertyName -> keywords.createKeywordFor(this, propertyName));
} else {
result = Stream.empty();
}
return result;
}

private boolean isJsonObject() {
return getValueType() == JsonValue.ValueType.OBJECT;
}

@Override
public Optional<JsonSubSchema> asSubSchema(final String name) {
return schema.asSubSchema(name);
return Optional
.ofNullable(asJsonObject().get(name))
.flatMap(new DefaultJsonSchemaFactory()::tryToCreateSchemaFrom)
.map(subSchema -> new DefaultJsonSubSchema(this, subSchema));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.codition.AllOfCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.ApplicatorBasedCondtion;
import io.github.sebastiantoepfer.jsonschema.core.codition.AssertionBasedCondition;
import io.github.sebastiantoepfer.jsonschema.core.codition.Condition;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.Optional;

final class KeywordBasedValidator implements Validator {

private final DefaultValidator validator;

public KeywordBasedValidator(final Collection<Keyword> keywords) {
this.validator =
keywords
.stream()
.map(KeywordBasedValidator::asContraint)
.flatMap(Optional::stream)
.collect(
collectingAndThen(toList(), constraints -> new DefaultValidator(new AllOfCondition<>(constraints)))
);
}

@Override
public boolean isValid(final JsonValue data) {
return validator.isValid(data);
}

private static Optional<Condition<JsonValue>> asContraint(final Keyword keyword) {
final Condition<JsonValue> result;
if (keyword.hasCategory(Keyword.KeywordCategory.ASSERTION)) {
result = new AssertionBasedCondition(keyword.asAssertion());
} else if (keyword.hasCategory(Keyword.KeywordCategory.APPLICATOR)) {
result = new ApplicatorBasedCondtion(keyword.asApplicator());
} else {
result = null;
}
return Optional.ofNullable(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.core.vocab.core.VocabularyKeywordType;
import jakarta.json.JsonValue;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

class KeywordExtractor {

private final JsonSchema schema;

public KeywordExtractor(final JsonSchema schema) {
this.schema = Objects.requireNonNull(schema);
}

public Keywords createKeywords() {
final Keywords result;
final VocabularyKeywordType keywordType = new VocabularyKeywordType();
if (
schema.getValueType() == JsonValue.ValueType.OBJECT && schema.asJsonObject().containsKey(keywordType.name())
) {
result =
keywordType
.createKeyword(schema)
.definitions()
.collect(Collectors.collectingAndThen(Collectors.toList(), Keywords::new));
} else {
result = new Keywords(List.of());
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.codition;

import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.List;

public class AnyOfCondition implements Condition<JsonValue> {

private final Collection<Condition<JsonValue>> conditions;

public AnyOfCondition(final Collection<Condition<JsonValue>> conditions) {
this.conditions = List.copyOf(conditions);
}

@Override
public boolean isFulfilledBy(final JsonValue value) {
return conditions.stream().anyMatch(c -> c.isFulfilledBy(value));
}
}
Loading