Celestine Babayaro
Celestine Babayaro

Reputation: 363

How to make string validation via jackson?

I have some class with string fields inside it

    public class Variable {
    private String id;
    private String name;
}

My goal is to have String name <= 256 characters and regexp ^[a-zA-Z0-9_]*$. Can I do it somehow using Jackson annotations?

Upvotes: 0

Views: 1829

Answers (1)

İsmail Y.
İsmail Y.

Reputation: 3945

You can try it like this:

import javax.validation.constraints.Pattern;

public class Variable {

    private String id;

    @Pattern(regexp = "^[a-zA-Z0-9_]{1,256}$")
    private String name;

    public Variable() {
    }

    // getter/setter ..
}

Upvotes: 1

Related Questions