예제 #1
0
        public ValidationResult Validate(Worker model)
        {
            if (string.IsNullOrWhiteSpace(model.FullName))
            {
                return(ValidationResult.Fail(nameof(model.FullName), "Expected non-empty"));
            }

            var result = ValidateLength(model.FullName, nameof(model.FullName), 120);

            if (!result.IsSuccess)
            {
                return(result);
            }

            if (!StringValidators.ValidateLettersAndNumbers(model.FullName))
            {
                return(ValidationResult.Fail(nameof(model.FullName), "Expected numbers or letters"));
            }

            if (!string.IsNullOrEmpty(model.Phone))
            {
                if (!StringValidators.ValidatePhone(model.Phone))
                {
                    return(ValidationResult.Fail(nameof(model.Phone), "Expected numeric"));
                }
            }

            if (!string.IsNullOrEmpty(model.Position))
            {
                result = ValidateLength(model.Position, nameof(model.Position), 120);
                if (!result.IsSuccess)
                {
                    return(result);
                }
            }

            if (model.AdditionalInfo != null && model.AdditionalInfo.Length > 500)
            {
                return(ValidationResult.Fail(nameof(model.AdditionalInfo), "Expected length <= 500"));
            }

            return(ValidationResult.Success());
        }
예제 #2
0
        public ValidationResult Validate(Customer model)
        {
            if (!string.IsNullOrEmpty(model.Name))
            {
                var result = ValidateLength(model.Name, nameof(model.Name), 120);
                if (!result.IsSuccess)
                {
                    return(result);
                }

                if (!StringValidators.ValidateLettersAndNumbers(model.Name))
                {
                    return(ValidationResult.Fail(nameof(model.Name), "Expected numbers or letters"));
                }
            }

            if (!string.IsNullOrEmpty(model.Email))
            {
                var result = ValidateLength(model.Email, nameof(model.Email), 100);
                if (!result.IsSuccess)
                {
                    return(result);
                }
            }

            if (!string.IsNullOrEmpty(model.CustomId))
            {
                var result = ValidateLength(model.CustomId, nameof(model.CustomId), 100);
                if (!result.IsSuccess)
                {
                    return(result);
                }
            }

            if (model.Birthday != null)
            {
                var birthdayValidation = birthdayValidator.Validate(model.Birthday);
                if (!birthdayValidation.IsSuccess)
                {
                    birthdayValidation.ErrorType = $"{nameof(model.Birthday)}.{birthdayValidation.ErrorType}";
                    return(birthdayValidation);
                }
            }

            if (model.Discount != null && (decimal.Round(model.Discount.Value, 2) != model.Discount || model.Discount > 100 || model.Discount < 0))
            {
                return(ValidationResult.Fail(nameof(model.Discount), "Expected value between [0, 100] and 2 decimals"));
            }

            if (model.Gender != null && model.Gender != Gender.Male && model.Gender != Gender.Female)
            {
                return(ValidationResult.Fail(nameof(Gender), "Expected Male or Female"));
            }

            if (model.AdditionalInfo != null && model.AdditionalInfo.Length > 500)
            {
                return(ValidationResult.Fail(nameof(model.AdditionalInfo), "Expected length <= 500"));
            }

            if (string.IsNullOrWhiteSpace(model.Name) && string.IsNullOrWhiteSpace(model.Phone) && string.IsNullOrWhiteSpace(model.CustomId))
            {
                return(ValidationResult.Fail($"{nameof(model.Name)}, {nameof(model.Phone)}, {nameof(model.CustomId)}", "Expected at least one non-empty"));
            }

            return(ValidationResult.Success());
        }
 public bool ValidateLettersAndNumbers(string name)
 {
     return(StringValidators.ValidateLettersAndNumbers(name));
 }