Exemple #1
0
        /// <summary>
        /// Adds a url regular expression validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, string> Url <TEntity>(this IEntityValidationRuleBuilderInitial <TEntity, string> ruleBuilder)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(UrlValidator.Instance)));
        }
Exemple #2
0
        private static void AssertAddValidatorIsCalled <TValidator, TEntity, TProperty>(Action <IEntityValidationRuleBuilderInitial <TEntity, TProperty> > action)
            where TValidator : IValidator
        {
            IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder = Substitute.For <IEntityValidationRuleBuilderInitial <TEntity, TProperty> >();

            ruleBuilder.WhenForAnyArgs(x => x.AddValidator(null)).Do(x => Assert.IsInstanceOf <TValidator>(x.Arg <EntityPropertyValidator>().InnerValidator));

            action(ruleBuilder);

            ruleBuilder.ReceivedWithAnyArgs(1).AddValidator(null);
        }
Exemple #3
0
        /// <summary>
        /// Adds a length validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="exactLength">Length of the exact.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, string> Length <TEntity>(this IEntityValidationRuleBuilderInitial <TEntity, string> ruleBuilder, int exactLength)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new LengthValidator(exactLength, exactLength))));
        }
Exemple #4
0
        /// <summary>
        /// Adds a max length validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="max">The maximum.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, string> MaxLength <TEntity>(this IEntityValidationRuleBuilderInitial <TEntity, string> ruleBuilder, int max)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(LengthValidator.CreateMaxLengthValidator(max))));
        }
Exemple #5
0
        /// <summary>
        /// Adds a between validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="from">The lowest value.</param>
        /// <param name="to">The highest value.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, TProperty?> Between <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty?> ruleBuilder, TProperty from, TProperty to)
            where TProperty : struct, IComparable <TProperty>, IComparable
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new BetweenValidator(from, to))));
        }
Exemple #6
0
        /// <summary>
        /// Adds a greater than or equal to validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, TProperty?> GreaterThanOrEqualTo <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty?> ruleBuilder, TProperty valueToCompare)
            where TProperty : struct, IComparable <TProperty>, IComparable
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new GreaterThanOrEqualToValidator(valueToCompare))));
        }
Exemple #7
0
        /// <summary>
        /// Adds a less than validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="valueToCompare">The value to compare.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> LessThan <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder, TProperty valueToCompare)
            where TProperty : IComparable <TProperty>, IComparable
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new LessThanValidator(valueToCompare))));
        }
Exemple #8
0
        /// <summary>
        /// Adds a predicate validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="predicate">The predicate.</param>
        /// <returns>Rule builder.</returns>
        /// <exception cref="System.ArgumentNullException">predicate</exception>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> Must <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder, Func <TProperty, bool> predicate)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            if (predicate == null)
            {
                throw new ArgumentNullException("predicate");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new PredicateValidator(instance => predicate((TProperty)instance)))));
        }
Exemple #9
0
        /// <summary>
        /// Adds an equal to property validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="compareExpression">The compare expression.</param>
        /// <param name="comparer">The comparer.</param>
        /// <returns>Rule builder.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// ruleBuilder
        /// or
        /// compareExpression
        /// </exception>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> EqualTo <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder, Expression <Func <TEntity, TProperty> > compareExpression, IEqualityComparer comparer = null)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            if (compareExpression == null)
            {
                throw new ArgumentNullException("compareExpression");
            }

            Func <TEntity, TProperty> memberToCompareFunc = compareExpression.Compile();

            return(ruleBuilder.AddValidator(new EqualToEntityPropertyValidator(x => memberToCompareFunc((TEntity)x), LinqUtils.GetMemberInfo(compareExpression), typeof(TEntity), true, comparer)));
        }
Exemple #10
0
        /// <summary>
        /// Adds an equal to validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <typeparam name="TProperty">The type of the property.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="toCompare">The value to compare.</param>
        /// <param name="comparer">The comparer.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> EqualTo <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder, TProperty toCompare, IEqualityComparer comparer = null)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new EqualToValidator(toCompare, comparer)
            {
                OwnerType = typeof(TEntity)
            })));
        }
Exemple #11
0
        /// <summary>
        /// Adds a regular expression validator to the rule builder.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="ruleBuilder">The rule builder.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="regexOptions">The regex options.</param>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, string> Matches <TEntity>(this IEntityValidationRuleBuilderInitial <TEntity, string> ruleBuilder, string expression, RegexOptions regexOptions = RegexOptions.None)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new RegexValidator(expression, regexOptions))));
        }