private IValidatorSelector GetSelector() { IValidatorSelector selector = null; if (_properties != null || _ruleSets != null || _customSelector != null) { var selectors = new List <IValidatorSelector>(3); if (_customSelector != null) { selectors.Add(_customSelector); } if (_properties != null) { selectors.Add(ValidatorOptions.Global.ValidatorSelectors.MemberNameValidatorSelectorFactory(_properties.ToArray())); } if (_ruleSets != null) { selectors.Add(ValidatorOptions.Global.ValidatorSelectors.RulesetValidatorSelectorFactory(_ruleSets.ToArray())); } selector = selectors.Count == 1 ? selectors[0] : new CompositeValidatorSelector(selectors); } else { selector = ValidatorOptions.Global.ValidatorSelectors.DefaultValidatorSelectorFactory(); } return(selector); }
/// <summary> /// Constructor /// </summary> /// <param name="objectToValidate">Object to validate</param> /// <param name="validatorSelector">The validator selector</param> public ValidationContext( object objectToValidate, IValidatorSelector validatorSelector) { InstanceToValidate = objectToValidate; ValidatorSelector = validatorSelector; }
protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext propertyValidatorContext, IValidatorSelector validatorSelector) { var propertyChain = new PropertyChain(propertyValidatorContext.PropertyChain); propertyChain.Add(propertyValidatorContext.Member); return new ValidationContext(instanceToValidate, propertyChain, validatorSelector); }
internal ValidationContext(T instanceToValidate, PropertyChain propertyChain, IValidatorSelector validatorSelector, List <ValidationFailure> failures, MessageFormatter messageFormatter) { PropertyChain = new PropertyChain(propertyChain); InstanceToValidate = instanceToValidate; Selector = validatorSelector; Failures = failures; MessageFormatter = messageFormatter; }
public ScimValidationContext( T instanceToValidate, T existingRecordInstance, PropertyChain propertyChain, IValidatorSelector validatorSelector) : base(instanceToValidate, propertyChain, validatorSelector) { ExistingResourceRecord = existingRecordInstance; }
/// <summary> /// Indicates that the specified selector should be used to control which rules are executed. /// </summary> /// <param name="selector">The custom selector to use</param> /// <returns></returns> public ValidationStrategy <T> UseCustomSelector(IValidatorSelector selector) { if (selector == null) { throw new ArgumentNullException(nameof(selector)); } _customSelector = selector; return(this); }
private List <ValidationError> Validate( T objectToValidate, IValidatorSelector validatorSelector) { objectToValidate.NotNull(nameof(objectToValidate)); var context = new ValidationContext <T>(objectToValidate, validatorSelector); return(_nestedValidators.SelectMany(x => x.Validate(context)).ToList()); }
/// <summary> /// Creates an instance of a ValidationContext for an object model. /// </summary> /// <param name="model"></param> /// <param name="validatorSelector"></param> /// <returns></returns> private IValidationContext CreateValidationContext(object model, IValidatorSelector validatorSelector = null) { // This method is required due to breaking changes in FluentValidation 9! // https://docs.fluentvalidation.net/en/latest/upgrading-to-9.html#removal-of-non-generic-validate-overload if (validatorSelector == null) { // No selector specified - use the default. validatorSelector = ValidatorOptions.Global.ValidatorSelectors.DefaultValidatorSelectorFactory(); } // Don't need to use reflection to construct the context. // If you create it as a ValidationContext<object> instead of a ValidationContext<T> then FluentValidation will perform the conversion internally, assuming the types are compatible. var context = new ValidationContext <object>(model, new PropertyChain(), validatorSelector); // InjectValidator looks for a service provider inside the ValidationContext with this key. context.RootContextData["_FV_ServiceProvider"] = ServiceProvider; return(context); }
/// <summary> /// 根据规则名来验证 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <param name="rule"></param> /// <param name="message"></param> /// <returns></returns> public bool Valid <T>(T value, string rule, out string message) { message = string.Empty; if (string.IsNullOrEmpty(rule)) { return(Valid(value, out message)); } Type type = value.GetType(); string typeName = type.ToString(); if (!_validatorSet.ContainsKey(type)) { message = $"未找到{value}相对应的验证类"; return(false); } IEnumerable <string> ruleSetNames = from x in rule.Split(',', ';') select x.Trim(); IValidatorSelector selector = ValidatorOptions.Global.ValidatorSelectors.RulesetValidatorSelectorFactory(ruleSetNames.ToArray()); var context = new ValidationContext <T>(value, new PropertyChain(), selector); //验证途中如果没有 则新加入验证方法 if (!_validaRuleBehaviorSet.ContainsKey(typeName)) { _validaRuleBehaviorSet[typeName] = _validatorSet[type].Validate; } ValidationResult result = _validaRuleBehaviorSet[typeName](context); if (result.IsValid) { return(true); } message = result.Errors?[0].ErrorMessage; return(false); }
/// <summary> /// Creates an instance of a ValidationContext for an object model. /// </summary> /// <param name="model"></param> /// <param name="validatorSelector"></param> /// <returns></returns> private IValidationContext CreateValidationContext(object model, IValidatorSelector validatorSelector = null) { // This method is required due to breaking changes in FluentValidation 9! // https://docs.fluentvalidation.net/en/latest/upgrading-to-9.html#removal-of-non-generic-validate-overload var validationContextGeneric = typeof(ValidationContext <>); var validationContextType = validationContextGeneric.MakeGenericType(model.GetType()); if (validatorSelector == null) { return((IValidationContext)Activator.CreateInstance(validationContextType, model)); } else { return((IValidationContext)Activator.CreateInstance(validationContextType, model, new PropertyChain(), validatorSelector )); } }
/// <summary> /// Creates a new validation context with a custom property chain and selector /// </summary> /// <param name="instanceToValidate"></param> /// <param name="propertyChain"></param> /// <param name="validatorSelector"></param> public ValidationContext(T instanceToValidate, PropertyChain propertyChain, IValidatorSelector validatorSelector) : this(instanceToValidate, propertyChain, validatorSelector, new List <ValidationFailure>(), ValidatorOptions.Global.MessageFormatterFactory()) { }
/// <summary> /// Creates a new validation context with a custom property chain and selector /// </summary> /// <param name="instanceToValidate"></param> /// <param name="propertyChain"></param> /// <param name="validatorSelector"></param> public ValidationContext(T instanceToValidate, PropertyChain propertyChain, IValidatorSelector validatorSelector) { PropertyChain = new PropertyChain(propertyChain); InstanceToValidate = instanceToValidate; Selector = validatorSelector; }
public static IValidatorSelector ValidatorAction(this IValidatorSelector ruleBuilder, Action action) { return(ruleBuilder); }
public static ValidationResult Validate <T>(this IValidator <T> validator, T instance, IValidatorSelector selector = null, string ruleSet = null) { if (selector != null && ruleSet != null) { throw new InvalidOperationException("Cannot specify both an IValidatorSelector and a RuleSet."); } if (selector == null) { selector = new DefaultValidatorSelector(); } if (ruleSet != null) { var ruleSetNames = ruleSet.Split(',', ';'); selector = new RulesetValidatorSelector(ruleSetNames); } var context = new ValidationContext <T>(instance, new PropertyChain(), selector); return(validator.Validate(context)); }
private static ValidationResult ValidateTyped <T>(IValidator <T> validator, T request, string[] ruleset, IValidatorSelector selector = null) { return(validator.Validate(request, selector: selector)); }
public static Task <ValidationResult> ValidateAsync <T>(this IValidator <T> validator, T instance, CancellationToken cancellationToken = default, IValidatorSelector selector = null, string ruleSet = null) { return(validator.ValidateAsync(instance, options => { if (selector != null) { options.UseCustomSelector(selector); } if (ruleSet != null) { options.IncludeRuleSets(RulesetValidatorSelector.LegacyRulesetSplit(ruleSet)); } }, cancellationToken)); }
public static IValidatorSelector NotNullEx(this IValidatorSelector ruleBuilder) { ruleBuilder.AddValidator(new NotNullValidator(ruleBuilder.PropertyName)); return(ruleBuilder); }
public static Task <ValidationResult> ValidateAsync <T>(this IValidator <T> validator, T instance, CancellationToken cancellationToken = default, IValidatorSelector selector = null, string ruleSet = null) { return(validator.ValidateAsync(instance, options => { if (selector != null) { options.UseCustomSelector(selector); } if (ruleSet != null) { var ruleSetNames = ruleSet.Split(',', ';') .Select(x => x.Trim()) .ToArray(); options.IncludeRuleSets(ruleSetNames); } }, cancellationToken)); }
public static ValidationResult Validate <T>(this IValidator <T> validator, T instance, IValidatorSelector selector = null, string ruleSet = null) { return(validator.Validate(instance, options => { if (selector != null) { options.UseCustomSelector(selector); } if (ruleSet != null) { var ruleSetNames = ruleSet.Split(',', ';') .Select(x => x.Trim()) .ToArray(); options.IncludeRuleSets(ruleSetNames); } })); }
private ScimValidationContext(T instanceToValidate, PropertyChain propertyChain, IValidatorSelector validatorSelector) : base(instanceToValidate, propertyChain, validatorSelector) { }
public static ValidationResult Validate <T>(this IValidator <T> validator, T instance, IValidatorSelector selector) { var context = new ValidationContext <T>(instance, new PropertyChain(), selector); return(validator.Validate(context)); }
private static ValidationResult ValidateTyped <T>(IValidator <T> validator, T request, string[] ruleset, IValidatorSelector selector = null) { return(validator.Validate(request, selector, string.Join(",", ruleset).TrimEnd(','))); }
public ValidationContext(T instanceToValidate, PropertyChain propertyChain, IValidatorSelector validatorSelector) : base(instanceToValidate, propertyChain, validatorSelector) { InstanceToValidate = instanceToValidate; }
/// <summary> /// 验证对象数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="validator"></param> /// <param name="instance"></param> /// <param name="selector"></param> /// <param name="ruleSet"></param> /// <returns></returns> public static Task <ValidationResult> ValidateAsync(this IValidator validator, IDatabase database, object instance, IValidatorSelector selector = null, string ruleSet = null) { if (selector != null && ruleSet != null) { throw new InvalidOperationException("Cannot specify both an IValidatorSelector and a RuleSet."); } if (selector == null) { selector = ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory(); } if (ruleSet != null) { var ruleSetNames = ruleSet.Split(',', ';'); selector = ValidatorOptions.ValidatorSelectors.RulesetValidatorSelectorFactory(ruleSetNames); } var context = new ValidationContext(database, instance, new PropertyChain(), selector); return(validator.ValidateAsync(context)); }
public static ValidationResult Validate <T>(this IValidator <T> validator, T instance, IValidatorSelector selector = null, string ruleSet = null) { return(validator.Validate(instance, options => { if (selector != null) { options.UseCustomSelector(selector); } if (ruleSet != null) { options.IncludeRuleSets(RulesetValidatorSelector.LegacyRulesetSplit(ruleSet)); } })); }
public static ValidationResult Validate <T>(this IValidator <T> validator, T instance, IValidatorSelector selector = null, string ruleSet = null, string[] properties = null) { if (selector != null && ruleSet != null) { throw new InvalidOperationException("Cannot specify both an IValidatorSelector and a RuleSet."); } if (selector != null && properties != null) { throw new InvalidOperationException("Cannot specify both an IValidatorSelector and properties to include."); } if (properties != null && ruleSet != null) { throw new InvalidOperationException("Cannot specify both properties to include and a RuleSet."); } if (selector == null) { selector = new DefaultValidatorSelector(); } if (properties != null) { selector = new MemberNameValidatorSelector(properties); } if (ruleSet != null) { selector = new RulesetValidatorSelector(ruleSet); } var context = new ValidationContext <T>(instance, new PropertyChain(), selector); return(validator.Validate(context)); }