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

How to do a custom validation using members from the same class? #99

Closed
agramajoL opened this issue Mar 11, 2023 · 2 comments
Closed

How to do a custom validation using members from the same class? #99

agramajoL opened this issue Mar 11, 2023 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@agramajoL
Copy link

For example:

Class A {
 int a;
 int b;
}

And I want to validate that

a <= b
@marcosolina
Copy link

marcosolina commented Mar 11, 2023

Try this

public class A {
 int a;
 int b;
}

public YourValidator extends AbstractValidator{
  public void rules() {
    ruleFor(c -> c)
            .must(c -> c.a <= c.b)
            .... other stuff
  }
}

@mvallim mvallim self-assigned this Mar 11, 2023
@mvallim mvallim added the question Further information is requested label Mar 11, 2023
@mvallim
Copy link
Owner

mvallim commented Mar 11, 2023

Hi @agramajoL,

Example:

class A

public class A {

  private int a;

  private int b;

  private A(final int a, final int b) {
    this.a = a;
    this.b = b;
  }

  public int getA() {
    return a;
  }

  public void setA(final int a) {
    this.a = a;
  }

  public int getB() {
    return b;
  }

  public void setB(final int b) {
    this.b = b;
  }

}

class AValidador

import static br.com.fluentvalidator.function.FunctionBuilder.of;
import static br.com.fluentvalidator.predicate.ComparablePredicate.lessThanOrEqual;

import br.com.fluentvalidator.AbstractValidator;

import static java.util.function.Function.identity;

public static class AValidador extends AbstractValidator<A> {

  @Override
  public void rules() {

    ruleFor(identity())
      .must(lessThanOrEqual(of(A::getA), of(A::getB)));

  }

}

class SampleTest

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;

import br.com.fluentvalidator.Validator;
import br.com.fluentvalidator.context.ValidationResult;

public class SampleTest {

  private final Validator<A> validator = new AValidador();

  @Test
  public void testSuccessLessThanCompare() {
    final A a = new A(1, 2);

    final ValidationResult validationResult = validator.validate(a);

    assertThat(validationResult.isValid(), equalTo(true));
  }

  @Test
  public void testSuccessLessThanOrEqualCompare() {
    final A a = new A(1, 1);

    final ValidationResult validationResult = validator.validate(a);

    assertThat(validationResult.isValid(), equalTo(true));
  }

  @Test
  public void testFailLessThanCompare() {
    final A a = new A(2, 1);

    final ValidationResult validationResult = validator.validate(a);

    assertThat(validationResult.isValid(), equalTo(false));
  }
}

Thanks for sharing your question.

Best regards,

@mvallim mvallim pinned this issue Mar 11, 2023
@mvallim mvallim closed this as completed Mar 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants