Skip to content

Move AnnotationValidationAdapter.Factory -> AnnotationValidatorFactory #18

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

Merged
merged 15 commits into from
May 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
8 changes: 3 additions & 5 deletions validator/src/main/java/io/avaje/validation/ValidPojo.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.avaje.validation;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PACKAGE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.CLASS;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.CLASS;

/**
* Marks a type for validation.
*/
Expand Down
32 changes: 14 additions & 18 deletions validator/src/main/java/io/avaje/validation/Validator.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package io.avaje.validation;

import io.avaje.validation.adapter.*;
import io.avaje.validation.core.DefaultBootstrap;
import io.avaje.validation.spi.Bootstrap;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;

import io.avaje.validation.adapter.AdapterBuildContext;
import io.avaje.validation.adapter.AnnotationValidationAdapter;
import io.avaje.validation.adapter.ValidationAdapter;
import io.avaje.validation.adapter.ValidatorComponent;
import io.avaje.validation.core.DefaultBootstrap;
import io.avaje.validation.spi.Bootstrap;

public interface Validator {

void validate(Object any) throws ConstraintViolationException;
Expand All @@ -30,22 +26,22 @@ static Builder builder() {
interface Builder {

/** Add a ValidationAdapter to use for the given type. */
<T> Builder add(Type type, ValidationAdapter<T> jsonAdapter);
<T> Builder add(Type type, ValidationAdapter<T> adapter);

/** Add a AnnotationValidationAdapter to use for the given type. */
<T> Builder add(Class<Annotation> type, AnnotationValidationAdapter<T> jsonAdapter);
<T> Builder add(Class<Annotation> type, ValidationAdapter<T> adapter);

/** Add a AdapterBuilder which provides a ValidationAdapter to use for the given type. */
Builder add(Type type, AdapterBuilder builder);

/** Add a Component which can provide multiple JsonAdapters and or configuration. */
/** Add a Component which can provide multiple ValidationAdapters and or configuration. */
Builder add(ValidatorComponent component);

/** Add a ValidationAdapter.Factory which provides JsonAdapters to use. */
Builder add(ValidationAdapter.Factory factory);
/** Add a ValidationAdapter.Factory which provides ValidationAdapters to use. */
Builder add(ValidationContext.AdapterFactory factory);

/** Add a ValidationAdapter.Factory which provides JsonAdapters to use. */
Builder add(AnnotationValidationAdapter.Factory factory);
/** Add a ValidationAdapter.Factory which provides ValidationAdapters to use. */
Builder add(ValidationContext.AnnotationFactory factory);

/**
* Build and return the Validator instance with all the given adapters and factories registered.
Expand All @@ -58,14 +54,14 @@ interface Builder {
interface AdapterBuilder {

/** Create a ValidationAdapter given the Validator instance. */
ValidationAdapter<?> build(AdapterBuildContext ctx);
ValidationAdapter<?> build(ValidationContext ctx);
}

/** Components register JsonAdapters Validator.Builder */
/** Components register ValidationAdapters Validator.Builder */
@FunctionalInterface
interface GeneratedComponent extends ValidatorComponent {

/** Register JsonAdapters with the Builder. */
/** Register ValidationAdapters with the Builder. */
@Override
void register(Builder builder);
}
Expand Down

This file was deleted.

This file was deleted.

70 changes: 70 additions & 0 deletions validator/src/main/java/io/avaje/validation/adapter/RegexFlag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.avaje.validation.adapter;

/**
*
*/
public enum RegexFlag {

/**
* Enables Unix lines mode.
*
* @see java.util.regex.Pattern#UNIX_LINES
*/
UNIX_LINES(java.util.regex.Pattern.UNIX_LINES),

/**
* Enables case-insensitive matching.
*
* @see java.util.regex.Pattern#CASE_INSENSITIVE
*/
CASE_INSENSITIVE(java.util.regex.Pattern.CASE_INSENSITIVE),

/**
* Permits whitespace and comments in pattern.
*
* @see java.util.regex.Pattern#COMMENTS
*/
COMMENTS(java.util.regex.Pattern.COMMENTS),

/**
* Enables multiline mode.
*
* @see java.util.regex.Pattern#MULTILINE
*/
MULTILINE(java.util.regex.Pattern.MULTILINE),

/**
* Enables dotall mode.
*
* @see java.util.regex.Pattern#DOTALL
*/
DOTALL(java.util.regex.Pattern.DOTALL),

/**
* Enables Unicode-aware case folding.
*
* @see java.util.regex.Pattern#UNICODE_CASE
*/
UNICODE_CASE(java.util.regex.Pattern.UNICODE_CASE),

/**
* Enables canonical equivalence.
*
* @see java.util.regex.Pattern#CANON_EQ
*/
CANON_EQ(java.util.regex.Pattern.CANON_EQ);

//JDK flag value
private final int value;

private RegexFlag(int value) {
this.value = value;
}

/**
* @return flag value as defined in {@link java.util.regex.Pattern}
*/
public int getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package io.avaje.validation.adapter;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Objects;

import io.avaje.validation.Validator;

public interface ValidationAdapter<T> {

/** Return true if validation should recurse */
Expand All @@ -15,37 +12,37 @@ 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);
default boolean validateAll(Collection<T> value, ValidationRequest req, String propertyName) {
if (propertyName != null) {
req.pushPath(propertyName);
}
int index = -1;
for (final T element : value) {
index++;
validate(element, req, String.valueOf(index));
validate(element, req, String.valueOf(index));
}
if (propertName != null) {
if (propertyName != null) {
req.popPath();
}
return true;
}

default boolean validateAll(T[] value, ValidationRequest req, String propertName) {
if (propertName != null) {
req.pushPath(propertName);
default boolean validateAll(T[] value, ValidationRequest req, String propertyName) {
if (propertyName != null) {
req.pushPath(propertyName);
}
int index = -1;
for (final T element : value) {
index++;
validate(element, req, String.valueOf(index));
validate(element, req, String.valueOf(index));
}
if (propertName != null) {
if (propertyName != null) {
req.popPath();
}
return true;
}

default AnnotationValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
default ValidationAdapter<T> andThen(ValidationAdapter<? super T> after) {
Objects.requireNonNull(after);
return (value, req, propertyName) -> {
if (validate(value, req, propertyName)) {
Expand All @@ -55,14 +52,4 @@ default AnnotationValidationAdapter<T> andThen(ValidationAdapter<? super T> afte
};
}

/** Factory for creating a ValidationAdapter. */
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, AdapterBuildContext ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.avaje.validation.adapter;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Map;

public interface ValidationContext {

/**
* Return the adapter for the given type.
*/
<T> ValidationAdapter<T> adapter(Class<T> cls);

/**
* Return the adapter for the given type.
*/
<T> ValidationAdapter<T> adapter(Type type);

/**
* Return the adapter for the given annotation with attributes.
*/
<T> ValidationAdapter<T> adapter(Class<? extends Annotation> cls, Map<String, Object> attributes);

/**
* Return the message to use given the global key and attributes.
* @param key Used to lookup the fallback default message format to use
* @param attributes Attributes that could contain user defined message to use
*/
String message(String key, Map<String, Object> attributes);

/**
* Factory for creating a ValidationAdapter for a given type.
*/
interface AdapterFactory {

/**
* 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, ValidationContext ctx);
}

/**
* Factory for creating a ValidationAdapter for a given annotation.
*/
interface AnnotationFactory {

/**
* 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(
Class<? extends Annotation> annotationType, ValidationContext ctx, Map<String, Object> attributes);
}
}
Loading