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(); } }
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))); } }