Slim3 の Validator を自作してみた

リクエストパラメータが Key や boolean に変換可能かどうか検証する Validator が見当たらなかったので。Slim3 のソースコードを参考にすれば、作るのは難しくないです。


まず boolean 用。

package sample.controller.validator;

import java.util.Map;

import org.slim3.controller.validator.AbstractValidator;
import org.slim3.util.ApplicationMessage;

/**
 * A validator for a boolean value.
 */
public class BooleanTypeValidator extends AbstractValidator {

    /**
     * The instance.
     */
    public static BooleanTypeValidator INSTANCE = new BooleanTypeValidator();

    /**
     * Constructor.
     */
    public BooleanTypeValidator() {
        super();
    }

    /**
     * Constructor.
     * 
     * @param message
     *            the error messsage
     */
    public BooleanTypeValidator(String message) {
        super(message);
    }

    public String validate(Map<String, Object> parameters, String name) {
        Object value = parameters.get(name);
        if (value == null || "".equals(value)) {
            return null;
        }

        try {
            String s = (String) value;
            Boolean.valueOf(s);
            return null;
        } catch (Throwable ignore) {
            if (message != null) {
                return message;
            }
            return ApplicationMessage.get(getMessageKey(), getLabel(name));
        }
    }

    @Override
    protected String getMessageKey() {
        return "validator.booleanType";
    }

}

次に Key 用。

package sample.controller.validator;

import java.util.Map;

import org.slim3.controller.validator.AbstractValidator;
import org.slim3.util.ApplicationMessage;

import com.google.appengine.api.datastore.KeyFactory;

/**
 * A validator for a key value.
 */
public class KeyTypeValidator extends AbstractValidator {

    /**
     * The instance.
     */
    public static KeyTypeValidator INSTANCE = new KeyTypeValidator();

    /**
     * Constructor.
     */
    public KeyTypeValidator() {
        super();
    }

    /**
     * Constructor.
     * 
     * @param message
     *            the error message
     */
    public KeyTypeValidator(String message) {
        super(message);
    }

    public String validate(Map<String, Object> parameters, String name) {
        Object value = parameters.get(name);
        if (value == null || "".equals(value)) {
            return null;
        }

        try {
            String s = (String) value;
            KeyFactory.stringToKey(s);
            return null;
        } catch (Throwable ignore) {
            if (message != null) {
                return message;
            }
            return ApplicationMessage.get(getMessageKey(), getLabel(name));
        }
    }

    @Override
    protected String getMessageKey() {
        return "validator.keyType";
    }

}


そうそう、検証に失敗したとき表示するメッセージを、application_en.properties と application_ja.properties に追加するのを忘れずに。

validator.booleanType={0} must be a boolean.
validator.keyType={0} must be a key.


Validator の自作は難しくないけど、できれば Entity のプロパティに使える型は一通り用意してあると嬉しいですね。特に Key や boolean なんてしょっちゅう使いそうですし。