Skip to content

Commit

Permalink
Merge pull request #5 from sebastian-toepfer/smaller_refactoring
Browse files Browse the repository at this point in the history
smaller refactorings
  • Loading branch information
sebastian-toepfer authored Sep 8, 2023
2 parents 52744d1 + 49b689f commit 946c6c5
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.impl.constraint;

import static java.util.Arrays.asList;
import static java.util.function.Predicate.not;

import io.github.sebastiantoepfer.jsonschema.core.ConstraintViolation;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public final class AllOfConstraint<T> implements Constraint<T> {

private final List<Constraint<? super T>> contraints;

public AllOfConstraint(final Constraint<? super T>... constraints) {
this(asList(constraints));
}

public AllOfConstraint(final Collection<? extends Constraint<? super T>> contraints) {
if (contraints.isEmpty()) {
throw new IllegalArgumentException("min one constraint must be provided!");
}
this.contraints = List.copyOf(contraints);
}

@Override
public Collection<ConstraintViolation> violationsBy(final T value) {
return contraints
.stream()
.map(c -> c.violationsBy(value))
.filter(not(Collection::isEmpty))
.findFirst()
.orElseGet(Set::of);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

public class AnyConstraint<T> implements Constraint<T> {

private final List<Constraint<T>> contraints;
private final List<Constraint<? super T>> contraints;

public AnyConstraint(final Constraint<T>... constraints) {
public AnyConstraint(final Constraint<? super T>... constraints) {
this(asList(constraints));
}

public AnyConstraint(final Collection<? extends Constraint<T>> contraints) {
public AnyConstraint(final Collection<? extends Constraint<? super T>> contraints) {
if (contraints.isEmpty()) {
throw new IllegalArgumentException("min one constraint must be provided!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.impl.keyword;

import io.github.sebastiantoepfer.jsonschema.core.keyword.DefaultAnnotation;
import io.github.sebastiantoepfer.jsonschema.core.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Map;

public final class Keywords {

public static Keyword createKeywordFor(final Map.Entry<String, JsonValue> property) {
return switch (property.getKey()) {
case "type" -> new Type(property.getValue());
default -> new DefaultAnnotation(property.getKey(), property.getValue());
};
}

private Keywords() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/**
* see: https://json-schema.org/understanding-json-schema/reference/type.html
*/
public final class Type implements ConstraintAssertion, Constraint<JsonValue> {
final class Type implements ConstraintAssertion, Constraint<JsonValue> {

private final JsonValue definition;

Expand Down Expand Up @@ -106,22 +106,9 @@ public JsonStringTypeConstraint(final JsonString type) {

@Override
public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
return new InstanceTypeConstraint(InstanceType.valueOf(type)).violationsBy(value);
}
}

private static final class InstanceTypeConstraint implements Constraint<JsonValue> {

private final InstanceType type;

public InstanceTypeConstraint(final InstanceType type) {
this.type = Objects.requireNonNull(type);
}

@Override
public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
final InstanceType instanceType = InstanceType.valueOf(type);
final Collection<ConstraintViolation> result;
if (type.isInstance(value)) {
if (instanceType.isInstance(value)) {
result = Set.of();
} else {
result = Set.of(new ConstraintViolation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.impl.spi;

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

import io.github.sebastiantoepfer.jsonschema.core.Validator;
import io.github.sebastiantoepfer.jsonschema.core.impl.constraint.AllOfConstraint;
import io.github.sebastiantoepfer.jsonschema.core.impl.constraint.Constraint;
import io.github.sebastiantoepfer.jsonschema.core.impl.constraint.UnfulfillableConstraint;
import io.github.sebastiantoepfer.jsonschema.core.impl.keyword.Type;
import io.github.sebastiantoepfer.jsonschema.core.impl.keyword.Keywords;
import jakarta.json.JsonObject;
import jakarta.json.JsonValue;

Expand All @@ -38,12 +41,14 @@ public DefaultJsonSchema(final JsonObject value) {

@Override
public Validator validator() {
final Constraint<JsonValue> constraint;
if (asJsonObject().containsKey("type")) {
constraint = new Type(asJsonObject().get("type"));
} else {
constraint = new UnfulfillableConstraint<>();
}
return new DefaultValidator(constraint);
return asJsonObject()
.entrySet()
.stream()
.map(Keywords::createKeywordFor)
.filter(Constraint.class::isInstance)
.map(k -> (Constraint<JsonValue>) k)
.collect(
collectingAndThen(toList(), constraints -> new DefaultValidator(new AllOfConstraint<>(constraints)))
);
}
}

0 comments on commit 946c6c5

Please sign in to comment.