0

So I have a simple class like this:

import javax.validation.constraints.Size;

public class Temail {

    @Size(min = 0, max = 10)
    private String json;

    public Temail(String json) {
        Validate.notEmpty(json, "json can't be empty");
        setJson(json);
    }

    public void setJson(String json) {
        this.json = json;
    }

    public String getJson() {
        return json;
    }

}

What I am expecting is if I run new Temail("1234123123123"); I expect it to throw an exception but it does not. I looked at the conventions and it does fit into the right convention. So what's the problem here?

Sarp Kaya
  • 3,686
  • 21
  • 64
  • 103

1 Answers1

0

It seems that it is not supposed to throw an exception when a constraint is invalid. Instead, you use a validator instance to validate the fields based on the constraints you define. So, something like this:

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

Temail t = new Temail("123456789012345");

Set<ConstraintViolation<Temail>> constraintViolations = validator.validateProperty(t, "json");

See more examples and explanations here.

Allen
  • 478
  • 9
  • 21
dramzy
  • 1,379
  • 1
  • 11
  • 25