com.salesmanager.shop.validation.EnumValidator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sm-shop-model Show documentation
Show all versions of sm-shop-model Show documentation
sm-shop-model contains Shopizer model objects for api
The newest version!
package com.salesmanager.shop.validation;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
/**
* Validates values of a String used as payload in REST service
* Solution taken from https://funofprograming.wordpress.com/2016/09/29/java-enum-validator/
* @author c.samson
*
*/
public class EnumValidator implements ConstraintValidator
{
private Enum annotation;
public void initialize(Enum annotation)
{
this.annotation = annotation;
}
public boolean isValid(String valueForValidation, ConstraintValidatorContext constraintValidatorContext)
{
boolean result = false;
Object[] enumValues = this.annotation.enumClass().getEnumConstants();
if(enumValues != null)
{
for(Object enumValue:enumValues)
{
if(valueForValidation.equals(enumValue.toString())
|| (this.annotation.ignoreCase() && valueForValidation.equalsIgnoreCase(enumValue.toString())))
{
result = true;
break;
}
}
}
return result;
}
}