コード例 #1
0
ファイル: FallbackAdapter.cs プロジェクト: Borzoo/Nancy
 /// <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));
 }
コード例 #2
0
 public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName)
 {
     ParentContext = parentContext;
     Rule = rule;
     PropertyName = propertyName;
     propertyValueContainer = new Lazy<object>(() => rule.PropertyFunc(parentContext.InstanceToValidate));
 }
コード例 #3
0
        /// <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;
        }
コード例 #4
0
		public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName, object propertyValue)
		{
			ParentContext = parentContext;
			Rule = rule;
			PropertyName = propertyName;
			propertyValueContainer = new Lazy<object>(() => propertyValue);
		}
コード例 #5
0
 /// <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);
 }
コード例 #6
0
        /// <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;
        }
コード例 #8
0
ファイル: LessThanAdapter.cs プロジェクト: RadifMasud/Nancy
 /// <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);
 }
コード例 #9
0
ファイル: Properties.cs プロジェクト: xeno-by/xenogears
 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;
 }
コード例 #10
0
ファイル: LengthAdapter.cs プロジェクト: RadifMasud/Nancy
 /// <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);
 }
コード例 #11
0
 public SimpleUnitaryClientSideValidator(ModelMetadata metadata,
                                         ControllerContext controllerContext,
                                         PropertyRule rule,
                                         IPropertyValidator validator,
                                         string validationType)
     : base(metadata, controllerContext, rule, validator)
 {
     _validationType = validationType;
 }
コード例 #12
0
		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;
		}
コード例 #13
0
 /// <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;
 }
コード例 #14
0
ファイル: AdapterBase.cs プロジェクト: jbattermann/Nancy
 /// <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());
     };
 }
コード例 #15
0
		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);
		}
コード例 #16
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();
		}
コード例 #17
0
        /// <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);
        }
コード例 #18
0
		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
			};
		}
コード例 #19
0
        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);
        }
コード例 #20
0
        /// <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);
        }
コード例 #21
0
        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);
            }
        }
コード例 #22
0
		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 };
				}
			}
		}
コード例 #23
0
        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;
        }
コード例 #24
0
        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);
            }
        }
コード例 #25
0
 public EmailFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator)
 {
     ShouldValidate = false;
 }
コード例 #26
0
 public GreaterThanOrEqualPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
     : base(metadata, controllerContext, rule, validator)
 {
     ShouldValidate = false;
 }
コード例 #27
0
 public GreatherThanPropertyValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #28
0
        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
            };
        }
コード例 #29
0
        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);
        }
コード例 #30
0
 public DefaultFluentAdapterFactoryFixture()
 {
     this.rule    = new PropertyRule(null, null, null, null, null, null);
     this.factory = new DefaultFluentAdapterFactory();
 }
コード例 #31
0
ファイル: NotNullAdapter.cs プロジェクト: yannisgu/Nancy
 /// <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)));
 }
コード例 #32
0
 public NotEqualPropertyValidator(
     ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
     : base(metadata, controllerContext, rule, validator)
 {
 }
コード例 #33
0
 public MinFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule propertyDescription, IPropertyValidator validator)
     : base(metadata, controllerContext, propertyDescription, validator)
 {
 }
コード例 #34
0
		public EqualToFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator) {
			ShouldValidate = false;
		}
コード例 #35
0
ファイル: SearchHelper.cs プロジェクト: andresglx/Dynamite
        /// <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)
 {
 }
コード例 #37
0
 public RangeClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #38
0
ファイル: AdapterBase.cs プロジェクト: jbattermann/Nancy
 /// <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);
コード例 #39
0
 public CreditCardClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #40
0
 public PhoneNumberClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #41
0
 public EqualToValueFluentValidationPropertyValidator(ModelMetadata metadata,
                                                      ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator)
     : base(metadata, controllerContext, rule, validator)
 {
 }
コード例 #42
0
 public MaxLengthClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #43
0
ファイル: AdapterBase.cs プロジェクト: jbattermann/Nancy
 /// <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;
 }
コード例 #44
0
 /// <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);
 }
コード例 #45
0
 public GreaterThanOrEqualClientValidator(PropertyRule rule, IPropertyValidator validator, Func <HttpContext, string> dateFormatProvider) : base(rule, validator)
 {
     _dateFormatProvider = dateFormatProvider;
 }
コード例 #46
0
 public PropertyValidatorContext(ValidationContext parentContext, PropertyRule rule, string propertyName)
 {
     ParentContext = parentContext;
     Rule          = rule;
     PropertyName  = propertyName;
 }
コード例 #47
0
 public ActiveAndStatusMatchPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule rule, IPropertyValidator validator) : base(metadata, controllerContext, rule, validator)
 {
     ShouldValidate = false;
 }
コード例 #48
0
 public RangeFluentValidationPropertyValidator(ModelMetadata metadata, ControllerContext controllerContext, PropertyRule propertyDescription, IPropertyValidator validator) : base(metadata, controllerContext, propertyDescription, validator)
 {
     ShouldValidate = false;
 }
コード例 #49
0
ファイル: LessThanAdapter.cs プロジェクト: williware/Nancy
 /// <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)
 {
 }
コード例 #50
0
        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));
        }
コード例 #51
0
        public void AddConstraint_WithNullContraint_ThrowsArgumentException()
        {
            var rule = PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, "Rule 1");

            rule.AddConstraint(null);
        }
コード例 #52
0
        public void CreatePropertyRule_WithPropertyExpression_ReturnsPropertyRule()
        {
            var rule = PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, "Rule 1");

            Assert.IsInstanceOfType(rule, typeof(PropertyRule));
        }
コード例 #53
0
 private IEnumerable <ModelValidationRule> GetValidationRule(PropertyRule rule, IPropertyValidator propertyValidator)
 {
     return(this.factory.Create(propertyValidator).GetRules(rule, propertyValidator));
 }
コード例 #54
0
 public void CreatePropertyRule_WithNullPropertyExpression_ThrowsArgumentException()
 {
     PropertyRule.CreatePropertyRule <Customer, string> (null, "Rule 1");
 }
コード例 #55
0
 public void CreatePropertyRule_WithNullName_ThrowsArgumentException()
 {
     PropertyRule.CreatePropertyRule <Customer, string> (c => c.FirstName, null);
 }
コード例 #56
0
 public EqualToClientValidator(PropertyRule rule, IPropertyValidator validator) : base(rule, validator)
 {
 }
コード例 #57
0
        public void Setup()
        {
            var rule = PropertyRule.Create <Person, string>(x => x.Surname);

            builder = new RuleBuilder <Person, string>(rule);
        }
コード例 #58
0
 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();
 }
コード例 #59
0
        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");
        }
コード例 #60
-2
ファイル: Program.cs プロジェクト: ReinhardHsu/devfw
        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);
            }
        }