Exemplo n.º 1
0
 public bool CanExecute(IValidateRule rule, ValidateContext context)
 {
     return string.IsNullOrEmpty(rule.RuleSet)
         || context.RuleSetList.IsEmptyOrNull()
             ? true
             : context.RuleSetList.Contains(rule.RuleSet);
 }
Exemplo n.º 2
0
 public bool CanExecute(IValidateRule rule, ValidateContext context)
 {
     return(string.IsNullOrEmpty(rule.RuleSet) ||
            context.RuleSetList.IsEmptyOrNull()
             ? true
             : context.RuleSetList.Contains(rule.RuleSet));
 }
Exemplo n.º 3
0
 public static void AppendDefaultValueOnEmptyValidators(PropertyInfo pi, IValidateRule rule)
 {
     if (rule.Validator == "Empty" || rule.Validator == "NotEmpty")
     {
         // Not/EmptyValidator has a required default constructor required to accurately determine empty for value types
         if (pi.PropertyType.IsValueType && !pi.PropertyType.IsNullableType())
         {
             rule.Validator += "(default('" + pi.PropertyType.Namespace + "." + pi.PropertyType.Name + "'))";
         }
     }
 }
 private async void ValidateNextRule(ValidateContext context, IValidateResult result, int index, IValidateRule rule)
 {
     var nextResult = await rule.ValidateAsync(context);
     if (!nextResult.IsValid)
     {
         foreach (var failure in nextResult.Failures)
         {
             failure.Name = string.Format(string.IsNullOrEmpty(failure.Name) ? "{0}[{1}]{2}" : "{0}[{1}].{2}", ValueName, index, failure.Name);
         }
     }
     result.Merge(nextResult.Failures);
 }
Exemplo n.º 5
0
        public static void AddTypeValidator(List <ITypeValidator> to, IValidateRule attr)
        {
            var appHost = HostContext.AppHost;

            if (!string.IsNullOrEmpty(attr.Condition))
            {
                var evalCode = ScriptCodeUtils.EnsureReturn(attr.Condition);
                var code     = appHost.ScriptContext.CodeSharpPage(evalCode);
                to.Add(new ScriptValidator(code, attr.Condition).Init(attr));
            }
            else if (!string.IsNullOrEmpty(attr.Validator))
            {
                var ret = appHost.EvalExpression(attr
                                                 .Validator); //Validators can't be cached due to custom code/msgs
                if (ret == null)
                {
                    ThrowNoValidator(attr.Validator);
                }

                if (ret is ITypeValidator validator)
                {
                    to.Add(validator.Init(attr));
                }
                else if (ret is List <object> objs)
                {
                    foreach (var o in objs)
                    {
                        if (o is ITypeValidator itemValidator)
                        {
                            to.Add(itemValidator.Init(attr));
                        }
                        else
                        {
                            ThrowInvalidValidator(attr.Validator, nameof(ITypeValidator));
                        }
                    }
                }
                else
                {
                    ThrowInvalidValidator(attr.Validator, nameof(ITypeValidator));
                }
            }
            else
            {
                ThrowInvalidValidateRequest();
            }
        }
Exemplo n.º 6
0
 private static void GenerateEntityValidators()
 {
     foreach (var validateConfig in validateConfigs)
     {
         var entityValidator = new EntityValidator();
         entityValidator.Type = validateConfig.EntityType;
         foreach (var field in validateConfig.Fields)
         {
             foreach (var rule in field.Rules)
             {
                 IValidateRule validateRule = CreateRule(rule, field.Name);
                 entityValidator.ValidateRules.Add(validateRule);
             }
         }
         entityValidators.Add(entityValidator);
     }
 }
Exemplo n.º 7
0
 public static ITypeValidator Init(this ITypeValidator validator, IValidateRule rule)
 {
     if (rule.ErrorCode != null)
     {
         validator.ErrorCode = rule.ErrorCode;
     }
     if (rule.Message != null)
     {
         validator.Message = rule.Message;
     }
     if (rule is ValidateRequestAttribute attr)
     {
         if (attr.StatusCode != default)
         {
             validator.StatusCode = attr.StatusCode;
         }
     }
     return(validator);
 }
Exemplo n.º 8
0
        private static IValidateRule CreateRule(ValidateConfigRule rule, string fieldName)
        {
            IValidateRule validateRule = null;

            try
            {
                var type = Type.GetType(string.Format("SunTzu.Web.EntityValidate." + rule.Type + "Rule"));
                validateRule = Activator.CreateInstance(type) as IValidateRule;
            }
            catch (Exception e)
            {
                logger.Error(e);
            }
            if (validateRule == null)
            {
                logger.DebugFormat("Create ValidateRule failed! ValidateConfigRule={0}, fieldName={1}", rule, fieldName);
                validateRule = new DummyRule();
            }
            validateRule.FieldName    = fieldName;
            validateRule.ErrorMessage = rule.ErrorMessage;
            return(validateRule);
        }
Exemplo n.º 9
0
        public static void AddRule(this List <IPropertyValidator> validators, PropertyInfo pi, IValidateRule propRule)
        {
            var appHost = HostContext.AppHost;

            foreach (var ruleFilter in RuleFilters)
            {
                ruleFilter(pi, propRule);
            }

            IPropertyValidator apply(IPropertyValidator validator)
            {
                var errorCode = propRule.ErrorCode;

                if (!string.IsNullOrEmpty(errorCode))
                {
                    validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
                }
                else
                {
                    if (propRule.Condition != null &&
                        ConditionErrorCodes.TryGetValue(propRule.Condition, out errorCode))
                    {
                        validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
                    }
                }

                if (!string.IsNullOrEmpty(propRule.Message))
                {
                    validator.Options.ErrorMessageSource = new StaticStringSource(propRule.Message.Localize());
                }
                else if (errorCode != null && ErrorCodeMessages.TryGetValue(errorCode, out var errorMsg))
                {
                    validator.Options.ErrorMessageSource = new StaticStringSource(errorMsg.Localize());
                }

                return(validator);
            }

            if (propRule.Validator != null)
            {
                var ret = appHost.EvalExpression(propRule
                                                 .Validator); //Validators can't be cached due to custom code/msgs
                if (ret == null)
                {
                    ThrowNoValidator(propRule.Validator);
                }

                if (ret is IPropertyValidator validator)
                {
                    validators.Add(apply(validator));
                }
                else if (ret is List <object> objs)
                {
                    foreach (var o in objs)
                    {
                        if (o is IPropertyValidator itemValidator)
                        {
                            validators.Add(apply(itemValidator));
                        }
                        else
                        {
                            ThrowInvalidValidator(propRule.Validator, nameof(IPropertyValidator));
                        }
                    }
                }
                else
                {
                    ThrowInvalidValidator(propRule.Validator, nameof(IPropertyValidator));
                }
            }
            else if (!string.IsNullOrEmpty(propRule.Condition))
            {
                var evalCode = ScriptCodeUtils.EnsureReturn(propRule.Condition);
                var page     = appHost.ScriptContext.CodeSharpPage(evalCode);
                validators.Add(apply(new ScriptConditionValidator(page)));
            }
            else
            {
                ThrowInvalidValidate();
            }
        }
Exemplo n.º 10
0
        public static void AddRule(this List <IPropertyValidator> validators, PropertyInfo pi, IValidateRule propRule)
        {
            var appHost = HostContext.AppHost;

            foreach (var ruleFilter in RuleFilters)
            {
                ruleFilter(pi, propRule);
            }

            IPropertyValidator apply(IPropertyValidator validator)
            {
                var errorCode = propRule.ErrorCode;

                if (!string.IsNullOrEmpty(errorCode))
                {
                    validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
                }
                else
                {
                    if (propRule.Condition != null &&
                        ConditionErrorCodes.TryGetValue(propRule.Condition, out errorCode))
                    {
                        validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
                    }
                }

                if (!string.IsNullOrEmpty(propRule.Message))
                {
                    validator.Options.ErrorMessageSource =
                        new StaticStringSource(appHost.ResolveLocalizedString(propRule.Message));
                }
                else if (errorCode != null && ErrorCodeMessages.TryGetValue(errorCode, out var errorMsg))
                {
                    validator.Options.ErrorMessageSource =
                        new StaticStringSource(appHost.ResolveLocalizedString(errorMsg));
                }

                return(validator);
            }

            if (propRule.Validator != null)
            {
                var ret = appHost.EvalExpression(propRule
                                                 .Validator); //Validators can't be cached due to custom code/msgs
                if (ret == null)
                {
                    throw new NotSupportedException(
                              $"Could not resolve matching '{propRule.Validator}` Validator Script Method. " +
                              $"Ensure it's registered in AppHost.ScriptContext and called with correct number of arguments.");
                }

                if (ret is IPropertyValidator validator)
                {
                    validators.Add(apply(validator));
                }
                else if (ret is List <object> objs)
                {
                    foreach (var o in objs)
                    {
                        if (o is IPropertyValidator itemValidator)
                        {
                            validators.Add(apply(itemValidator));
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException($"{propRule.Validator} is not an IPropertyValidator");
                }
            }
            else if (!string.IsNullOrEmpty(propRule.Condition))
            {
                var evalCode = ScriptCodeUtils.EnsureReturn(propRule.Condition);
                var page     = appHost.ScriptContext.CodeSharpPage(evalCode);
                validators.Add(apply(new ScriptConditionValidator(page)));
            }
        }
Exemplo n.º 11
0
        private async void ValidateNextRule(ValidateContext context, IValidateResult result, int index, IValidateRule rule)
        {
            var nextResult = await rule.ValidateAsync(context);

            if (!nextResult.IsValid)
            {
                foreach (var failure in nextResult.Failures)
                {
                    failure.Name = string.Format(string.IsNullOrEmpty(failure.Name) ? "{0}[{1}]{2}" : "{0}[{1}].{2}", ValueName, index, failure.Name);
                }
            }
            result.Merge(nextResult.Failures);
        }