Question

I am implementing server side validation for my REST service. In below class I have imposed javax size validation. The problem is that the size validation fires even when address1 is null or zero length string. I want the size validation to occur only when address1 contains some text, if it is empty then I don't want size validation to come into effect.

    import javax.validation.constraints.Size;
    import javax.xml.bind.annotation.XmlElement;

    public class Address {

        private String address1;

        @XmlElement(name="address1")
        @Size(min=1, max=50, message="address1")
        public String getAddress1() {
            return address1;
        }

        public void setAddress1(String address1) {
            this.address1 = address1;
        }

    }

I am thinking of using @Pattern in place of @size. What is the regex for it?

Was it helpful?

Solution

The regex .{1,50} should work, though I'd suggest sticking with @Size unless you plan on making the validation more complex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top