weixin_39615499
weixin_39615499
2020-11-28 16:29

V8 suggestion: allow null as argument to ValidationResult.error or make SimpleValidationResult constructor public

Small improvement would be to allow null as argument to ValidationResult.error here or make SimpleValidationResult constructor public.

It is common to have methods that validate stuff and return either error message or null - validation package has no idea that vaadin exists nor should it. Right now I have to do either of these:

`java
public ValidationResult apply(String value, ValueContext context) {
    String error = actualValidation(value);
    return new CopyPaste_Of_SimpleValidationResult(error);
}

// Whole SimpleValidationResult is copy pasted somewhere here

public ValidationResult apply(String value, ValueContext context) {
    String error = actualValidation(value);
    if (error == null) {
        return ValidationResult.ok();
    } else {
        return ValidationResult.error(error);
    }
}

`

Following would be more elegant:

`java
public ValidationResult apply(String value, ValueContext context) {
    String error = actualValidation(value);
    return new ValidationResult.SimpleValidationResult(error);
}

//or
public ValidationResult apply(String value, ValueContext context) {
    String error = actualValidation(value);
    return ValidationResult.error(error);
}

`

That check can not prevent a bug nor anything like that, it just forces user to write a bit more boilerplate.

该提问来源于开源项目:vaadin/framework

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39688687 weixin_39688687 5月前

    The latter is terrible - you use a factory method that is supposed to create an error result, but make it actually return an OK result.

    If a factory method is supposed to behave that way, it should be something different, like for example ValidationResult.fromErrorMessage(String message)

    点赞 评论 复制链接分享
  • weixin_39616348 weixin_39616348 5月前

    The recommended way to use Validators is to use the factory methods provided by us to remove all the boilerplate. Currently the most flexible way is to provide a Predicate<VALUE> that contains the code that checks if the validation is ok or not and an ErrorMessageProvider that takes a ValueContext and returns the error message as a String.

    Is some information missing from the ValueContext that you'd need to create the error message in this case? Is there something else you can't achieve with these?

    We can extend the ValueContext to provide more information and add an option to use a BiPredicate to pass in the value and the context for the validation if needed.

    点赞 评论 复制链接分享
  • weixin_39875842 weixin_39875842 5月前

    Hello there!

    It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.

    There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):

    • Check if the issue is still valid for the latest version. There are dozens of duplicates in our issue tracker, so it is possible that the issue is already tackled. If it appears to be fixed, close the issue, otherwise report to the issue that it is still valid.
    • Provide more details how to reproduce the issue.
    • Explain why it is important to get this issue fixed and politely draw others attention to it e.g. via the forum or social media.
    • Add a reduced test case about the issue, so it is easier for somebody to start working on a solution.
    • Try fixing the issue yourself and create a pull request that contains the test case and/or a fix for it. Handling the pull requests is the top priority for the core team.
    • If the issue is clearly a bug, use the Warranty in your Vaadin subscription to raise its priority.

    Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!

    点赞 评论 复制链接分享
  • weixin_39875842 weixin_39875842 5月前

    The issue was automatically closed due to inactivity. If you found some new details to it or started working on it, comment on the issue so that maintainers can re-open it.

    点赞 评论 复制链接分享

相关推荐