Error message for duplicates in list

May 30, 2011 at 5:03 PM
Edited May 30, 2011 at 5:04 PM

This is another feedback question because I couldn't find anything specific in the the dicussions or the documentation.

Lets say I need to check for duplicate credit card numbers in my list of credit cards for a customer and I want to have my error say "Duplicate credit card - 5555 5555 5555 5555".

If I do this:

Check(customer => c.CreditCards).Optional().Expect(CheckDuplicateCreditCard, "Duplicate Credit Card"); 

public bool CheckDuplicateCreditCard(Customer customer, CreditCard[] creditCards) 
        //return false if duplicate found, else return true. 
I don't understand how to return the card number in my error message. 
Of course, life would get a bit complicated if there are multiple sets of duplicates. 
The only thought I have is to create a custom validator but your suggestions would be greatly appreciated.
May 31, 2011 at 4:28 PM


We've ran into this same problem before and have an approach on our backlog. But, if you are willing to pull down the code and do a build, I checked in a small fix that will allow you to do something like this:


public class PaymentSpecification: Validates<Payment>
        public PaymentSpecification()
            Check(p => GetDuplicates(p.CreditCardNumber)).Optional().IsEmpty().With( m=> m.Message = "Duplicates Credit Cards: {PropertyValue}");

        public string GetDuplicates(List<string> numbers)
            var dups = from c in numbers
                       group c by c
                       into g
                       where g.Count() > 1
                       select g.Key;

            //Need to have the value of the property be a list of strings
            return string.Join(", ", dups);

Note that you can use an expression or a method return value as a property. This just creates a property that is a comma delimited string that contains any duplicated numbers. This will allow you to use a Custom Error Message and inject the {PropertyValue}. We're not sure how we feel about this approach long term, and we'd like to see the Expect rule actually return the error message, but I think this will solve your immediate need.



Jun 1, 2011 at 6:26 PM

Wow, that was fast. Thank you, it works.

If you like, I have some unsolicited feedback with this approach (which might be why you are unsure of it too).

  • Since we are "binding" the check statement to a string, we can't do any "chained" validation after this check on the original list - this is clearly a limitation with this example and may not be a limitation with the approach at all.
  • I agree with you that it would be preferable for the expect rule to "return" the error message or somehow add a ValidationResult to ValidationNotification - this obviously would require exposing ValidationNotification in the Validates<something> class so the expect rule can use it or maybe pass it in as an argument. I haven't spent enough time to completely understand the code to determine if that is even possible. Even if it is possible, it might violate your purity-of-essence. So basically, I don't really have a suggestion, yet.

Thanks for all your help.