-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from avaje/feature/validation-request
Add ConstraintViolationException and ValidationRequest
- Loading branch information
Showing
27 changed files
with
631 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
validator/src/main/java/io/avaje/validation/ConstraintViolationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.avaje.validation; | ||
|
||
import java.util.Set; | ||
|
||
public final class ConstraintViolationException extends RuntimeException { | ||
|
||
private final Set<ConstraintViolation> violations; | ||
|
||
public ConstraintViolationException(Set<ConstraintViolation> violations) { | ||
this.violations = violations; | ||
} | ||
|
||
public Set<ConstraintViolation> violations() { | ||
return violations; | ||
} | ||
} |
35 changes: 0 additions & 35 deletions
35
validator/src/main/java/io/avaje/validation/ValidationAdapter.java
This file was deleted.
Oops, something went wrong.
7 changes: 1 addition & 6 deletions
7
validator/src/main/java/io/avaje/validation/ValidationType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
package io.avaje.validation; | ||
|
||
import java.util.Set; | ||
|
||
|
||
public interface ValidationType<T> { | ||
|
||
Set<ConstraintViolation> validate(T object); | ||
|
||
void validate(T object, Set<ConstraintViolation> violations); | ||
void validate(T object) throws ConstraintViolationException; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
validator/src/main/java/io/avaje/validation/adapter/CoreValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.avaje.validation.adapter; | ||
|
||
import java.util.Collection; | ||
|
||
public interface CoreValidation { | ||
|
||
/** Return true to continue validation on this value if needed */ | ||
boolean required(Object value, ValidationRequest ctx, String propertyName); | ||
|
||
boolean collection(ValidationRequest ctx, String propertyName, Collection<?> collection, int minSize, int maxSize); | ||
} |
20 changes: 20 additions & 0 deletions
20
validator/src/main/java/io/avaje/validation/adapter/ScalarValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.avaje.validation.adapter; | ||
|
||
public interface ScalarValidator<T> { | ||
|
||
/** Return true to continue validation on this value if needed */ | ||
boolean optional(ValidationRequest ctx, T value); | ||
|
||
/** Return true to continue validation on this value if needed */ | ||
boolean required(ValidationRequest ctx, T value); | ||
|
||
/** Return true to continue validation on this value if needed */ | ||
boolean optional(ValidationRequest ctx, T value, int minLength, int maxLength); | ||
|
||
/** Return true to continue validation on this value if needed */ | ||
boolean required(ValidationRequest ctx, T value, int minLength, int maxLength); | ||
|
||
boolean min(ValidationRequest ctx, T value, T minValue); | ||
boolean max(ValidationRequest ctx, T value, T maxValue); | ||
boolean range(ValidationRequest ctx, T value, T minValue, T maxValue); | ||
} |
67 changes: 67 additions & 0 deletions
67
validator/src/main/java/io/avaje/validation/adapter/ValidationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2014 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.avaje.validation.adapter; | ||
|
||
import io.avaje.validation.Validator; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
|
||
public interface ValidationAdapter<T> { | ||
|
||
/** Return true if validation should recurse */ | ||
boolean validate(T value, ValidationRequest req, String propertyName); | ||
|
||
default boolean validate(T value, ValidationRequest req) { | ||
return validate(value, req, null); | ||
} | ||
|
||
default boolean validateAll(Collection<T> value, ValidationRequest req, String propertName) { | ||
if (propertName != null) { | ||
req.pushPath(propertName); | ||
} | ||
int index = -1; | ||
for (T element : value) { | ||
validate(element, req, String.valueOf(++index)); | ||
} | ||
if (propertName != null) { | ||
req.popPath(); | ||
} | ||
return true; | ||
} | ||
|
||
default AnnotationValidationAdapter<T> andThen(ValidationAdapter<? super T> after) { | ||
Objects.requireNonNull(after); | ||
return (value, req, propertyName) -> { | ||
if (validate(value, req, propertyName)) { | ||
return after.validate(value, req, propertyName); | ||
} | ||
return true; | ||
}; | ||
} | ||
|
||
/** Factory for creating a ValidationAdapter. */ | ||
public interface Factory { | ||
|
||
/** | ||
* Create and return a ValidationAdapter given the type and annotations or return null. | ||
* | ||
* <p>Returning null means that the adapter could be created by another factory. | ||
*/ | ||
ValidationAdapter<?> create(Type type, Validator jsonb); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
validator/src/main/java/io/avaje/validation/adapter/ValidationRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.avaje.validation.adapter; | ||
|
||
import io.avaje.validation.ConstraintViolation; | ||
import io.avaje.validation.ConstraintViolationException; | ||
|
||
import java.util.*; | ||
|
||
public class ValidationRequest { | ||
|
||
private final ArrayDeque<String> pathStack = new ArrayDeque<>(); | ||
|
||
private final Set<ConstraintViolation> violations = new LinkedHashSet<>(); | ||
|
||
|
||
public void addViolation(String msg, String propertyName) { | ||
violations.add(new ConstraintViolation(currentPath(), propertyName, msg)); | ||
} | ||
|
||
private String currentPath() { | ||
StringJoiner joiner = new StringJoiner("."); | ||
final var descendingIterator = pathStack.descendingIterator(); | ||
while (descendingIterator.hasNext()) { | ||
joiner.add(descendingIterator.next()); | ||
} | ||
return joiner.toString(); | ||
} | ||
|
||
|
||
public void pushPath(String path) { | ||
pathStack.push(path); | ||
} | ||
|
||
public void popPath() { | ||
pathStack.pop(); | ||
} | ||
|
||
public void throwWithViolations() { | ||
if (!violations.isEmpty()) { | ||
throw new ConstraintViolationException(violations); | ||
} | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
.../avaje/validation/ValidatorComponent.java → ...alidation/adapter/ValidatorComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
validator/src/main/java/io/avaje/validation/core/DCoreValidation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.avaje.validation.core; | ||
|
||
import io.avaje.validation.adapter.CoreValidation; | ||
import io.avaje.validation.adapter.ValidationRequest; | ||
|
||
import java.util.Collection; | ||
|
||
final class DCoreValidation implements CoreValidation { | ||
|
||
@Override | ||
public boolean required(Object value, ValidationRequest ctx, String propertyName) { | ||
if (value == null) { | ||
ctx.addViolation("Required", propertyName); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean collection(ValidationRequest ctx, String propertyName, Collection<?> collection, int minSize, int maxSize) { | ||
if (collection == null) { | ||
if (minSize != -1) { | ||
ctx.addViolation("CollectionNull", propertyName); | ||
} | ||
return false; | ||
} | ||
final int size = collection.size(); | ||
if (size < minSize) { | ||
ctx.addViolation("CollectionMinSize", propertyName); | ||
} | ||
if (maxSize > 0 && size > maxSize) { | ||
ctx.addViolation("CollectionMaxSize", propertyName); | ||
} | ||
// also validate the collection elements | ||
return size > 0; | ||
} | ||
} |
Oops, something went wrong.