package io.hosuaby.restful.domain.validators;

import io.hosuaby.restful.domain.Teapot;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Teapot validator.
 *
 * @author Alexei KLENIN
 */
@Component
public class TeapotValidator implements Validator {

    /**
     * Supports only {@link Teapot} objects;
     */
    @Override
    public boolean supports(Class<?> clazz) {
        return Teapot.class.equals(clazz);
    }

    /**
     * Validates or rejects the {@link Teapot} object.
     */
    @Override
    public void validate(Object teapot, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id", "errIdUdefined", "Teapot id is undefined");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "errNameUndefined", "Teapot name is undefined");
    }

}

@@ -93,48 +93,151 @@
 ort 
-org.springframework.stereotype.Component
+java.util.Arrays;%0Aimport java.util.regex.Pattern;%0A%0Aimport org.springframework.stereotype.Component;%0Aimport org.springframework.util.StringUtils
 ;%0Aim
@@ -506,16 +506,1035 @@
 ator %7B%0A%0A
+    /**%0A     * Pattern for teapot id. Id must start with letter and can contain letter,%0A     * digits and %22_%22 character.%0A     */%0A    private static final Pattern ID_PATTERN = Pattern.compile(%22%5Ba-zA-Z%5D%5C%5Cw*%22);%0A%0A    /**%0A     * Valid teapot volumes.%0A     */%0A    private static final Float%5B%5D VALID_VOLUMES = new Float%5B%5D %7B%0A        Teapot.L0_3, Teapot.L0_5, Teapot.L1, Teapot.L1_5, Teapot.L3, Teapot.L5,%0A        Teapot.L8, Teapot.L10%0A    %7D;%0A%0A    /**%0A     * Error messages.%0A     */%0A    private static final String ERR_ID_UNDEFINED = %22Teapot id is undefined%22;%0A    private static final String ERR_NAME_UNDEFINED = %22Teapot name is undefined%22;%0A    private static final String ERR_ID_BAD_FORMAT = %22Teapot id has bad format. %22%0A            + %22Id must start with letter and can contain letter, digits and %22%0A            + %22%5C%22_%5C%22 character.%22;%0A    private static final String ERR_BAD_VOLUME = %22Teapot has a bad volume. %22%0A            + %22List of authorized volumes: %22%0A            + StringUtils.arrayToDelimitedString(VALID_VOLUMES, %22, %22);%0A%0A
     /**%0A
@@ -1813,22 +1813,19 @@
 (Object 
-teapot
+obj
 , Errors
@@ -1834,16 +1834,50 @@
 rrors) %7B
+%0A%0A        /* Id must be defined */
 %0A       
@@ -1911,32 +1911,49 @@
 ptyOrWhitespace(
+%0A                
 errors, %22id%22, %22e
@@ -1971,34 +1971,62 @@
 d%22, 
-%22Teapot id is un
+ERR_ID_UNDEFINED);%0A%0A        /* Name must be 
 defined
-%22);
+ */
 %0A   
@@ -2072,16 +2072,33 @@
 tespace(
+%0A                
 errors, 
@@ -2129,36 +2129,474 @@
 d%22, 
-%22Teapot name is undefined%22);
+ERR_NAME_UNDEFINED);%0A%0A        Teapot teapot = (Teapot) obj;%0A%0A        /* Check id against pattern */%0A        if (!ID_PATTERN.matcher(teapot.getId()).matches()) %7B%0A            errors.rejectValue(%22id%22, %22errIdBadFormat%22, ERR_ID_BAD_FORMAT);%0A        %7D%0A%0A        /* Check if volume is in the list of valid volumes */%0A        if (!Arrays.asList(VALID_VOLUMES).contains(teapot.getVolume())) %7B%0A            errors.rejectValue(%22volume%22, %22errBadVolume%22, ERR_BAD_VOLUME);%0A        %7D
 %0A   

Implement advanced validations of teapots.