@Override public Result<T> convertToModel(T value, ValueContext context) { ValidationResult validationResult = validator.apply(value, context); return new ValidationResultWrap<>(value, validationResult); }
/** * Builds a validator out of a conditional function and an error message * provider. If the function returns true, the validator returns * {@code Result.ok()}; if it returns false or throws an exception, * {@code Result.error()} is returned with the message from the provider. * * @param <T> * the value type * @param guard * the function used to validate, not null * @param errorMessageProvider * the provider to generate error messages, not null * @return the new validator using the function */ public static <T> Validator<T> from(SerializablePredicate<T> guard, ErrorMessageProvider errorMessageProvider) { return from(guard, errorMessageProvider, ErrorLevel.ERROR); }
/** * Returns a validator that checks the internal state of the HasValue. This * should be overridden for components with internal value conversion or * validation, e.g. when the user is providing a string that has to be * parsed into a date. An invalid input from user will be exposed to a * {@link Binder} and can be seen as a validation failure. * * @since 8.1 * @return internal state validator * @see Binder#validate() */ public default Validator<V> getDefaultValidator() { return Validator.alwaysPass(); } }
private static void validate(Validator validator, Object value, List<InvalidValueException> exceptionList) { try { validator.validate(value); // TODO do it more dynamically } catch (Validator.InvalidValueException ex) { exceptionList.add(ex); } } }
public boolean isValid(PC value) { if (validators == null) { return true; } for (Validator validator : validators) { if (!validator.isValid(value)) { return false; } } return true; }
@Override public void validate(T value) throws com.holonplatform.core.Validator.ValidationException { try { validator.validate(value); } catch (InvalidValueException e) { throw ValidationUtils.translateValidationException(e); } }
/** * Tests the current value against all registered validators. * * @return <code>true</code> if all registered validators claim that the * current value is valid, <code>false</code> otherwise. */ public boolean isValid() { if (isEmpty()) { if (isRequired()) { return false; } else { return true; } } if (validators == null) { return true; } final Object value = getValue(); for (final Iterator<Validator> i = validators.iterator(); i.hasNext();) { if (!(i.next()).isValid(value)) { return false; } } return true; }
protected void validateInternal(com.vaadin.data.Validator validator, Object itemId, Object value, ValidationResult result) { try { validator.validate(value); } catch (com.vaadin.data.Validator.InvalidValueException ex) { result.add(itemId, ex); } }
String errorMessage, ErrorLevel errorLevel) { Objects.requireNonNull(errorMessage, "errorMessage cannot be null"); return from(guard, ctx -> errorMessage, errorLevel);
/** * Validates the {@code bean} using validators added using * {@link #withValidator(Validator)} and returns the result of the * validation as a list of validation results. * <p> * * @see #withValidator(Validator) * * @param bean * the bean to validate * @return a list of validation errors or an empty list if validation * succeeded */ private List<ValidationResult> validateBean(BEAN bean) { Objects.requireNonNull(bean, "bean cannot be null"); List<ValidationResult> results = Collections.unmodifiableList(validators .stream() .map(validator -> validator.apply(bean, new ValueContext())) .collect(Collectors.toList())); return results; }
@Override public void validate(final Object value) { if (StringUtils.isEmpty(value)) { throw new InvalidValueException(message); } try { rangeValidator.validate(Integer.parseInt(value.toString())); } catch (final RuntimeException e) { LOGGER.debug("Action expiry validation failed", e); throw new InvalidValueException(message); } }
/** * Builds a validator out of a conditional function and an error message. If * the function returns true, the validator returns {@code Result.ok()}; if * it returns false or throws an exception, * {@link ValidationResult#error(String)} is returned with the given message * and error level {@link ErrorLevel#ERROR}. * <p> * For instance, the following validator checks if a number is between 0 and * 10, inclusive: * * <pre> * Validator<Integer> v = Validator.from(num -> num >= 0 && num <= 10, * "number must be between 0 and 10"); * </pre> * * @param <T> * the value type * @param guard * the function used to validate, not null * @param errorMessage * the message returned if validation fails, not null * @return the new validator using the function */ public static <T> Validator<T> from(SerializablePredicate<T> guard, String errorMessage) { Objects.requireNonNull(errorMessage, "errorMessage cannot be null"); return from(guard, ctx -> errorMessage); }
@Override public void validate(T value) throws ValidationException { ValidationResult result = validator.apply(value, getValueContext()); if (result.isError()) { throw new ValidationException(result.getErrorMessage()); } }
@Override public void validate(final Object value) { if (StringUtils.isEmpty(value)) { throw new InvalidValueException(message); } try { rangeValidator.validate(Integer.parseInt(value.toString())); } catch (final RuntimeException e) { LOGGER.debug("Action expiry validation failed", e); throw new InvalidValueException(message); } }
@Override public BindingBuilder<BEAN, TARGET> asRequired( ErrorMessageProvider errorMessageProvider) { return asRequired(Validator.from( value -> !Objects.equals(value, field.getEmptyValue()), errorMessageProvider)); }
public void validate(PC value) throws Validator.InvalidValueException { // If there is no validator, there can not be any errors if (validators == null) { return; } // Initialize temps List<InvalidValueException> errors = new ArrayList<InvalidValueException>(); // validate the underlying value, not the formatted value // Gets all the validation errors for (Validator validator : validators) { try { validator.validate(value); } catch (Validator.InvalidValueException e) { errors.add(e); } } // If there were no error if (errors.isEmpty()) { return; } // If only one error occurred, throw it forwards if (errors.size() == 1) { throw errors.get(0); } // Creates composite validation exception final Validator.InvalidValueException[] exceptions = errors.toArray(new Validator.InvalidValueException[errors.size()]); throw new Validator.InvalidValueException(null, exceptions); }
/** * A convenience method to add a validator to this binding using the * {@link Validator#from(SerializablePredicate, ErrorMessageProvider)} * factory method. * <p> * Validators are applied, in registration order, when the field value * is written to the backing property. If any validator returns a * failure, the property value is not updated. * * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, String) * @see #withValidator(SerializablePredicate, ErrorMessageProvider, * ErrorLevel) * @see Validator#from(SerializablePredicate, ErrorMessageProvider) * * @param predicate * the predicate performing validation, not null * @param errorMessageProvider * the provider to generate error messages, not null * @return this binding, for chaining * @throws IllegalStateException * if {@code bind} has already been called */ public default BindingBuilder<BEAN, TARGET> withValidator( SerializablePredicate<? super TARGET> predicate, ErrorMessageProvider errorMessageProvider) { return withValidator( Validator.from(predicate, errorMessageProvider)); }
(i.next()).validate(value); } catch (final Validator.InvalidValueException e) { if (firstError == null) {
/** * A convenience method to add a validator to this binder using the * {@link Validator#from(SerializablePredicate, String)} factory method. * <p> * Bean level validators are applied on the bean instance after the bean is * updated. If the validators fail, the bean instance is reverted to its * previous state. * * @see #writeBean(Object) * @see #writeBeanIfValid(Object) * @see #withValidator(Validator) * @see #withValidator(SerializablePredicate, ErrorMessageProvider) * * @param predicate * the predicate performing validation, not null * @param message * the error message to report in case validation failure * @return this binder, for chaining */ public Binder<BEAN> withValidator(SerializablePredicate<BEAN> predicate, String message) { return withValidator(Validator.from(predicate, message)); }
if (validators != null) { for (Validator v : validators) { v.validate(newValue);