示例#1
0
        public bool IsValid()
        {
            // check if all data present
            if (!(
                    BirthYear != null &&
                    IssueYear != null &&
                    ExpirationYear != null &&
                    Height != null &&
                    HairColor != null &&
                    EyeColor != null &&
                    PassportId != null))
            {
                return(false);
            }

            // validate birth year
            if (BirthYear < 1920 || BirthYear > 2002)
            {
                return(false);
            }

            // validate issue year
            if (IssueYear < 2010 || IssueYear > 2020)
            {
                return(false);
            }

            // validate expiration year
            if (ExpirationYear < 2020 || ExpirationYear > 2030)
            {
                return(false);
            }

            // validate height
            if (!Height.EndsWith("cm") && !Height.EndsWith("in"))
            {
                return(false);
            }

            try
            {
                int    heightNumber = Convert.ToInt32(Height.Substring(0, Height.Length - 2));
                string heightType   = Height.Substring(Height.Length - 2);
                if (heightType == "cm" && (heightNumber < 150 || heightNumber > 193))
                {
                    return(false);
                }
                if (heightType == "in" && (heightNumber < 59 || heightNumber > 76))
                {
                    return(false);
                }
            } catch
            {
                // Cannot parse
                throw new Exception("Cannot parse " + Height);
            }

            // validate hair color
            if (HairColor.Length != 7 || !HairColor.StartsWith("#"))
            {
                return(false);
            }
            var allowedChars = new List <char> {
                '#', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
            };

            for (var i = 0; i < HairColor.Length; i++)
            {
                var currentChar = Convert.ToChar(HairColor.Substring(i, 1));
                if (!allowedChars.Contains(currentChar))
                {
                    return(false);
                }
            }

            // validate eye color
            var allowedValues = new List <string> {
                "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
            };

            if (!allowedValues.Contains(EyeColor))
            {
                return(false);
            }

            // validate passport number
            if (PassportId.Length != 9)
            {
                return(false);
            }
            allowedChars = new List <char> {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
            };
            for (var i = 0; i < PassportId.Length; i++)
            {
                var currentChar = Convert.ToChar(PassportId.Substring(i, 1));
                if (!allowedChars.Contains(currentChar))
                {
                    return(false);
                }
            }

            // Passed all validations - return true;
            return(true);
        }
示例#2
0
        private void Validator()
        {
            if (BirthYear == 0)
            {
                ValidationErrors.Add("Invalid Birth Year");
            }
            if (BirthYear < 1920 || BirthYear > 2002)
            {
                ValidationErrors.Add("Birth Year out of rage");
            }
            if (IssueYear == 0)
            {
                ValidationErrors.Add("Invalid Issue Year");
            }
            if (IssueYear < 2010 || IssueYear > 2020)
            {
                ValidationErrors.Add("Issue Year out of range");
            }
            if (ExpirationYear == 0)
            {
                ValidationErrors.Add("Invalid Expiration Year");
            }
            if (ExpirationYear < 2020 || ExpirationYear > 2030)
            {
                ValidationErrors.Add("Expiration Year out of range");
            }
            if (Height == 0)
            {
                ValidationErrors.Add("Invalid Height");
            }
            if (HeightUnit != "cm" && HeightUnit != "in")
            {
                ValidationErrors.Add("Missing Height Unit");
            }
            if (HeightUnit == "cm" && (Height < 150 || Height > 193))
            {
                ValidationErrors.Add("Height out of range (cm)");
            }
            if (HeightUnit == "in" && (Height < 59 || Height > 76))
            {
                ValidationErrors.Add("Height out of range (in)");
            }
            if (HairColor == null)
            {
                ValidationErrors.Add("Invalid Hair color");
            }
            if (HairColor != null && !HairColor.StartsWith("#"))
            {
                ValidationErrors.Add("Hair color code missing #");
            }
            if (HairColor != null && Regex.IsMatch(HairColor, "[g-z.]"))
            {
                ValidationErrors.Add("Hair color code is not valid");
            }
            if (HairColor != null && HairColor.Length != 7)
            {
                ValidationErrors.Add("Hair color code is wrong length");
            }
            if (EyeColor == null)
            {
                ValidationErrors.Add("Invalid Eye color");
            }
            if (!new List <string> {
                "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
            }.Contains(EyeColor))
            {
                ValidationErrors.Add("Eye color is not a valid option");
            }
            if (PassportId == null)
            {
                ValidationErrors.Add("Invalid PassportId");
            }
            if (PassportId != null && PassportId.Length != 9)
            {
                ValidationErrors.Add("PassportId must be 9 characters");
            }

            if (!ValidationErrors.Any())
            {
                IsValid = true;
            }
        }