/// <summary> /// 设置一个过滤规则 /// </summary> /// <param name="Input">需要校验的字段名</param> /// <param name="vi">规则</param> /// <returns></returns> public bool SetFilter(string Input, VaildItem vi) { if (vi == null) { return(true); } if (string.IsNullOrEmpty(vi.Name) || !f.HasFormat(vi.Name)) { return(false); } if (Formater.ContainsKey(Input)) { Formater[Input].Add(vi); } else { Formater.Add(Input, new List <VaildItem>() { vi }); } return(true); }
/// <summary> /// 验证 /// </summary> public void DataValid() { //设置默认校验器 List <VaildItem> BaseVailate = null; if (Validate.ContainsKey("*")) { BaseVailate = Validate["*"]; } //设置默认过滤器 List <VaildItem> BaseFilter = null; if (Formater.ContainsKey("*")) { BaseFilter = Formater["*"]; } Output = new Dictionary <string, Result <object> >(); foreach (var i in Input)//循环输入表 { #region base if (BaseFilter == null && BaseVailate == null && !Validate.ContainsKey(i.Key) && !Formater.ContainsKey(i.Key) )//如果未设置默认过滤器和验证器 且不再过滤器与验证器名单上的输入将会被抛弃 { continue; } #endregion Result <object> res = Result <object> .GetResult(true, i.Value, string.Empty);//预设为正常 #region vailate //先处理验证,后处理过滤 if (Validate.ContainsKey(i.Key))//检查是否有验证器设置 { foreach (VaildItem vi in Validate[i.Key]) { if (!v.Validates[vi.Name](i.Value, vi.Param))//如果验证失败直接返回 { res.Bool = false; res.Data = i.Value; res.ErrorMessage = "Validate:" + vi.Name; break; } } } else if (BaseVailate != null)//如果有默认校验器的 { foreach (VaildItem vi in BaseVailate) { if (!v.Validates[vi.Name](i.Value, vi.Param)) { res.Bool = false; res.Data = i.Value; res.ErrorMessage = "Validate:" + vi.Name; break; } } } //校验完毕 if (!res.Bool)//如果验证失败 不过滤直接推出 { Output.Add(i.Key, res); continue; } #endregion #region format //开始转换 if (Formater.ContainsKey(i.Key))//检查是否有过滤器设置 { object tempdate = res.Data; foreach (VaildItem fi in Formater[i.Key]) { Result <object> ir = f.Formats[fi.Name](tempdate, fi.Param); if (!ir.Bool)//如果失败中断并返回 { res.Bool = false; res.ErrorMessage = "Formats:" + fi.Name; res.Data = ir.Data; break; } tempdate = ir.Data; //如果成功,则赋值到寄存器,继续过滤 } res.Data = tempdate; //完成之后赋值到返回上 } else if (BaseFilter != null) { foreach (VaildItem vi in BaseVailate) { object tempdate = i.Value; foreach (VaildItem fi in BaseFilter) { Result <object> ir = f.Formats[fi.Name](tempdate, fi.Param); if (!ir.Bool)//如果失败中断并返回 { res.Bool = false; res.ErrorMessage = "Formats:" + fi.Name; res.Data = ir.Data; break; } tempdate = ir.Data; //如果成功,则赋值到寄存器,继续过滤 } res.Data = tempdate; //完成之后赋值到返回上 } } #endregion //过滤完毕 Output.Add(i.Key, res);//赋值到结果字典 }//end foreach }//end func