private void registerBasicValidators() { Validator<String> noWhitespaceName = Validator.createRegexValidator("Name cannot contain whitespace", "\\S*+", Severity.ERROR); Validator<String> emptyName = Validator.createEmptyValidator("Name required"); Validator<String> uniqueName = (c, val) -> { long sameNameDescriptors = backingDescriptorList.getOrElse(FXCollections.emptyObservableList()) .stream() .map(PropertyDescriptorSpec::getName) .filter(getName()::equals) .count(); return new ValidationResult().addErrorIf(c, "The name must be unique", sameNameDescriptors > 1); }; validationSupport.registerValidator(nameField, Validator.combine(noWhitespaceName, emptyName, uniqueName)); Validator<String> noWhitespaceDescription = Validator.createRegexValidator("Message cannot be whitespace", "(\\s*+\\S.*)?", Severity.ERROR); Validator<String> emptyDescription = Validator.createEmptyValidator("Message required"); validationSupport.registerValidator(descriptionField, Validator.combine(noWhitespaceDescription, emptyDescription)); }
private void registerTypeDependentValidators(PropertyTypeId typeId) { Validator<String> valueValidator = (c, val) -> ValidationResult.fromErrorIf(valueField, "The value couldn't be parsed", Try.tryGet(() -> getValueParser(typeId).valueOf(getValue())).isFailure()); validationSupport.registerValidator(valueField, valueValidator); }
/** * Combine validation result with another. This will create a new instance of combined validation result * @param validationResult * @return new instance of combined validation result */ public ValidationResult combine( ValidationResult validationResult ) { return validationResult == null? copy(): copy().addAll(validationResult.getMessages()); }
/** * Factory method to create validation result out of collection of validation results * @param results results * @return New instance of validation result, combining all into one */ public static final ValidationResult fromResults( Collection<ValidationResult> results ) { return new ValidationResult().combineAll(results); }
/** * Factory method to create validation result out of several messages * @param messages * @return New instance of validation result */ public static final ValidationResult fromMessages( ValidationMessage... messages ) { return new ValidationResult().addAll(messages); }
/** * Factory method to create validation result out of one message * @param target validation target * @param text message text * @param severity message severity * @param condition condition on which message will be added to validation result * @return New instance of validation result */ public static final ValidationResult fromMessageIf( Control target, String text, Severity severity, boolean condition ) { return new ValidationResult().addMessageIf(target, text, severity, condition); }
/** * Factory method to create validation result out of one warning * @param target validation target * @param text message text * @param condition condition on which message will be added to validation result * @return New instance of validation result */ public static final ValidationResult fromWarningIf( Control target, String text, boolean condition ) { return new ValidationResult().addWarningIf(target, text, condition); }
/** * Combines the given validators into a single Validator instance. * @param validators the validators to combine * @return a Validator instance */ @SafeVarargs static <T> Validator<T> combine(Validator<T>... validators) { return (control, value) -> Stream.of(validators) .map(validator -> validator.apply(control, value)) .collect(Collectors.reducing(new ValidationResult(), ValidationResult::combine)); }
/** * Factory method to create a validator, which if value exists in the provided collection. * @param values text of a message to be created if value is not found * @param severity severity of a message to be created if value is found * @return new validator */ public static <T> Validator<T> createEqualsValidator(final String message, final Severity severity, final Collection<T> values) { return (c, value) -> ValidationResult.fromMessageIf(c,message,severity, !values.contains(value)); }
/** * Add one warning to validation result with condition * @param target validation target * @param text message text * @param condition condition on which warning will be added * @return updated validation result */ public ValidationResult addWarningIf( Control target, String text, boolean condition) { return addMessageIf(target,text,Severity.WARNING,condition); }
/** * Add collection of validation messages * @param messages * @return updated validation result */ public ValidationResult addAll( Collection<? extends ValidationMessage> messages ) { messages.stream().forEach( msg-> add(msg)); return this; }
/** * Add several validation messages * @param messages * @return updated validation result */ public ValidationResult addAll( ValidationMessage... messages ) { return addAll(Arrays.asList(messages)); }
/** * Combine validation result with others. This will create a new instance of combined validation result * @param validationResults * @return new instance of combined validation result */ public ValidationResult combineAll( ValidationResult... validationResults ) { return combineAll( Arrays.asList(validationResults)); }
/** * Combine validation result with others. This will create a new instance of combined validation result * @param validationResults * @return new instance of combined validation result */ public ValidationResult combineAll( Collection<ValidationResult> validationResults ) { return validationResults.stream().reduce(copy(), (x,r) -> { return r == null? x: x.addAll(r.getMessages()); }); }
/** * Factory method to create validation result out of several validation results * @param results results * @return New instance of validation result, combining all into one */ public static final ValidationResult fromResults( ValidationResult... results ) { return new ValidationResult().combineAll(results); }
/** * Factory method to create validation result out of collection of messages * @param messages * @return New instance of validation result */ public static final ValidationResult fromMessages( Collection<? extends ValidationMessage> messages ) { return new ValidationResult().addAll(messages); }
/** * Factory method to create a validator, which evaluates the value validity with a given predicate. * Error is created if the evaluation is <code>false</code>. * @param message text of a message to be created if value is invalid * @param predicate the predicate to be used for the value validity evaluation. * @param severity severity of a message to be created if value is invalid * @return new validator */ static <T> Validator<T> createPredicateValidator(Predicate<T> predicate, String message, Severity severity) { return (control, value) -> ValidationResult.fromMessageIf( control, message, severity, predicate.test(value) == false); }
/** * Add one error to validation result with condition * @param target validation target * @param text message text * @param condition condition on which error will be added * @return updated validation result */ public ValidationResult addErrorIf( Control target, String text, boolean condition) { return addMessageIf(target,text,Severity.ERROR,condition); }
/** * Add one message to validation result with condition * @param target validation target * @param text message text * @param severity message severity * @param condition condition on which message will be added * @return updated validation result */ public ValidationResult addMessageIf( Control target, String text, Severity severity, boolean condition) { return condition? add( new SimpleValidationMessage(target, text, severity)): this; }
/** * Factory method to create validation result out of one error * @param target validation target * @param text message text * @param condition condition on which message will be added to validation result * @return New instance of validation result */ public static final ValidationResult fromErrorIf( Control target, String text, boolean condition ) { return new ValidationResult().addErrorIf(target, text, condition); }