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 applyTo to schema and deprecate validator #161

Merged
merged 1 commit into from
Jun 15, 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
@@ -0,0 +1,30 @@
/*
* The MIT License
*
* Copyright 2024 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;

import jakarta.json.JsonValue;

public interface Applicable {
boolean applyTo(JsonValue instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import java.util.Optional;
import java.util.stream.Stream;

public interface JsonSchema extends JsonValue, Printable {
public interface JsonSchema extends JsonValue, Applicable, Printable {
@Deprecated(forRemoval = true)
Validator validator();

Optional<Keyword> keywordByName(String name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
package io.github.sebastiantoepfer.jsonschema.keyword;

import jakarta.json.JsonValue;
import io.github.sebastiantoepfer.jsonschema.Applicable;
import java.util.Collection;
import java.util.Set;

Expand All @@ -35,11 +35,9 @@
*
* see: http://json-schema.org/draft/2020-12/json-schema-core.html#name-root-schema-and-subschemas-
**/
public interface Applicator extends Keyword {
public interface Applicator extends Keyword, Applicable {
@Override
default Collection<KeywordCategory> categories() {
return Set.of(KeywordCategory.APPLICATOR);
}

boolean applyTo(JsonValue instance);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean applyTo(JsonValue instance) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<Keyword> keywordByName(String name) {
throw new UnsupportedOperationException("Not supported yet.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean applyTo(JsonValue instance) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Optional<Keyword> keywordByName(final String name) {
throw new UnsupportedOperationException("Not supported yet.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonObject;
import jakarta.json.JsonPointer;
import jakarta.json.JsonValue;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -51,6 +52,11 @@ public Validator validator() {
return keywords().collect(collectingAndThen(toList(), KeywordBasedValidator::new));
}

@Override
public boolean applyTo(final JsonValue instance) {
return keywords().map(KeywordPredicate::new).allMatch(prdct -> prdct.test(instance));
}

@Override
public Optional<Keyword> keywordByName(final String name) {
return keywords().filter(k -> k.hasName(name)).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import jakarta.json.JsonValue;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

final class DefaultJsonSubSchema implements JsonSubSchema {
Expand All @@ -58,6 +59,23 @@ public JsonSchema owner() {
return owner;
}

@Override
public boolean applyTo(final JsonValue instance) {
final Stream<Predicate<JsonValue>> predicates;
if (isJsonObject()) {
predicates = keywords()
.filter(
k ->
k.hasCategory(Keyword.KeywordCategory.ASSERTION) ||
k.hasCategory(Keyword.KeywordCategory.APPLICATOR)
)
.map(KeywordPredicate::new);
} else {
predicates = Stream.of(schema::applyTo);
}
return predicates.allMatch(prdct -> prdct.test(instance));
}

@Override
public Validator validator() {
final Validator result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public EmptyJsonSchema() {
super(JsonValue.EMPTY_JSON_OBJECT);
}

@Override
public boolean applyTo(final JsonValue instance) {
return true;
}

@Override
public Validator validator() {
return new DefaultValidator(new NoCondition<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public <T extends Media<T>> T printOn(final T media) {
throw new UnsupportedOperationException("false schema not supported yet!");
}

@Override
public boolean applyTo(final JsonValue instance) {
return false;
}

@Override
public Validator validator() {
return new DefaultValidator(new UnfulfillableCondition<>());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright 2024 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.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.function.Predicate;

final class KeywordPredicate implements Predicate<JsonValue> {

private final Predicate<JsonValue> predicate;

public KeywordPredicate(final Keyword keyword) {
if (keyword.hasCategory(Keyword.KeywordCategory.ASSERTION)) {
predicate = keyword.asAssertion()::isValidFor;
} else if (keyword.hasCategory(Keyword.KeywordCategory.APPLICATOR)) {
predicate = keyword.asApplicator()::applyTo;
} else {
throw new IllegalArgumentException("keyword must be an assertion or an applicator!");
}
}

@Override
public boolean test(final JsonValue t) {
return predicate.test(t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public TrueJsonSchema() {
super(JsonValue.TRUE);
}

@Override
public boolean applyTo(final JsonValue instance) {
return true;
}

@Override
public Validator validator() {
return new DefaultValidator(new NoCondition<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean applyTo(final JsonValue instance) {
private boolean additionalPropertiesMatches(final JsonObject instance) {
return findPropertiesForValidation(instance)
.map(Map.Entry::getValue)
.allMatch(value -> additionalPropertiesSchema.validator().isValid(value));
.allMatch(additionalPropertiesSchema::applyTo);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ private JsonValue valueFor(final JsonArray values) {
}

Stream<JsonValue> matchingValues(final JsonArray values) {
return values.stream().filter(contains.validator()::isValid);
return values.stream().filter(contains::applyTo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public JsonValue valueFor(final JsonValue value) {
}

private boolean appliesToAnyFor(final JsonArray value) {
return itemsForValidation(value).anyMatch(schema.validator()::isValid);
return itemsForValidation(value).anyMatch(schema::applyTo);
}

@Override
Expand All @@ -99,7 +99,7 @@ public boolean applyTo(final JsonValue instance) {
}

private boolean applyTo(final JsonArray items) {
return itemsForValidation(items).allMatch(schema.validator()::isValid);
return itemsForValidation(items).allMatch(schema::applyTo);
}

private Stream<JsonValue> itemsForValidation(final JsonArray items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public NotKeyword(final JsonSchema schema) {

@Override
public boolean applyTo(final JsonValue instance) {
return !schema.validator().isValid(instance);
return !schema.applyTo(instance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private boolean propertyMatches(final Map.Entry<String, JsonValue> property) {
.stream()
.filter(e -> e.getKey().matcher(property.getKey()).find())
.map(Map.Entry::getValue)
.allMatch(schema -> schema.validator().isValid(property.getValue()));
.allMatch(schema -> schema.applyTo(property.getValue()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean applyTo(final JsonValue instance) {
private boolean matchesSchemas(final JsonArray instance) {
boolean result = true;
for (int i = 0; i < Math.min(schemas.size(), instance.size()); i++) {
result &= schemas.get(i).validator().isValid(instance.get(i));
result &= schemas.get(i).applyTo(instance.get(i));
if (!result) {
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.InstanceType;
import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;
Expand Down Expand Up @@ -73,7 +72,6 @@ public boolean applyTo(final JsonValue instance) {
}

private boolean allProperyNamesMatchesSchema(final JsonObject obj) {
final Validator validator = names.validator();
return obj.keySet().stream().map(provider::createValue).allMatch(validator::isValid);
return obj.keySet().stream().map(provider::createValue).allMatch(names::applyTo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public <T extends Media<T>> T printOn(final T media) {

@Override
public boolean applyTo(final JsonValue instance) {
return retrieveJsonSchema().validator().isValid(instance);
return retrieveJsonSchema().applyTo(instance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public MyJsonValueSchema(JsonValue value) {
super(value);
}

@Override
public boolean applyTo(final JsonValue instance) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public Validator validator() {
throw new UnsupportedOperationException("Not supported yet.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class EmptyJsonSchemaTest {
@ArgumentsSource(JsonValuesArguments.class)
void should_be_valid_for_everything(final JsonValue value) {
assertThat(new EmptyJsonSchema().validator().isValid(value), is(true));
assertThat(new EmptyJsonSchema().applyTo(value), is(true));
}

@Test
Expand Down