package foundation.data.rule; import foundation.data.entity.Entity; import foundation.data.meta.field.Field; import foundation.handler.IMessageReporter; import foundation.util.Util; public class FieldValidateRule { public static String ErrorCode_Empty = "Error_Empty"; public static String ErrorCode_Regex = "Error_Regex"; private Field field; private boolean notEmpty; private String validateRegex; public FieldValidateRule(Field field) { this.field = field; this.notEmpty = !field.isNullable(); this.validateRegex = field.getValidateRegex(); } public static FieldValidateRule getInstance(Field field) { FieldValidateRule rule = null; if (!field.isNullable() || field.hasValidateRegex()) { rule = new FieldValidateRule(field); } return rule; } public boolean exec(Entity entity, IMessageReporter reporter) { boolean success = true; int idx = field.getIndexNo(); boolean empty = entity.isEmptyValue(idx); //1. 非空校验 if (notEmpty && empty) { success = false; if (reporter != null) { reporter.reportOneMessage("数据校验", field.getLabelChinese() + "不能为空"); } } if (Util.isEmpty(validateRegex) || empty) { return success; } String value = entity.getString(idx); boolean match = value.matches(validateRegex); if (!match) { success = false; if (reporter != null) { reporter.reportOneMessage("数据校验", field.getLabelChinese() + "格式不对"); } } return success; } }