/// <summary> /// 验证属性的值是否通过 /// </summary> /// <param name="value">属性的值</param> /// <param name="validContext">验证上下文</param> /// <returns></returns> bool IValidRule.IsValid(object value, ValidContext validContext) { var stringValue = value == null ? null : value.ToString(); if (string.IsNullOrWhiteSpace(stringValue) == true) { return true; } return this.IsValid(stringValue, validContext); }
/// <summary> /// 获取验证不通过的第一个规则 /// 都通过则返回null /// </summary> /// <param name="context">上下文</param> /// <returns></returns> public IValidRule GetFailureRule(ValidContext context) { if (this.ValidRules.Length == 0) { return(null); } var value = this.GetValue(context.Instance); return(this.ValidRules.FirstOrDefault(r => r.IsValid(value, context) == false)); }
/// <summary> /// 验证属性的值是否通过 /// </summary> /// <param name="value">属性的值</param> /// <param name="validContext">验证上下文</param> /// <returns></returns> bool IValidRule.IsValid(object value, ValidContext validContext) { var stringValue = value?.ToString(); if (string.IsNullOrWhiteSpace(stringValue) == true) { return(true); } return(this.IsValid(stringValue, validContext)); }
/// <summary> /// 验证模型 /// 包括Attribute规则和Fluent规则 /// </summary> /// <typeparam name="T">模型类型</typeparam> /// <param name="model">模型实例</param> /// <returns></returns> public static ValidResult ValidFor <T>(T model) { if (model == null) { return(ValidResult.False("模型不能为null ..")); } var context = new ValidContext(model, RuleProperty.GetGetProperties(typeof(T))); foreach (var property in context.Properties) { var failureRule = property.GetFailureRule(context); if (failureRule != null) { var message = failureRule.FormatErrorMessage(null); return(ValidResult.False(message, property.Info)); } } return(ValidResult.True()); }
/// <summary> /// 验证模型 /// </summary> /// <typeparam name="T">模型类型</typeparam> /// <param name="model">模型实例</param> /// <returns></returns> public static ValidResult ValidFor <T>(T model) { if (model == null) { return(new ValidResult { State = false, ErrorMessage = "模型不能为null .." }); } var validContext = new ValidContext { Instance = model, Properties = Property.From(typeof(T)) }; foreach (var property in validContext.Properties) { var value = property.GetValue(model); var failureRule = property.ValidRules.FirstOrDefault(r => r.IsValid(value, validContext) == false); if (failureRule == null) { continue; } return(new ValidResult { ProperyName = property.Name, ErrorMessage = failureRule.FormatErrorMessage(null) }); } return(new ValidResult { State = true }); }
/// <summary> /// 获取验证不通过的第一个规则 /// 都通过则返回null /// </summary> /// <param name="context">上下文</param> /// <returns></returns> public IValidRule GetFailureRule(ValidContext context) { if (this.ValidRules.Length == 0) { return null; } var value = this.GetValue(context.Instance); return this.ValidRules.FirstOrDefault(r => r.IsValid(value, context) == false); }
/// <summary> /// 验证属性的值是否通过 /// </summary> /// <param name="value">属性的值</param> /// <param name="validContext">验证上下文</param> /// <returns></returns> protected abstract bool IsValid(string value, ValidContext validContext);