Friday, 14 March 2014

Interesting validation problem on new user's email, caused by a TLD in caps (and using NCrunch to test it)

While working on this issue and improving the Unit Test coverage of TeamMentor's user creation code, I noticed that:
    a) these emails worked:
    abc@def.ghi , ABC@def.ghi , abc@EDF.ghi
    b) but these ones didn't:
    abc@def.Ghi , abc@def.gHi , abc@def.GHI

Basically, if the TLD (top level domain) part of the email address was in caps, the data validation failed, and the new user creation did not complete.

The problem was the email validation regex used:
public class ValidationRegex
{
      public const string Email = @"^[\w-+\.]{1,}\@([\w-]{1,}\.){1,}[a-z]{2,4}$";
}
which was changed to (diff in bold)
public const string Email = @"^[\w-+\.]{1,}\@([\w-]{1,}\.){1,}[a-zA-Z]{2,4}$";
After this fix, the Unit Test I was working on passed with no errors:



For reference, here is what the Data Contract (for new user creation) looks like:



For a detailed explanation on how this is working and was implemented see Validating a POCO DataContract using .NET's DataAnnotations Validator.

Also interesting is the history of the regex I'm currently using: The Email RegEx that (could had) DOSed a site

NCrunch in action

If you noticed those green dots on the test source code, they are from NCrunch VisualStudio plugin which is really making a massive difference in my .NET development speed, quality and enjoyment.

For example, if I change back the RegEx to the original value:



 ... NCrunch will pick up the fact that the code changed, and in a couple secs, this test fails:



... and this code was flagged as part of the 'path that had a failed test'




Here are a couple posts I've written about NCrunch: