예제 #1
0
 /// <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);
 }
예제 #2
0
        /// <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));
        }
예제 #3
0
        /// <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));
        }
예제 #4
0
        /// <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());
        }
예제 #5
0
        /// <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
            });
        }
예제 #6
0
 /// <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);
 }
예제 #7
0
 /// <summary>
 /// 验证属性的值是否通过
 /// </summary>
 /// <param name="value">属性的值</param>
 /// <param name="validContext">验证上下文</param>
 /// <returns></returns>
 protected abstract bool IsValid(string value, ValidContext validContext);
예제 #8
0
 /// <summary>
 /// 验证属性的值是否通过
 /// </summary>
 /// <param name="value">属性的值</param>
 /// <param name="validContext">验证上下文</param>
 /// <returns></returns>
 protected abstract bool IsValid(string value, ValidContext validContext);