public static Contract IsNewFormatCellPhone(this Contract contract, string value, string property, string message)
 {
     if (string.IsNullOrEmpty(value) || !new Phone().Validate(value, true, true))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
 public static Contract IsCarLicensePlate(this Contract contract, string value, string property, string message)
 {
     if (string.IsNullOrEmpty(value) || !new CarLicensePlate().Validate(value))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
Exemple #3
0
 public static Contract IsCnpjOrCPF(this Contract contract, string value, string property, string message)
 {
     if (string.IsNullOrEmpty(value) || (!new Cnpj().Validate(value) && !new Cpf().Validate(value)))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
 public static Contract IsVoterDocument(this Contract contract, string value, string property, string message)
 {
     if (string.IsNullOrEmpty(value) || !new VoterDocument().Validate(value))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
 public static Contract IsPhone(this Contract contract, string value, string property, string message)
 {
     if (!new Phone().Validate(value))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
 public static Contract IsCellPhone(this Contract contract, string value, string property, string message, bool strictNineDigit = false)
 {
     if (string.IsNullOrEmpty(value) || !new Phone().Validate(value, strictNineDigit, true))
     {
         contract.AddNotification(property, message);
     }
     return(contract);
 }
Exemple #7
0
        public static Contract IsValidGuid(this Contract contract, string val, string property, string message)
        {
            if (!Guid.TryParse(val, out _))
            {
                contract.AddNotification(property, message);
            }

            return(contract);
        }
Exemple #8
0
        /// <summary>
        /// This method is a workaround beacuse exists a problem in oginal method HasMinLen from Flunt package in version 1.0.2
        /// see more in https://github.com/andrebaltieri/flunt/issues/19
        /// </summary>
        /// <returns></returns>
        public static Contract HasMaxLength(this Contract contract, string val, int max, string property, string message)
        {
            if (string.IsNullOrEmpty(val) || val.Length > max)
            {
                contract.AddNotification(property, message);
            }

            return(contract);
        }
Exemple #9
0
        public static Contract IsLowerThanExtension(this Contract contract, int valor, int comparado, string propriedade, string mensagem)
        {
            if (valor < comparado)
            {
                contract.AddNotification(propriedade, mensagem);
            }

            return(contract);
        }
Exemple #10
0
        protected override Contract ConfigurarContrato(Contract contract)
        {
            if (!ValidarCPF(Cpf))
            {
                contract.AddNotification("Cpf", $"O Cpf '{Cpf}' do Cliente é inválido.");
            }

            return(contract);
        }
Exemple #11
0
        public static Contract <T> IsBetweenLength <T>(this Contract <T> contract, string val, int min, int max, string property, string message)
        {
            val ??= string.Empty;

            if (!(val.Length >= min && val.Length <= max))
            {
                contract.AddNotification(property, message);
            }

            return(contract);
        }
        public static Contract IsPhone(this Contract contract, string value, string numberFormat, string property, string message, bool strictNineDigit = false)
        {
            var result = new Phone(new PhoneValidationOptions {
                Format = numberFormat, StrictNineDigit = strictNineDigit
            }).Validate(value);

            if (string.IsNullOrEmpty(value) || !result)
            {
                contract.AddNotification(property, message);
            }
            return(contract);
        }
        public static Contract IsGuid(this Contract contract, string val, string property, string message)
        {
            Guid guid;
            var  isValid = Guid.TryParse(val, out guid);

            if (!isValid)
            {
                contract.AddNotification(property, message);
            }

            return(contract);
        }
        public static Contract IsNewFormatCellPhone(this Contract contract, string value, string property, string message)
        {
            var result = new Phone(new PhoneValidationOptions {
                StrictNineDigit = true, CellPhonesOnly = true
            }).Validate(value);

            if (string.IsNullOrEmpty(value) || !result)
            {
                contract.AddNotification(property, message);
            }
            return(contract);
        }
        protected Contract ContratoBase(object entidade)
        {
            var contrato = new Contract();

            foreach (var property in entidade.GetType().GetProperties())
            {
                if (property.CustomAttributes.Any())
                {
                    if (property.GetValue(entidade) == null)
                    {
                        contrato.AddNotification(property.Name, $"{property.Name} de {entidade.GetType().Name} não pode ser nulo");
                    }
                    else if (string.IsNullOrEmpty(property.GetValue(entidade).ToString()))
                    {
                        contrato.AddNotification(property.Name, $"{property.Name} de {entidade.GetType().Name} não pode ser vazio");
                    }
                    else
                    {
                        var valor = property.GetValue(entidade).ToString();

                        if (SearchDatanotation(property, "RequiredAttribute") != null)
                        {
                            contrato.IsNotNullOrEmpty(valor, property.Name, $"{property.Name} de {entidade.GetType().Name} {valor} é obrigatório");
                        }

                        if (SearchDatanotation(property, "MaxLengthAttribute") != null)
                        {
                            var maxLength = (int)SearchDatanotation(property, "MaxLengthAttribute").ConstructorArguments[0].Value;
                            contrato.HasMaxLen(valor, maxLength, property.Name, $"{property.Name} de {entidade.GetType().Name} {valor} deve ter no máximo {maxLength} caracteres.");
                        }
                    }
                }
            }

            return(contrato);
        }
        public void Check_ContratoInvalido_RetornaUmaExcecao()
        {
            // Arrange
            var contract = new Contract();

            contract.AddNotification("Prop Qualquer", "Notificacao Qualquer");

            // Act
            var exception = Assert.Throws <DomainException>(() =>
            {
                contract.ThrowExceptionIfInvalid();
            });

            // Assert
            Assert.True(exception.Message.Contains("Notificacao Qualquer"));
        }
        public FiltroAtendimentoRequestContract(FiltroAtendimentoRequest itemValidacao)
        {
            List <string> camposObrigatorios = new List <string>
            {
                itemValidacao.NomeConsumidor,
                itemValidacao.NumDocumento
            };

            if (!camposObrigatorios.Any(x => !string.IsNullOrWhiteSpace(x)))
            {
                Contract.AddNotification("todos", "Informe pelo um campo para pesquisa");
            }

            Contract
            .Requires()
            .HasMinLen(itemValidacao.NomeConsumidor, 2, "nomeconsumidor", "Nome do Consumidor deve ter no minimo 2 caracteres");
        }
Exemple #18
0
        private void Validate()
        {
            if (string.IsNullOrWhiteSpace(_value))
            {
                contract.AddNotification(nameof(Nin), "The CPF is required .");
                return;
            }

            int[] multiplierOne = new int[9] {
                10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] multiplierTwo = new int[10] {
                11, 10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            string aux;
            string digit;
            int    sum, rest;

            var value = _value.Trim();

            value = _value.Replace(".", "").Replace("-", "");

            if (value.Length != 11)
            {
                contract.AddNotification(nameof(Nin), "CPF should have 11 chars.");
                return;
            }

            aux = value.Substring(0, 9);
            sum = 0;

            for (int i = 0; i < 9; i++)
            {
                sum += int.Parse(aux[i].ToString()) * multiplierOne[i];
            }

            rest = sum % 11;

            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit = rest.ToString();
            aux   = aux + digit;
            sum   = 0;

            for (int i = 0; i < 10; i++)
            {
                sum += int.Parse(aux[i].ToString()) * multiplierTwo[i];
            }

            rest = sum % 11;

            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit = digit + rest.ToString();

            if (!value.EndsWith(digit))
            {
                contract.AddNotification(nameof(Nin), "This CPF is invalid.");
            }
        }
Exemple #19
0
        public static Contract IsValidCpf(this Contract contract, string value)
        {
            int[] multiplierOne = new int[9] {
                10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] multiplierTwo = new int[10] {
                11, 10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            string aux;
            string digit;
            int    sum, rest;

            value = value.Trim();
            value = value.Replace(".", "").Replace("-", "");

            if (value.Length != 11)
            {
                contract.AddNotification(nameof(Nin), "This CPF is invalid.");
                return(contract);
            }

            aux = value.Substring(0, 9);
            sum = 0;

            for (int i = 0; i < 9; i++)
            {
                sum += int.Parse(aux[i].ToString()) * multiplierOne[i];
            }

            rest = sum % 11;

            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit = rest.ToString();
            aux   = aux + digit;
            sum   = 0;

            for (int i = 0; i < 10; i++)
            {
                sum += int.Parse(aux[i].ToString()) * multiplierTwo[i];
            }

            rest = sum % 11;

            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit = digit + rest.ToString();

            if (!value.EndsWith(digit))
            {
                contract.AddNotification(nameof(Nin), "This CPF is invalid.");
            }

            return(contract);
        }
Exemple #20
0
 private bool AddNotification(string message)
 {
     contract.AddNotification(nameof(Name), message);
     return(false);
 }
Exemple #21
0
        public static Contract IsCpf(this Contract obj, string value, string property, string message)
        {
            bool Valid()
            {
                int[] multiplicador1 = new int[9] {
                    10, 9, 8, 7, 6, 5, 4, 3, 2
                };
                int[] multiplicador2 = new int[10] {
                    11, 10, 9, 8, 7, 6, 5, 4, 3, 2
                };
                string tempCpf;
                string digito;
                int    soma;
                int    resto;

                value = value.Trim();
                value = value.Replace(".", "").Replace("-", "");
                if (value.Length != 11)
                {
                    return(false);
                }
                tempCpf = value.Substring(0, 9);
                soma    = 0;

                for (int i = 0; i < 9; i++)
                {
                    soma += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];
                }
                resto = soma % 11;
                if (resto < 2)
                {
                    resto = 0;
                }
                else
                {
                    resto = 11 - resto;
                }
                digito  = resto.ToString();
                tempCpf = tempCpf + digito;
                soma    = 0;
                for (int i = 0; i < 10; i++)
                {
                    soma += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];
                }
                resto = soma % 11;
                if (resto < 2)
                {
                    resto = 0;
                }
                else
                {
                    resto = 11 - resto;
                }
                digito = digito + resto.ToString();
                return(value.EndsWith(digito));
            }

            if (!Valid())
            {
                obj.AddNotification(property, message);
            }

            return(obj);
        }
 private bool AdicionarErro(string erro)
 {
     contract.AddNotification(nameof(Descricao), erro);
     return(false);
 }