public static bool TryParse(string value, out ValidUsername username, out IReadOnlyCollection <string> errorMessages) { var errorMessageList = new List <string>(); errorMessages = errorMessageList; if (IsEmpty(value)) { // TryParse should never fail, so report null as an error instead of ArgumentNullException. errorMessageList.Add("Value required"); } else { value = Username.Normalize(value); if (value.Length < MinLength || value.Length > MaxLength) { errorMessageList.Add(string.Format("Length must be from {0} to {1} characters", MinLength, MaxLength)); } if (!Pattern.IsMatch(value)) { errorMessageList.Add("Only alphanumeric characters and underscores are allowed"); } } if (errorMessageList.Count > 0) { username = null; return(false); } username = new ValidUsername(value); return(true); }
public static bool TryParse(string value, out ValidUsername username) { IReadOnlyCollection <string> errorMessages; return(TryParse(value, out username, out errorMessages)); }