Exemple #1
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 #2
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 #3
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 : IComparable <TProperty>, IComparable
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(new BetweenValidator(from, to))));
        }
Exemple #4
0
        /// <summary>
        /// Adds a credit card 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> CreditCard <TEntity>(this IEntityValidationRuleBuilderInitial <TEntity, string> ruleBuilder)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(CreditCardValidator.Instance)));
        }
Exemple #5
0
        /// <summary>
        /// Adds a not empty 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>
        /// <returns>Rule builder.</returns>
        public static IEntityValidationRuleBuilder <TEntity, TProperty> NotEmpty <TEntity, TProperty>(this IEntityValidationRuleBuilderInitial <TEntity, TProperty> ruleBuilder)
        {
            if (ruleBuilder == null)
            {
                throw new ArgumentNullException("ruleBuilder");
            }

            return(ruleBuilder.AddValidator(new EntityPropertyValidator(NotEmptyValidator.Instance)));
        }
Exemple #6
0
        /// <summary>
        /// Adds a not 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> NotEqualTo <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 NotEqualToValidator(toCompare, comparer))));
        }
Exemple #7
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))));
        }
Exemple #8
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 #9
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 #10
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 #11
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)));
        }