/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return new ModelValidationRule( "Custom", base.FormatMessage(rule, validator), base.GetMemberNames(rule)); }
public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName) { ParentContext = parentContext; Rule = rule; PropertyName = propertyName; propertyValueContainer = new Lazy<object>(() => rule.PropertyFunc(parentContext.InstanceToValidate)); }
/// <summary> /// Determines whether or not a rule should execute. /// </summary> /// <param name="rule">The rule</param> /// <param name="propertyPath">Property path (eg Customer.Address.Line1)</param> /// <param name="context">Contextual information</param> /// <returns>Whether or not the validator can execute.</returns> public bool CanExecute(PropertyRule rule, string propertyPath, ValidationContext context) { if (string.IsNullOrEmpty(rule.RuleSet) && rulesetsToExecute.Length == 0) return true; if (!string.IsNullOrEmpty(rule.RuleSet) && rulesetsToExecute.Length > 0 && rulesetsToExecute.Contains(rule.RuleSet)) return true; return false; }
public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName, object propertyValue) { ParentContext = parentContext; Rule = rule; PropertyName = propertyName; propertyValueContainer = new Lazy<object>(() => propertyValue); }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return new RegexValidationRule( FormatMessage(rule, validator), GetMemberNames(rule), ((IRegularExpressionValidator)validator).Expression); }
/// <summary> /// Determines whether or not a rule should execute. /// </summary> /// <param name="rule">The rule</param> /// <param name="propertyPath">Property path (eg Customer.Address.Line1)</param> /// <param name="context">Contextual information</param> /// <returns>Whether or not the validator can execute.</returns> public bool CanExecute(PropertyRule rule, string propertyPath, ValidationContext context) { // By default we ignore any rules part of a RuleSet. if (!string.IsNullOrEmpty(rule.RuleSet)) return false; return true; }
public RequiredFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { bool isNonNullableValueType = !TypeAllowsNullValue(metadata.ModelType); bool nullWasSpecified = metadata.Model == null; ShouldValidate = isNonNullableValueType && nullWasSpecified; }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return new ComparisonValidationRule( base.FormatMessage(rule, validator), base.GetMemberNames(rule), ComparisonOperator.LessThan, ((LessThanValidator)validator).ValueToCompare); }
public static PropertyRule Rule(this Func<PropertyInfo, bool> pi, out IDisposable unreg) { pi.AssertNotNull(); var rule = new PropertyRule(pi); Repository.Rules.Add(rule); unreg = new DisposableAction(() => Repository.Rules.Remove(rule)); return rule; }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return new StringLengthValidationRule( base.FormatMessage(rule, validator), base.GetMemberNames(rule), ((ILengthValidator)validator).Min, ((ILengthValidator)validator).Max); }
public SimpleUnitaryClientSideValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator, string validationType) : base(metadata, controllerContext, rule, validator) { _validationType = validationType; }
private static IEnumerable<ValidationFailure> Validate(object value) { var parentContext = new ValidationContext(null); var rule = new PropertyRule(null, x => value, null, null, typeof(string), null) { PropertyName = "Name" }; var context = new PropertyValidatorContext(parentContext, rule, null); var validator = new OnlyCharactersValidator(); var result = validator.Validate(context); return result; }
/// <summary> /// Constructor /// </summary> /// <param name="parentContext">The parent context</param> /// <param name="rule">Property rule to apply</param> /// <param name="propertyName">Property name</param> public PropertyValidatorContext( ValidationContext parentContext, PropertyRule rule, string propertyName) { ParentContext = parentContext; Rule = rule; PropertyName = propertyName; }
/// <summary> /// Get the formatted error message of the validator. /// </summary> /// <returns>A formatted error message string.</returns> protected virtual Func<string, string> FormatMessage(PropertyRule rule, IPropertyValidator validator) { return displayName => { return new MessageFormatter() .AppendPropertyName(displayName ?? rule.GetDisplayName()) .BuildMessage(validator.ErrorMessageSource.GetString()); }; }
public void Should_validate_property_value_without_instance_different_types() { var validator = new EqualValidator(100M); // decimal var parentContext = new ValidationContext(null); var rule = new PropertyRule(null, x => 100D /* double */, null, null, typeof(string), null) { PropertyName = "Surname" }; var context = new PropertyValidatorContext(parentContext, rule, null); var result = validator.Validate(context); // would fail saying that decimal is not double result.Count().ShouldEqual(0); }
public void Should_validate_property_value_without_instance() { var validator = new NotNullValidator(); var parentContext = new ValidationContext(null); var rule = new PropertyRule(null, x => null, null, null, typeof(string), null) { PropertyName = "Surname" }; var context = new PropertyValidatorContext(parentContext, rule, null); var result = validator.Validate(context); result.Single().ShouldNotBeNull(); }
/// <summary> /// Creates a <see cref="IFluentAdapter"/> instance based on the provided <paramref name="rule"/> and <paramref name="propertyValidator"/>. /// </summary> /// <param name="rule">The <see cref="PropertyRule"/> for which the adapter should be created.</param> /// <param name="propertyValidator">The <see cref="IPropertyValidator"/> for which the adapter should be created.</param> /// <returns>An <see cref="IFluentAdapter"/> instance.</returns> public IFluentAdapter Create(PropertyRule rule, IPropertyValidator propertyValidator) { Func<PropertyRule, IPropertyValidator, IFluentAdapter> factory; if (!factories.TryGetValue(propertyValidator.GetType(), out factory)) { factory = (a, d) => new FluentAdapter("Custom", rule, propertyValidator); } return factory(rule, propertyValidator); }
public FluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext) { this.Validator = validator; // Build a new rule instead of the one passed in. // We do this as the rule passed in will not have the correct properties defined for standalone validation. // We also want to ensure we copy across the CustomPropertyName and RuleSet, if specified. Rule = new PropertyRule(null, x => metadata.Model, null, null, metadata.ModelType, null) { PropertyName = metadata.PropertyName, DisplayName = rule == null ? null : rule.DisplayName, RuleSet = rule == null ? null : rule.RuleSet }; }
private ModelValidator GetModelValidator(ModelMetadata meta, IEnumerable<ModelValidatorProvider> validatorProviders, PropertyRule rule, IPropertyValidator propertyValidator) { //var type = propertyValidator.GetType(); FluentValidationHttpModelValidationFactory factory = //validatorFactories //.Where(x => x.Key.IsAssignableFrom(type)) //.Select(x => x.Value) //.FirstOrDefault() //?? ((metadata, controllerContext, description, validator) => new FluentValidationHttpPropertyValidator(metadata, controllerContext, description, validator)); return factory(meta, validatorProviders, rule, propertyValidator); }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return new ComparisonValidationRule( base.FormatMessage(rule, validator), base.GetMemberNames(rule), ComparisonOperator.GreaterThan, ((ExclusiveBetweenValidator)validator).From); yield return new ComparisonValidationRule( base.FormatMessage(rule, validator), base.GetMemberNames(rule), ComparisonOperator.LessThan, ((ExclusiveBetweenValidator)validator).To); }
public ValidatorPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { ShouldValidate = false; var model = controllerContext.Controller.ViewData.Model; if (validator is PropertyValidatorWithDynamicState) { DynamicState = new DynamicState(model, ((PropertyValidatorWithDynamicState)validator).Properties); } else { DynamicState = new DynamicState(model); } }
public override IEnumerable<ModelValidationResult> Validate(object container) { if (ShouldValidate) { var fakeRule = new PropertyRule(null, x => Metadata.Model, null, null, Metadata.ModelType, null) { PropertyName = Metadata.PropertyName, DisplayName = Rule == null ? null : Rule.DisplayName, }; var fakeParentContext = new ValidationContext(container); var context = new PropertyValidatorContext(fakeParentContext, fakeRule, Metadata.PropertyName); var result = Validator.Validate(context); foreach (var failure in result) { yield return new ModelValidationResult { Message = failure.ErrorMessage }; } } }
public EmailFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { ShouldValidate = false; }
public GreaterThanOrEqualPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { ShouldValidate = false; }
public GreatherThanPropertyValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
public FluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext) { this.Validator = validator; // Build a new rule instead of the one passed in. // We do this as the rule passed in will not have the correct properties defined for standalone validation. // We also want to ensure we copy across the CustomPropertyName and RuleSet, if specified. Rule = new PropertyRule(null, x => metadata.Model, null, null, metadata.ModelType, null) { PropertyName = metadata.PropertyName, DisplayName = rule?.DisplayName, RuleSets = rule?.RuleSets }; }
private ModelValidator GetModelValidator(ModelMetadata meta, ControllerContext context, PropertyRule rule, IPropertyValidator propertyValidator) { var type = propertyValidator.GetType(); var factory = validatorFactories .Where(x => x.Key.IsAssignableFrom(type)) .Select(x => x.Value) .FirstOrDefault() ?? FluentValidationPropertyValidator.Create; return factory(meta, context, rule.PropertyDescription, propertyValidator); }
public DefaultFluentAdapterFactoryFixture() { this.rule = new PropertyRule(null, null, null, null, null, null); this.factory = new DefaultFluentAdapterFactory(); }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public override IEnumerable <ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator) { yield return(new NotNullValidationRule( base.FormatMessage(rule, validator), base.GetMemberNames(rule))); }
public NotEqualPropertyValidator( ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { }
public MinFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule propertyDescription, IPropertyValidator validator) : base(metadata, controllerContext, propertyDescription, validator) { }
public EqualToFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { ShouldValidate = false; }
/// <summary> /// Create a custom result type rule /// </summary> /// <param name="resultTypeRule">The result type rule info object</param> /// <returns>The property rule</returns> public PropertyRule CreateCustomPropertyRule(ResultTypeRuleInfo resultTypeRule) { var type = typeof(PropertyRuleOperator); var info = type.GetProperty("DefaultOperators", BindingFlags.NonPublic | BindingFlags.Static); var value = info.GetValue(null); var defaultOperators = (Dictionary<PropertyRuleOperator.DefaultOperator, PropertyRuleOperator>)value; var rule = new PropertyRule(resultTypeRule.PropertyName, defaultOperators[resultTypeRule.Operator]) { PropertyValues = new List<string>(resultTypeRule.Values) }; return rule; }
public UniqueEmailPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { }
public RangeClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
/// <summary> /// Get the <see cref="ModelValidationRule"/> instances that are mapped from the fluent validation rule. /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="ModelValidationRule"/> instances.</returns> public abstract IEnumerable<ModelValidationRule> GetRules(PropertyRule rule, IPropertyValidator validator);
public CreditCardClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
public PhoneNumberClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
public EqualToValueFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { }
public MaxLengthClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
/// <summary> /// Gets the name of the members that the validator applied to. /// </summary> /// <returns>A string containing the name of members that the validator is applied to.</returns> protected virtual IEnumerable<string> GetMemberNames(PropertyRule rule) { yield return rule.PropertyName; }
/// <summary> /// Returns a <see cref="bool"/> indicating if the <paramref name="propertyRule"/> is conditional. /// </summary> internal static bool HasNoCondition(this PropertyRule propertyRule) { return(propertyRule?.Condition == null && propertyRule?.AsyncCondition == null); }
public GreaterThanOrEqualClientValidator(PropertyRule rule, IPropertyValidator validator, Func <HttpContext, string> dateFormatProvider) : base(rule, validator) { _dateFormatProvider = dateFormatProvider; }
public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName) { ParentContext = parentContext; Rule = rule; PropertyName = propertyName; }
public ActiveAndStatusMatchPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) { ShouldValidate = false; }
public RangeFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule propertyDescription, IPropertyValidator validator) : base(metadata, controllerContext, propertyDescription, validator) { ShouldValidate = false; }
/// <summary> /// Initializes a new instance of the <see cref="LessThanAdapter"/> class for the specified /// <paramref name="rule"/> and <paramref name="validator"/>. /// </summary> /// <param name="rule">The fluent validation <see cref="PropertyRule"/> that is being mapped.</param> /// <param name="validator">The <see cref="PropertyRule"/> of the rule.</param> public LessThanAdapter(PropertyRule rule, LessThanValidator validator) : base(rule, validator) { }
protected virtual ModelValidator GetModelValidator(ModelMetadata meta, ControllerContext context, PropertyRule rule, IPropertyValidator propertyValidator) { var type = propertyValidator.GetType(); var factory = validatorFactories .Where(x => x.Key.IsAssignableFrom(type)) .Select(x => x.Value) .FirstOrDefault() ?? ((metadata, controllerContext, description, validator) => new FluentValidationPropertyValidator(metadata, controllerContext, description, validator)); return(factory(meta, context, rule, propertyValidator)); }
public void AddConstraint_WithNullContraint_ThrowsArgumentException() { var rule = PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, "Rule 1"); rule.AddConstraint(null); }
public void CreatePropertyRule_WithPropertyExpression_ReturnsPropertyRule() { var rule = PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, "Rule 1"); Assert.IsInstanceOfType(rule, typeof(PropertyRule)); }
private IEnumerable <ModelValidationRule> GetValidationRule(PropertyRule rule, IPropertyValidator propertyValidator) { return(this.factory.Create(propertyValidator).GetRules(rule, propertyValidator)); }
public void CreatePropertyRule_WithNullPropertyExpression_ThrowsArgumentException() { PropertyRule.CreatePropertyRule <Customer, string> (null, "Rule 1"); }
public void CreatePropertyRule_WithNullName_ThrowsArgumentException() { PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, null); }
public EqualToClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator) { }
public void Setup() { var rule = PropertyRule.Create <Person, string>(x => x.Surname); builder = new RuleBuilder <Person, string>(rule); }
public void Property_should_return_null_when_it_is_not_a_property_being_validated() { builder = new RuleBuilder <Person, string>(PropertyRule.Create <Person, string>(x => "Foo")); builder.Rule.Member.ShouldBeNull(); }
public void PropertyDescription_should_return_property_name_split() { var builder = new RuleBuilder <Person, DateTime>(PropertyRule.Create <Person, DateTime>(x => x.DateOfBirth)); builder.Rule.GetDisplayName().ShouldEqual("Date Of Birth"); }
private static void Test() { PropertyRule rule = new PropertyRule(); rule.Add("title", "^title$"); rule.Add("content", "^content$"); DataPack pack = new DataPack(rule,""); foreach (KeyValuePair<string, string> pair in pack) { Console.WriteLine(pair.Key + "->" + pair.Value); } }