/** * Sets the maximum permissible length of the string. * * @param maxLength * the maximum length to accept or null for no limit */ public void setMaxLength(Integer maxLength) { validator.setMaxValue(maxLength); }
/** * Creates a simple message interpolation context based on the given * constraint violation. * * @param violation * the constraint violation * @return the message interpolation context */ protected Context createContext(ConstraintViolation<?> violation) { return new ContextImpl(violation); }
/** * Returns whether the given value lies in the valid range. * * @param value * the value to validate * @return true if the value is valid, false otherwise */ protected boolean isValid(T value) { if (value == null) { return true; } if (getMinValue() != null) { int result = comparator.compare(value, getMinValue()); if (result < 0) { return false; } else if (result == 0 && !isMinValueIncluded()) { return false; } } if (getMaxValue() != null) { int result = comparator.compare(value, getMaxValue()); if (result > 0) { return false; } else if (result == 0 && !isMaxValueIncluded()) { return false; } } return true; } }
/** * Returns {@code Result.ok} if the value is within the specified bounds, * {@code Result.error} otherwise. If null is passed to {@code apply}, the * behavior depends on the used comparator. */ @Override public ValidationResult apply(T value, ValueContext context) { return toResult(value, isValid(value)); }
@Override public String toString() { return String.format("%s[%d, %d]", getClass().getSimpleName(), getMinLength(), getMaxLength()); }
/** * Returns the interpolated error message for the given constraint violation * using the locale specified for this validator. * * @param violation * the constraint violation * @param locale * the used locale * @return the localized error message */ protected String getMessage(ConstraintViolation<?> violation, Locale locale) { return getJavaxBeanValidatorFactory().getMessageInterpolator() .interpolate(violation.getMessageTemplate(), createContext(violation), locale); }
/** * Gets the maximum permissible length of the string. * * @return the maximum length of the string or null if there is no limit */ public Integer getMaxLength() { return validator.getMaxValue(); }
/** * Gets the minimum permissible length of the string. * * @return the minimum length of the string or null if there is no limit */ public Integer getMinLength() { return validator.getMinValue(); }
/** * Creates a new StringLengthValidator with a given error message and * minimum and maximum length limits. * * @param errorMessage * the error message to return if validation fails * @param minLength * the minimum permissible length of the string or null for no * limit. * @param maxLength * the maximum permissible length of the string or null for no * limit. */ public StringLengthValidator(String errorMessage, Integer minLength, Integer maxLength) { super(errorMessage); validator = RangeValidator.of(errorMessage, minLength, maxLength); }
/** * Returns a shared JSR-303 validator instance to use. * * @return the validator to use */ public javax.validation.Validator getJavaxBeanValidator() { return getJavaxBeanValidatorFactory().getValidator(); }
/** * Returns whether the given string matches the regular expression. * * @param value * the string to match * @return true if the string matched, false otherwise */ protected boolean isValid(String value) { if (value == null) { return true; } if (complete) { return getMatcher(value).matches(); } else { return getMatcher(value).find(); } }
/** * Returns a {@code RangeValidator} comparing values of a {@code Comparable} * type using their <i>natural order</i>. Passing null to either * {@code minValue} or {@code maxValue} means there is no limit in that * direction. Both limits may be null; this can be useful if the limits are * resolved programmatically. * <p> * Null is considered to be less than any non-null value. This means null * never passes validation if a minimum value is specified. * * @param <C> * the {@code Comparable} value type * @param errorMessage * the error message to return if validation fails, not null * @param minValue * the least value of the accepted range or null for no limit * @param maxValue * the greatest value of the accepted range or null for no limit * @return the new validator */ public static <C extends Comparable<? super C>> RangeValidator<C> of( String errorMessage, C minValue, C maxValue) { return new RangeValidator<>(errorMessage, Comparator.nullsFirst(Comparator.naturalOrder()), minValue, maxValue); }
/** * A helper method for creating a {@code Result} from a value and a validity * flag. If the flag is true, returns {@code Result.ok}, otherwise yields * {@code Result.error} bearing the error message returned by * {@link #getMessage(T)}. * <p> * For instance, the following {@code apply} method only accepts even * numbers: * * <pre> * @Override * public Result<T> apply(Integer value) { * return toResult(value, value % 2 == 0); * } * </pre> * * @param value * the validated value * @param isValid * whether the value is valid or not * @return the validation result */ protected ValidationResult toResult(T value, boolean isValid) { return isValid ? ValidationResult.ok() : ValidationResult.error(getMessage(value)); } }
@Override protected BindingBuilder<BEAN, ?> configureBinding( BindingBuilder<BEAN, ?> binding, PropertyDefinition<BEAN, ?> definition) { Class<?> actualBeanType = findBeanType(beanType, definition); BeanValidator validator = new BeanValidator(actualBeanType, definition.getTopLevelName()); if (requiredConfigurator != null) { configureRequired(binding, definition, validator); } return binding.withValidator(validator); }
@Override protected RangeValidator<LocalDateTime> getRangeValidator() { return new DateTimeRangeValidator(getDateOutOfRangeMessage(), getDate(getRangeStart(), getResolution()), getDate(getRangeEnd(), getResolution())); }
@Override protected RangeValidator<LocalDate> getRangeValidator() { return new DateRangeValidator(getDateOutOfRangeMessage(), getDate(getRangeStart(), getResolution()), getDate(getRangeEnd(), getResolution())); }
@Override public String toString() { T min = getMinValue(); T max = getMaxValue(); return String.format("%s %c%s, %s%c", getClass().getSimpleName(), isMinValueIncluded() ? '[' : '(', min != null ? min : "-∞", max != null ? max : "∞", isMaxValueIncluded() ? ']' : ')'); }
/** * Sets the minimum permissible length. * * @param minLength * the minimum length to accept or null for no limit */ public void setMinLength(Integer minLength) { validator.setMaxValue(minLength); }