|
| 1 | +/* |
| 2 | + * Jooby https://jooby.io |
| 3 | + * Apache License Version 2.0 https://jooby.io/LICENSE.txt |
| 4 | + * Copyright 2014 Edgar Espina |
| 5 | + */ |
| 6 | +package io.jooby.avaje.validator; |
| 7 | + |
| 8 | +import java.time.Duration; |
| 9 | +import java.time.temporal.ChronoUnit; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.Locale; |
| 13 | +import java.util.Optional; |
| 14 | +import java.util.function.Consumer; |
| 15 | + |
| 16 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 17 | +import io.avaje.validation.ConstraintViolationException; |
| 18 | +import io.avaje.validation.Validator; |
| 19 | +import io.jooby.Extension; |
| 20 | +import io.jooby.Jooby; |
| 21 | +import io.jooby.StatusCode; |
| 22 | +import io.jooby.validation.MvcValidator; |
| 23 | + |
| 24 | +/** |
| 25 | + * Avaje Validator Module: https://jooby.io/modules/avaje-validator. |
| 26 | + * |
| 27 | + * <pre>{@code |
| 28 | + * { |
| 29 | + * install(new AvajeValidatorModule()); |
| 30 | + * |
| 31 | + * } |
| 32 | + * |
| 33 | + * public class Controller { |
| 34 | + * |
| 35 | + * @POST("/create") |
| 36 | + * public void create(@Valid Bean bean) { |
| 37 | + * } |
| 38 | + * |
| 39 | + * } |
| 40 | + * }</pre> |
| 41 | + * |
| 42 | + * <p>Supports validation of a single bean, list, array, or map. |
| 43 | + * |
| 44 | + * <p>The module also provides a built-in error handler that catches {@link |
| 45 | + * ConstraintViolationException} and transforms it into a {@link |
| 46 | + * io.jooby.validation.ValidationResult} |
| 47 | + * |
| 48 | + * @authors kliushnichenko, SentryMan |
| 49 | + * @since 3.2.10 |
| 50 | + */ |
| 51 | +public class AvajeValidatorModule implements Extension { |
| 52 | + |
| 53 | + private Consumer<Validator.Builder> configurer; |
| 54 | + private StatusCode statusCode = StatusCode.UNPROCESSABLE_ENTITY; |
| 55 | + private String title = "Validation failed"; |
| 56 | + private boolean disableDefaultViolationHandler = false; |
| 57 | + |
| 58 | + /** |
| 59 | + * Setups a configurer callback. |
| 60 | + * |
| 61 | + * @param configurer Configurer callback. |
| 62 | + * @return This module. |
| 63 | + */ |
| 64 | + public AvajeValidatorModule doWith(@NonNull final Consumer<Validator.Builder> configurer) { |
| 65 | + this.configurer = configurer; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Overrides the default status code for the errors produced by validation. Default code is |
| 71 | + * UNPROCESSABLE_ENTITY(422) |
| 72 | + * |
| 73 | + * @param statusCode new status code |
| 74 | + * @return This module. |
| 75 | + */ |
| 76 | + public AvajeValidatorModule statusCode(@NonNull StatusCode statusCode) { |
| 77 | + this.statusCode = statusCode; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Overrides the default title for the errors produced by validation. Default title is "Validation |
| 83 | + * failed" |
| 84 | + * |
| 85 | + * @param title new title |
| 86 | + * @return This module. |
| 87 | + */ |
| 88 | + public AvajeValidatorModule validationTitle(@NonNull String title) { |
| 89 | + this.title = title; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Disables default constraint violation handler. By default {@link AvajeValidatorModule} provides |
| 95 | + * built-in error handler for the {@link ConstraintViolationException} Such exceptions are |
| 96 | + * transformed into response of {@link io.jooby.validation.ValidationResult} Use this flag to |
| 97 | + * disable default error handler and provide your custom. |
| 98 | + * |
| 99 | + * @return This module. |
| 100 | + */ |
| 101 | + public AvajeValidatorModule disableViolationHandler() { |
| 102 | + this.disableDefaultViolationHandler = true; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void install(@NonNull Jooby app) throws Exception { |
| 108 | + |
| 109 | + var props = app.getEnvironment(); |
| 110 | + |
| 111 | + final var locales = new ArrayList<Locale>(); |
| 112 | + final var builder = Validator.builder(); |
| 113 | + Optional.ofNullable(props.getProperty("validation.failFast", "false")) |
| 114 | + .map(Boolean::valueOf) |
| 115 | + .ifPresent(builder::failFast); |
| 116 | + |
| 117 | + Optional.ofNullable(props.getProperty("validation.resourcebundle.names")) |
| 118 | + .map(s -> s.split(",")) |
| 119 | + .ifPresent(builder::addResourceBundles); |
| 120 | + |
| 121 | + Optional.ofNullable(props.getProperty("validation.locale.default")) |
| 122 | + .map(Locale::forLanguageTag) |
| 123 | + .ifPresent( |
| 124 | + l -> { |
| 125 | + builder.setDefaultLocale(l); |
| 126 | + locales.add(l); |
| 127 | + }); |
| 128 | + |
| 129 | + Optional.ofNullable(props.getProperty("validation.locale.addedLocales")).stream() |
| 130 | + .flatMap(s -> Arrays.stream(s.split(","))) |
| 131 | + .map(Locale::forLanguageTag) |
| 132 | + .forEach( |
| 133 | + l -> { |
| 134 | + builder.addLocales(l); |
| 135 | + locales.add(l); |
| 136 | + }); |
| 137 | + |
| 138 | + Optional.ofNullable(props.getProperty("validation.temporal.tolerance.value")) |
| 139 | + .map(Long::valueOf) |
| 140 | + .ifPresent( |
| 141 | + duration -> { |
| 142 | + final var unit = |
| 143 | + Optional.ofNullable(props.getProperty("validation.temporal.tolerance.chronoUnit")) |
| 144 | + .map(ChronoUnit::valueOf) |
| 145 | + .orElse(ChronoUnit.MILLIS); |
| 146 | + builder.temporalTolerance(Duration.of(duration, unit)); |
| 147 | + }); |
| 148 | + |
| 149 | + if (configurer != null) { |
| 150 | + configurer.accept(builder); |
| 151 | + } |
| 152 | + |
| 153 | + Validator validator = builder.build(); |
| 154 | + app.getServices().put(Validator.class, validator); |
| 155 | + app.getServices().put(MvcValidator.class, new MvcValidatorImpl(validator)); |
| 156 | + |
| 157 | + if (!disableDefaultViolationHandler) { |
| 158 | + app.error( |
| 159 | + ConstraintViolationException.class, new ConstraintViolationHandler(statusCode, title)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + static class MvcValidatorImpl implements MvcValidator { |
| 164 | + |
| 165 | + private final Validator validator; |
| 166 | + |
| 167 | + MvcValidatorImpl(Validator validator) { |
| 168 | + this.validator = validator; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void validate(Object bean) throws ConstraintViolationException { |
| 173 | + validator.validate(bean); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments