Пример #1
0
        /// <summary>Applies a given set of validations to each element of a given collection.</summary>
        /// <param name="collectionLambda">A function that returns the collection; will default to the empty array in the event of a runtime error</param>
        /// <param name="validationMethod">A function that returns a validation status given an item in the collection and the item's index</param>
        /// <returns>The merged result of executing the validation method on each item in the collection</returns>
        protected ValidationStatus ForEach(Func <IEnumerable <dynamic> > collectionLambda, Func <dynamic, int, ValidationStatus> validationMethod)
        {
            IEnumerable <dynamic> collection;

            try { collection = collectionLambda() ?? new dynamic[0]; }
            catch { collection = new dynamic[0]; }
            return(ValidationStatus.MergeAll(collection.Select(validationMethod).ToArray()));
        }
Пример #2
0
        /// <summary>Executes the configured rules against the value returned by the given lambda.</summary>
        /// <param name="valueLambda">A function that returns the field's value for validation (NOTE: runtime errors in the evaluation of this lambda will cause it to return the default for the type `T`)</param>
        /// <param name="messagePrefix">(Optional) A bit of text to prepend to any error messages</param>
        /// <returns>The merged result of executing all the configured rules</returns>
        public ValidationStatus RunAgainst(Func <T> valueLambda,
                                           string messagePrefix = null)
        {
            T value;

            try { value = valueLambda(); }
            catch { value = default(T); }
            return(ValidationStatus.MergeAll(
                       Rules.Select(rule => rule.Run(value, Key, messagePrefix)).ToArray()));
        }