Skip to content

Recursive Bundle Lookup #79

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 3 commits into from
Jul 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import io.avaje.inject.BeanScopeBuilder;

/** Plugin for avaje inject that provides a default Http Validator instance. */
public final class DefaultValidatorProvider implements io.avaje.inject.spi.Plugin {
public final class HttpValidatorProvider implements io.avaje.inject.spi.Plugin {

@Override
public Class<?>[] provides() {
Expand Down
4 changes: 3 additions & 1 deletion validator-http-plugin/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module io.avaje.validation.http {

exports io.avaje.validation.http;

requires transitive io.avaje.validation.plugin;
requires transitive io.avaje.http.api;

provides io.avaje.inject.spi.Plugin with io.avaje.validation.http.DefaultValidatorProvider;
provides io.avaje.inject.spi.Plugin with io.avaje.validation.http.HttpValidatorProvider;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.avaje.validation.http.DefaultValidatorProvider
io.avaje.validation.http.HttpValidatorProvider
7 changes: 7 additions & 0 deletions validator/src/main/java/io/avaje/validation/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.avaje.lang.Nullable;
import io.avaje.validation.adapter.*;
import io.avaje.validation.core.DefaultBootstrap;
import io.avaje.validation.spi.MessageInterpolator;
import io.avaje.validation.spi.ValidatorCustomizer;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -96,6 +97,11 @@ interface Builder {
/** Define the acceptable margin of error when comparing date/time in temporal constraints. */
Builder temporalTolerance(Duration temporalTolerance);

/**
* Set the MessageInterpolator that will be used to parse and interpolate error messages
*/
Builder messageInterpolator(MessageInterpolator interpolator);

/**
* Enable/Disable fail fast mode. When fail fast is enabled the validation will stop on the
* first constraint violation detected.
Expand Down Expand Up @@ -123,6 +129,7 @@ interface Builder {
* Build and return the Validator instance with all the given adapters and factories registered.
*/
Validator build();

}

/** Function to build a ValidationAdapter from a Validation Context */
Expand Down
23 changes: 16 additions & 7 deletions validator/src/main/java/io/avaje/validation/core/DValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.ServiceLoader;
import java.util.Set;
Expand Down Expand Up @@ -172,11 +173,12 @@ static final class DBuilder implements Validator.Builder {
private final List<AnnotationFactory> afactories = new ArrayList<>();
private final List<String> bundleNames = new ArrayList<>();
private final List<ResourceBundle> bundles = new ArrayList<>();
private final List<Locale> otherLocals = new ArrayList<>();
private Locale defaultLocal = Locale.getDefault();
private final List<Locale> otherLocales = new ArrayList<>();
private Locale defaultLocale = Locale.getDefault();
private Supplier<Clock> clockSupplier = Clock::systemDefaultZone;
private Duration temporalTolerance = Duration.ZERO;
private boolean failfast;
private MessageInterpolator userInterpolator;

@Override
public Builder add(Type type, AdapterBuilder builder) {
Expand Down Expand Up @@ -230,13 +232,13 @@ public Builder addResourceBundles(ResourceBundle... bundle) {

@Override
public Builder setDefaultLocale(Locale defaultLocal) {
this.defaultLocal = defaultLocal;
this.defaultLocale = defaultLocal;
return this;
}

@Override
public Builder addLocales(Locale... locals) {
Collections.addAll(otherLocals, locals);
Collections.addAll(otherLocales, locals);
return this;
}

Expand All @@ -258,6 +260,12 @@ public Builder failFast(boolean failfast) {
return this;
}

@Override
public Builder messageInterpolator(MessageInterpolator interpolator) {
this.userInterpolator = interpolator;
return this;
}

private void registerComponents() {
// first register all user defined ValidatorCustomizer
for (final ValidatorCustomizer next : ServiceLoader.load(ValidatorCustomizer.class)) {
Expand All @@ -272,11 +280,12 @@ private void registerComponents() {
public DValidator build() {
registerComponents();

final var localeResolver = new LocaleResolver(defaultLocal, otherLocals);
final var localeResolver = new LocaleResolver(defaultLocale, otherLocales);
final var interpolator =
ServiceLoader.load(MessageInterpolator.class)
.findFirst()
Optional.ofNullable(this.userInterpolator)
.or(() -> ServiceLoader.load(MessageInterpolator.class).findFirst())
.orElseGet(BasicMessageInterpolator::new);

return new DValidator(
factories,
afactories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ String lookup(String template, Locale resolvedLocale) {
final String key = template.substring(1, template.length() - 1);
final String msg = bundleManager.message(key, resolvedLocale);
if (msg != null) {
return msg;
return lookup(msg, resolvedLocale);
}
return template;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public interface MessageInterpolator {
/**
* Interpolate the given message with the annotation attributes
*
* @param template template
* @param attributes
* @return the interpolated validation error message
* @param template The template loaded from annotation/resourceBundle
* @param attributes The Constraint annotation's attributes
* @return The interpolated validation error message
*/
String interpolate(String template, Map<String, Object> attributes);
}