コード例 #1
0
        /// <summary>
        /// Performs validations after projecting the current value to a new type.
        /// </summary>
        /// <typeparam name="T">The type of the current value.</typeparam>
        /// <typeparam name="TNew">The type of the projected value.</typeparam>
        /// <param name="validator">The current validator context.</param>
        /// <param name="selector">The selector lambda.</param>
        /// <param name="action">The action to perform with the projected value.</param>
        /// <returns>The unmodified validator context.</returns>
        public static IValidatorContext <T> Select <T, TNew>(this IValidatorContext <T> validator, Func <T, TNew> selector,
                                                             Action <IValidatorContext <TNew> > action)
        {
            ValidationInternals.ValidateNotNull(validator, nameof(validator));
            ValidationInternals.ValidateNotNull(selector, nameof(selector));
            ValidationInternals.ValidateNotNull(action, nameof(action));

            var context = new ValidatorContext <TNew>(selector.Invoke(validator.Value), validator.Path, null);

            action.Invoke(context);

            return(validator);
        }
コード例 #2
0
        /// <summary>
        /// Selects a property for validation.
        /// </summary>
        /// <typeparam name="T">The type of the current value.</typeparam>
        /// <typeparam name="TNext">The type of the selected property.</typeparam>
        /// <param name="validator">The current validator context.</param>
        /// <param name="selector">
        ///     The property selector expression.
        ///     <para>
        ///         The expression must be a `MemberExpression` (e.g. `x => x.Property`).
        ///     </para>
        /// </param>
        /// <param name="action">The action to perform for the selected property.</param>
        /// <returns>The unmodified validator context.</returns>
        public static IValidatorContext <T> For <T, TNext>(this IValidatorContext <T> validator,
                                                           Expression <Func <T, TNext> > selector, Action <IValidatorContext <TNext> > action)
        {
            ValidationInternals.ValidateNotNull(validator, nameof(validator));
            ValidationInternals.ValidateNotNull(selector, nameof(selector));
            ValidationInternals.ValidateNotNull(action, nameof(action));

            var name  = ValidationInternals.GetPropertyName(selector);
            var value = selector.Compile().Invoke(validator.Value);

            var context = new ValidatorContext <TNext>(value, validator.Path, name);

            action.Invoke(context);

            return(validator);
        }