Exemplo n.º 1
0
        /// <summary>
        ///   Adds a dynamic property to a rule, which will be copied to any <see cref="FailedEvaluation" /> generated by the rule.
        /// </summary>
        /// <typeparam name="TTarget"> The <see cref="Type" /> of the target. </typeparam>
        /// <typeparam name="T"> The <see cref="Type" /> of the dynamic property. </typeparam>
        /// <param name="rule"> The rule. </param>
        /// <param name="value"> The value. </param>
        /// <returns> A clone of the source validation rule. </returns>
        public static ValidationRule <TTarget> With <TTarget, T>(this ValidationRule <TTarget> rule, T value)
        {
            var clone = rule.Clone();

            clone.Set(value);
            return(clone);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Assigns a message to the validation rule that will be displayed when the rule passes.
        /// </summary>
        /// <typeparam name="TTarget">The type of the object being validated.</typeparam>
        /// <param name="rule">The rule.</param>
        /// <param name="buildMessage">A function that returns a message for a <see cref="SuccessfulEvaluation" />.</param>
        /// <returns>
        /// A clone of the source validation rule.
        /// </returns>
        public static ValidationRule <TTarget> WithSuccessMessage <TTarget>(this ValidationRule <TTarget> rule,
                                                                            Func <SuccessfulEvaluation, TTarget, string> buildMessage)
        {
            var clone = rule.Clone();

            clone.Set <SuccessMessageTemplate>(new SuccessMessageTemplate <TTarget>(buildMessage));
            return(clone);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Provides an error message to be shown when the rule fails.
        /// </summary>
        /// <typeparam name="TTarget">The type of the object being validated.</typeparam>
        /// <param name="rule">The rule.</param>
        /// <param name="buildMessage">A function that returns a message appropriate to the validation failure.</param>
        /// <returns></returns>
        public static ValidationRule <TTarget> WithErrorMessage <TTarget>(
            this ValidationRule <TTarget> rule,
            Func <FailedEvaluation, TTarget, string> buildMessage)
        {
            var clone = rule.Clone();

            clone.Set <FailureMessageTemplate>(new FailureMessageTemplate <TTarget>(buildMessage));
            return(clone);
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Declares a precondition, which must evaluate to true before the <paramref name="rule" /> will be evaluated.
        /// </summary>
        /// <typeparam name="TTarget"> The type of the target. </typeparam>
        /// <param name="rule"> The rule. </param>
        /// <param name="preconditions"> The preconditions. </param>
        /// <returns> A clone of the source validation rule. </returns>
        public static ValidationRule <TTarget> When <TTarget>(this ValidationRule <TTarget> rule,
                                                              params IValidationRule <TTarget>[] preconditions)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }
            if (preconditions == null)
            {
                throw new ArgumentNullException(nameof(preconditions));
            }

            var newRule = rule.Clone();

            foreach (var precondition in preconditions)
            {
                newRule.AddPrecondition(precondition);
            }

            return(newRule);
        }