Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.validation to validate list of values?

is there a way to use javax.validation to validate a variable of type string called colour that needs to have these values only(red, blue, green, pink) using annotations?

i have seen @size(min=1, max=25) and @notnull but is there something like this @In(red, blue, green, pink)

more or less similar to the In-keyword used in mysql

like image 863
Jonathan Avatar asked Feb 07 '11 14:02

Jonathan


People also ask

How do you validate a list of objects in Java?

list = new ArrayList<E>(); } public ValidList(List<E> list) { this. list = list; } // Bean-like methods, used by javax. validation but ignored by JSON parsing public List<E> getList() { return list; } public void setList(List<E> list) { this.

How do you validate a value in Java?

While it's not widely known, Java Bean Validation ships with the validateProperty() and validateValue() methods, which can be used for validating the individual fields of a constrained class as well as values even before assigning them.

How do you validate a list of strings?

You don't have to use any wrapper class just to validate a list of strings. Just use @EachPattern constraint from validator-collection: @NotNull @EachPattern(regexp="\b[A-Z0-9. _%+-]+@[A-Z0-9.


4 Answers

In that case I think it would be simpler to use the @Pattern annotation, like the snippet below. If you want a case insensitive evaluation, just add the appropriate flag:

@Pattern(regexp = "red|blue|green|pink", flags = Pattern.Flag.CASE_INSENSITIVE)

like image 148
R. Duval Avatar answered Oct 04 '22 15:10

R. Duval


You can create a custom validation annotation. I will write it here (untested code!):

@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = InConstraintValidator.class)
public @interface In
{
    String message() default "YOURPACKAGE.In.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default {};

    Object[] values(); // TODO not sure if this is possible, might be restricted to String[]
}

public class InConstraintValidator implements ConstraintValidator<In, String>
{

    private Object[] values;

    public final void initialize(final In annotation)
    {
        values = annotation.values();
    }

    public final boolean isValid(final String value, final ConstraintValidatorContext context)
    {
        if (value == null)
        {
            return true;
        }
        return ...; // check if value is in this.values
    }

}
like image 31
Wouter Lievens Avatar answered Oct 04 '22 17:10

Wouter Lievens


you can create an enum

public enum Colors {
    RED, PINK, YELLOW
}

and then in your model, you can validate it like so:

public class Model {
    @Enumerated(EnumType.STRING)
    private Colors color;
}

which will validate your payload against the enum, given that you have added @Valid in your RestController.

like image 24
Asheesh Avatar answered Oct 04 '22 17:10

Asheesh


You can create your validation class by your own. for reference https://www.javatpoint.com/spring-mvc-custom-validation

like image 35
prakash kandhasamy Avatar answered Oct 04 '22 17:10

prakash kandhasamy