Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to pass in message when using validate with boolean condition #1

Closed
anjo-swe opened this issue Nov 8, 2016 · 1 comment

Comments

@anjo-swe
Copy link

anjo-swe commented Nov 8, 2016

Currently there's no way to pass in String message when using a boolean condition.

public static <X extends Exception> void validate(boolean condition, ExceptionFactory<X> factory) throws X {
    if (!condition) {
      throw factory.newInstance(null);
    }
}
@helsing
Copy link
Member

helsing commented Nov 13, 2016

You could achieve it via creating a decorator class for the exception factory. Like this:

  private static <X extends Exception> ExceptionFactory<X> failWith(final ExceptionFactory<X> xFactory, final String message) {
    // (Obviously we could use a lambda expression instead, when in Java8)
    return new ExceptionFactory<X>() {
      @Override
      public X newInstance(String ignored) {
        return xFactory.newInstance(message);
      }
    };
  }

and then use it like this:

  import static org.valid4j.ExceptionFactories.exception;

  @Test
  public void shouldDescribeFailedValidation() {
    validate(false, failWith(exception(IllegalStateException.class), "failure message"));
  }

Would this work for you? Or would you suggest a better approach?

@helsing helsing closed this as completed Jul 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants