FieldValidateUtils.java

package com.mycim.utils;

import com.fa.sesa.i18n.I18nUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.SystemConstant;

import java.util.regex.Pattern;

public class FieldValidateUtils {
    /**
     * Edc Id 不包含 .,对ID进行严格卡控,可以有字符@、%、$、-、_,且只能拥有一个,不能包含其他字符
     */
    // public static final String COMMON_EDC_ID_REGEX = "^([\\w]+[$%@-]{1})+[\\w]+$|^[A-Za-z0-9]+$";

    /**
     * 验证特殊字符
     * 原函数在action 层,某些模块调用在service。
     * input str 实参为null 或者 “” 会校验不通过
     * @param inputStr
     * @return
     */
    public static String validateSpecialChar(String inputStr){
        if (!Pattern.matches(SystemConstant.Str.SPECIAL_CHAR, inputStr)) {
            String message = I18nUtils.getMessage(MessageIdList.SYSTEM_ALLOW_SPECIAL_CHAR_WITH_KEY,
                                                  "({}) The characters must be letters or digits or hyphens(-) or " +
                                                          "underscores(_) or at(@) or slash(/) or dollar($) or percent(%) or comma(.)",
                                                  inputStr);
            return message;
        }
        return StringUtils.EMPTY;
    }

    /**
     * recipe 创建 id 验证,允许$R
     * @param recipe
     * @return
     */
    public static String validateRecipeId(String recipe){
        if (!Pattern.matches(SystemConstant.Str.RECIPE_REGEX, recipe)) {
            return  I18nUtils.getMessage(MessageIdList.RECIPE_ID_REGEXP_MSG);
        }
        return StringUtils.EMPTY;
    }

    /**
     * 536 save 触发创建recipe,逻辑特殊,不允许变量 $R开头
     * @param recipeId
     * @return
     */
    public static String validateRecipeIdFor536(String recipeId){
        if (!Pattern.matches(SystemConstant.Str.RECIPE_REGEX_FOR_536, recipeId)) {
            return  I18nUtils.getMessage(MessageIdList.RECIPE_ID_REGEXP_MSG_536);
        }
        return StringUtils.EMPTY;
    }

}