Skip to content

Commit

Permalink
Merge pull request #37 from sebastian-toepfer/remove_constaint_violation
Browse files Browse the repository at this point in the history
Remove constaint violation
  • Loading branch information
sebastian-toepfer authored Oct 11, 2023
2 parents 12db75f + 06c5d75 commit 93862fe
Show file tree
Hide file tree
Showing 35 changed files with 189 additions and 462 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import java.io.StringReader;
import java.util.Collection;

public interface Validator {
default Collection<ConstraintViolation> validate(final String data) {
default boolean isValid(final String data) {
try (final JsonReader reader = Json.createReader(new StringReader(data))) {
return validate(reader.readValue());
return isValid(reader.readValue());
}
}

Collection<ConstraintViolation> validate(final JsonValue data);
boolean isValid(final JsonValue data);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.constraint.AllOfConstraint;
import io.github.sebastiantoepfer.jsonschema.core.constraint.Constraint;
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.Applicator;
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
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.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -59,7 +56,7 @@ public Validator validator() {
.map(this::asContraint)
.flatMap(Optional::stream)
.collect(
collectingAndThen(toList(), constraints -> new DefaultValidator(new AllOfConstraint<>(constraints)))
collectingAndThen(toList(), constraints -> new DefaultValidator(new AllOfCondition<>(constraints)))
);
}

Expand All @@ -82,55 +79,15 @@ private Stream<Keyword> keywords() {
return asJsonObject().entrySet().stream().map(property -> keywords.createKeywordFor(this, property));
}

private Optional<Constraint<JsonValue>> asContraint(final Keyword keyword) {
final Constraint<JsonValue> result;
private Optional<Condition<JsonValue>> asContraint(final Keyword keyword) {
final Condition<JsonValue> result;
if (keyword.hasCategory(Keyword.KeywordCategory.ASSERTION)) {
result = new AssertionConstraint(keyword.asAssertion());
result = new AssertionBasedCondition(keyword.asAssertion());
} else if (keyword.hasCategory(Keyword.KeywordCategory.APPLICATOR)) {
result = new ApplicatorConstaint(keyword.asApplicator());
result = new ApplicatorBasedCondtion(keyword.asApplicator());
} else {
result = null;
}
return Optional.ofNullable(result);
}

private static final class AssertionConstraint implements Constraint<JsonValue> {

private final Assertion assertion;

public AssertionConstraint(final Assertion assertion) {
this.assertion = Objects.requireNonNull(assertion);
}

@Override
public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
final Collection<ConstraintViolation> result;
if (assertion.isValidFor(value)) {
result = List.of();
} else {
result = List.of(new ConstraintViolation());
}
return result;
}
}

private static final class ApplicatorConstaint implements Constraint<JsonValue> {

private final Applicator applicator;

public ApplicatorConstaint(final Applicator applicator) {
this.applicator = Objects.requireNonNull(applicator);
}

@Override
public Collection<ConstraintViolation> violationsBy(final JsonValue value) {
final Collection<ConstraintViolation> result;
if (applicator.applyTo(value)) {
result = List.of();
} else {
result = List.of(new ConstraintViolation());
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,21 @@
*/
package io.github.sebastiantoepfer.jsonschema.core;

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.constraint.Constraint;
import io.github.sebastiantoepfer.jsonschema.core.codition.Condition;
import jakarta.json.JsonValue;
import java.util.Collection;
import java.util.Objects;

final class DefaultValidator implements Validator {

private final Constraint<? super JsonValue> contraint;
private final Condition<? super JsonValue> contraint;

public DefaultValidator(final Constraint<? super JsonValue> contraint) {
public DefaultValidator(final Condition<? super JsonValue> contraint) {
this.contraint = Objects.requireNonNull(contraint);
}

@Override
public Collection<ConstraintViolation> validate(final JsonValue data) {
return contraint.violationsBy(data);
public boolean isValid(final JsonValue data) {
return contraint.isFulfilledBy(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package io.github.sebastiantoepfer.jsonschema.core;

import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.constraint.NoConstraint;
import io.github.sebastiantoepfer.jsonschema.core.codition.NoCondition;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;
Expand All @@ -37,7 +37,7 @@ public EmptyJsonSchema() {

@Override
public Validator validator() {
return new DefaultValidator(new NoConstraint<>());
return new DefaultValidator(new NoCondition<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package io.github.sebastiantoepfer.jsonschema.core;

import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.constraint.UnfulfillableConstraint;
import io.github.sebastiantoepfer.jsonschema.core.codition.UnfulfillableCondition;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;
Expand All @@ -37,7 +37,7 @@ public FalseJsonSchema() {

@Override
public Validator validator() {
return new DefaultValidator(new UnfulfillableConstraint<>());
return new DefaultValidator(new UnfulfillableCondition<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package io.github.sebastiantoepfer.jsonschema.core;

import io.github.sebastiantoepfer.jsonschema.Validator;
import io.github.sebastiantoepfer.jsonschema.core.constraint.NoConstraint;
import io.github.sebastiantoepfer.jsonschema.core.codition.NoCondition;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import jakarta.json.JsonValue;
import java.util.Optional;
Expand All @@ -37,7 +37,7 @@ public TrueJsonSchema() {

@Override
public Validator validator() {
return new DefaultValidator(new NoConstraint<>());
return new DefaultValidator(new NoCondition<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 static java.util.Arrays.asList;

import java.util.Collection;
import java.util.List;

public final class AllOfCondition<T> implements Condition<T> {

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

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

public AllOfCondition(final Collection<? extends Condition<? super T>> contraints) {
this.contraints = List.copyOf(contraints);
}

@Override
public boolean isFulfilledBy(final T value) {
return contraints.stream().allMatch(c -> c.isFulfilledBy(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,22 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.constraint;
package io.github.sebastiantoepfer.jsonschema.core.codition;

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import java.util.Collection;
import java.util.Set;
import io.github.sebastiantoepfer.jsonschema.keyword.Applicator;
import jakarta.json.JsonValue;
import java.util.Objects;

public final class UnfulfillableConstraint<T> implements Constraint<T> {
public final class ApplicatorBasedCondtion implements Condition<JsonValue> {

private final Applicator applicator;

public ApplicatorBasedCondtion(final Applicator applicator) {
this.applicator = Objects.requireNonNull(applicator);
}

@Override
public Collection<ConstraintViolation> violationsBy(final T value) {
return Set.of(new ConstraintViolation());
public boolean isFulfilledBy(final JsonValue value) {
return applicator.applyTo(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.vocab;
package io.github.sebastiantoepfer.jsonschema.core.codition;

import io.github.sebastiantoepfer.jsonschema.core.constraint.Constraint;
import io.github.sebastiantoepfer.jsonschema.keyword.Assertion;
import jakarta.json.JsonValue;
import java.util.Objects;

public final class AssertionBasedCondition implements Condition<JsonValue> {

private final Assertion assertion;

public AssertionBasedCondition(final Assertion assertion) {
this.assertion = Objects.requireNonNull(assertion);
}

/**
* only simplify pitest :)
*/
public interface ConstraintAssertion extends Assertion, Constraint<JsonValue> {
@Override
default boolean isValidFor(JsonValue instance) {
return violationsBy(instance).isEmpty();
public boolean isFulfilledBy(final JsonValue value) {
return assertion.isValidFor(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema;
package io.github.sebastiantoepfer.jsonschema.core.codition;

public final class ConstraintViolation {}
public interface Condition<T> {
boolean isFulfilledBy(T value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.constraint;
package io.github.sebastiantoepfer.jsonschema.core.codition;

import io.github.sebastiantoepfer.jsonschema.ConstraintViolation;
import java.util.Collection;
public final class NoCondition<T> implements Condition<T> {

public interface Constraint<T> {
Collection<ConstraintViolation> violationsBy(T value);
@Override
public boolean isFulfilledBy(final T value) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.constraint;
package io.github.sebastiantoepfer.jsonschema.core.codition;

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

public final class NoConstraint<T> implements Constraint<T> {
public final class UnfulfillableCondition<T> implements Condition<T> {

@Override
public Collection<ConstraintViolation> violationsBy(final T value) {
return Set.of();
public boolean isFulfilledBy(final T value) {
return false;
}
}
Loading

0 comments on commit 93862fe

Please sign in to comment.