Search code examples
c#validationpredicates

c# How to perform multiple validation and return error message with predicate


just see a sample code where multiple functions are getting called to perform validation and if any one validation fail then return false to calling environment and if all pass then return true.

how to customize the routine as a result when any validation will be fail then error message should be return and when all validation pass then empty string will be return. see my code and help me to customize the way i want it with predicate.

        private static Predicate<string>[] _checks = new Predicate<string>[]
        {
            ValidationSeries.IsAtLeastFiveChars,
            ValidationSeries.HasASpace,
            ValidationSeries.HasNoLeadingSpace,
            ValidationSeries.HasNoTrailingSpace
        };


private void button1_Click(object sender, EventArgs e)
{
    bool doesPassAllChecks = _checks.All(c => c("Hello Test"));
}

    public static class ValidationSeries
    {
            public static bool IsAtLeastFiveChars(string text)
            {
                return text.Length >= 5;
            }

            public static bool HasASpace(string text)
            {
                return text.Contains(' ');
            }

            public static bool HasNoLeadingSpace(string text)
            {
                return !text.StartsWith(" ");
            }

            public static bool HasNoTrailingSpace(string text)
            {
                return !text.EndsWith(" ");
            }
    }

looking for help. thanks


Solution

  • solved this way.

    public static class ValidationSeries
        {
            private static Predicate<string>[] _checks = new Predicate<string>[]
            {
                    ValidationSeries.IsAtLeastFiveChars,
                    ValidationSeries.HasASpace,
                    ValidationSeries.HasNoLeadingSpace,
                    ValidationSeries.HasNoTrailingSpace
            };
    
            public static bool Check(string s, ICollection<string> failedPredicateNames)
            {
    
                var failed = false;
                foreach (var check in _checks)
                {
                    if ( !check(s) )
                    {
                        failedPredicateNames.Add(check.Method.Name);
                        failed = true;
                    }
                }
                return !failed;
            }
    
            public static bool IsAtLeastFiveChars(string text)
            {
                return text.Length >= 5;
            }
    
            public static bool HasASpace(string text)
            {
                return text.Contains(" ");
            }
    
            public static bool HasNoLeadingSpace(string text)
            {
                return !text.StartsWith(" ");
            }
    
            public static bool HasNoTrailingSpace(string text)
            {
                return !text.EndsWith(" ");
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                var failures = new List<string>();
                if (!ValidationSeries.Check(" Hello World", failures))
                {
                    foreach (var f in failures)
                    {
                        Console.WriteLine(f);
                    }
                }
            }