Last active
June 24, 2018 16:00
-
-
Save ivanpaulovich/6c7776aaff93e29e21ec3e037c9df2e9 to your computer and use it in GitHub Desktop.
A value object for the swedish Personnummer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class SSN | |
{ | |
private string _text; | |
const string RegExForValidation = @"^\d{6,8}[-|(\s)]{0,1}\d{4}$"; | |
public SSN(string text) | |
{ | |
if (string.IsNullOrWhiteSpace(text)) | |
throw new SSNShouldNotBeEmptyException("The 'SSN' field is required"); | |
Regex regex = new Regex(RegExForValidation); | |
Match match = regex.Match(text); | |
if (!match.Success) | |
throw new InvalidSSNException("Invalid SSN format. Use YYMMDDNNNN."); | |
_text = text; | |
} | |
public static implicit operator SSN(string text) | |
{ | |
return new SSN(text); | |
} | |
public static implicit operator string(SSN ssn) | |
{ | |
return ssn._text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment