public bool Validate(Product product, PropertyInfo propertyInfo, string propertyValue,
                             out string errorMsg, out object convertedValue)
        {
            errorMsg       = null;
            convertedValue = propertyValue;
            switch (propertyInfo.Name)
            {
            case "Category":
            {
                var categories     = productCatalog.GetPropertyValidValues(propertyInfo);
                var validateResult = CommonValidator.ValidateStringContainsInArray(propertyValue, (string[])categories);
                if (validateResult == ValidateResult.Ok)
                {
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageStringDoesNotContainInArray(validateResult, (string[])categories,
                                                                                      "Category");
                return(false);
            }

            case "Model":
            case "Company":
            {
                if (propertyValue.Length >= MinModelLength && propertyValue.Length <= MaxModelLength)
                {
                    var regex = new Regex(@"(^[\S]+ ?){1,5}$");
                    if (regex.IsMatch(propertyValue))
                    {
                        var regex2 = new Regex(@"[\p{P}]{4,}");
                        if (!regex2.IsMatch(propertyValue))
                        {
                            return(true);
                        }

                        errorMsg =
                            $"\"{Misc.GetPropertyName(propertyInfo)}\" should not contain more than 3 punctuation marks adjacent to each other" +
                            Environment.NewLine;
                        return(false);
                    }

                    errorMsg =
                        $"\"{Misc.GetPropertyName(propertyInfo)}\" should consist of maximum 5 words, double whitespace characters is not allowed" +
                        Environment.NewLine;
                    return(false);
                }

                errorMsg =
                    $"\"{Misc.GetPropertyName(propertyInfo)}\" should have length from {MinModelLength} to {MaxModelLength}" +
                    Environment.NewLine;
                return(false);
            }

            default:
            {
                throw new ArgumentException();
            }
            }
        }
Beispiel #2
0
        public bool Validate(Product product, PropertyInfo propertyInfo, string propertyValue,
                             out string errorMsg, out object convertedValue)
        {
            errorMsg       = null;
            convertedValue = propertyValue;
            switch (propertyInfo.Name)
            {
            case "AuthorFirstName":
            case "AuthorLastName":
            case "AuthorMiddleName":
            {
                if (propertyValue.Length >= MinAuthorLength && propertyValue.Length <= MaxAuthorLength)
                {
                    var regex = new Regex(@"(^[\p{L}-']+ ?){1,2}$");
                    if (regex.IsMatch(propertyValue))
                    {
                        var regex2 = new Regex(@"[\p{P}]{2,}");
                        if (!regex2.IsMatch(propertyValue))
                        {
                            return(true);
                        }

                        errorMsg =
                            $"\"{Misc.GetPropertyName(propertyInfo)}\" should not contain more than 1 punctuation marks adjacent to each other" +
                            Environment.NewLine;
                        return(false);
                    }

                    errorMsg =
                        $"\"{Misc.GetPropertyName(propertyInfo)}\" should consist of maximum 2 words. Only letters, \"'\", \"-\" marks are allowed. Double whitespace characters is not allowed" +
                        Environment.NewLine;
                    return(false);
                }

                if (propertyInfo.Name.Equals("AuthorMiddleName") && propertyValue.Length == 0)
                {
                    return(true);
                }

                errorMsg =
                    $"\"{Misc.GetPropertyName(propertyInfo)}\" should have length from {MinAuthorLength} to {MaxAuthorLength}" +
                    Environment.NewLine;
                return(false);
            }

            case "Genre":
            {
                var genres         = productCatalog.GetPropertyValidValues(propertyInfo);
                var validateResult =
                    CommonValidator.ValidateStringContainsInArray(propertyValue, (string[])genres);
                if (validateResult == ValidateResult.Ok)
                {
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageStringDoesNotContainInArray(validateResult, (string[])genres,
                                                                                      "Genre");
                return(false);
            }

            default:
            {
                throw new ArgumentException();
            }
            }
        }
        public bool Validate(Product product, PropertyInfo propertyInfo, string propertyValue,
                             out string errorMsg, out object convertedValue)
        {
            errorMsg       = null;
            convertedValue = propertyValue;
            switch (propertyInfo.Name)
            {
            case "Category":
            {
                var categories     = productCatalog.GetPropertyValidValues(propertyInfo);
                var validateResult = CommonValidator.ValidateStringContainsInArray(propertyValue, (string[])categories);
                if (validateResult == ValidateResult.Ok)
                {
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageStringDoesNotContainInArray(validateResult, (string[])categories,
                                                                                      "Category");
                return(false);
            }

            case "MinAge":
            {
                var validateResult = CommonValidator.ValidateNonNegativeIntInRange(propertyValue, MinAge, MaxAge);
                if (validateResult == ValidateResult.Ok)
                {
                    convertedValue = int.Parse(propertyValue);
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageNonNegativeInteger(validateResult, MinAge, MaxAge,
                                                                             "Minimum Age");
                return(false);
            }

            case "MaxAge":
            {
                var toy            = (Toy)product;
                var validateResult =
                    CommonValidator.ValidateNonNegativeIntInRange(propertyValue, toy.MinAge, MaxAge);
                if (validateResult == ValidateResult.Ok)
                {
                    convertedValue = int.Parse(propertyValue);
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageNonNegativeInteger(validateResult, toy.MinAge, MaxAge,
                                                                             "Maximum Age");
                return(false);
            }

            case "Sex":
            {
                var sexes          = productCatalog.GetPropertyValidValues(propertyInfo);
                var validateResult = CommonValidator.ValidateStringContainsInArray(propertyValue, (string[])sexes);
                if (validateResult == ValidateResult.Ok)
                {
                    return(true);
                }

                errorMsg = CommonValidator.GetErrorMessageStringDoesNotContainInArray(validateResult, (string[])sexes, "Sex");
                return(false);
            }

            default:
            {
                throw new ArgumentException();
            }
            }
        }