Skip to content

Commit

Permalink
Merge pull request #31 from toshiossada/master
Browse files Browse the repository at this point in the history
Validação de CPF e CNPJ em um so campo
  • Loading branch information
lira92 authored Oct 7, 2020
2 parents 1f4f1ff + fe4880f commit 40f8be8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ This lib enables in your validation contracts this methods:
var contratct = new Contract()
.IsCpf(person.document, "Document", "Invalid document")
.IsCnpj(company.document, "Document", "Invalid document")
.IsCnpjOrCPF(empresa.documento, "Documento", "Documento inválido")
.IsPhone(company.phone, "Phone", "Invalid phone")
.IsCellPhone(person.cellphone, "Phone", "Invalid cellphone")
.IsNewFormatCellPhone(person.cellphone, "Phone", "Invalid cellphone")
Expand All @@ -53,6 +54,7 @@ Essa biblioteca possibilita esses métodos em seus Validation Contracts:
var contratct = new Contract()
.IsCpf(pessoa.documento, "Documento", "Documento inválido")
.IsCnpj(empresa.documento, "Documento", "Documento inválido")
.IsCnpjOrCPF(empresa.documento, "Documento", "Documento inválido")
.IsPhone(empresa.telefone, "Telefone", "Telefone inválido")
.IsCellPhone(pessoa.telefone, "Telefone", "Telefone inválido")
.IsNewFormatCellPhone(pessoa.telefone, "Telefone", "Telefone inválido")
Expand Down
15 changes: 11 additions & 4 deletions src/Flunt.Br/Extensions/PersonContractExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Flunt.Br.Validations;
using Flunt.Validations;
using System;

namespace Flunt.Br.Extensions
{
Expand All @@ -23,6 +23,13 @@ public static Contract IsCnpj(this Contract contract, string value, string prope
return contract;
}

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))
Expand All @@ -32,9 +39,9 @@ public static Contract IsVoterDocument(this Contract contract, string value, str

public static Contract IsCnh(this Contract contract, string value, string property, string message)
{
if(string.IsNullOrEmpty(value) || !new Cnh().Validate(value))
contract.AddNotification(property,message);
if (string.IsNullOrEmpty(value) || !new Cnh().Validate(value))
contract.AddNotification(property, message);
return contract;
}
}
}
}
50 changes: 49 additions & 1 deletion tests/Flunt.Br.Tests/PersonContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,54 @@ public void IsCpf_Valid(string value)
.IsCpf(value, "document", "Invalid document");
Assert.AreEqual(true, right.Valid);
}

[TestMethod]
[DataRow("12345678910")]
[DataRow("124.835.069-34")]
[DataRow("000.000.000-00")]
[DataRow("111.111.111-11")]
[DataRow("222.222.222-22")]
[DataRow("333.333.333-33")]
[DataRow("444-444.444-44")]
[DataRow("555.555.555-55")]
[DataRow("666.666.666-66")]
[DataRow("777.777.777-77")]
[DataRow("888.888.888-88")]
[DataRow("999.999.999-99")]
[DataRow("123456789101112")]
[DataRow("655618111115522")]
[DataRow("00000000000abc")]
[DataRow("45.448.481/0501-18")]
[DataRow("00.000.000/0000-00")]
[DataRow("11.111.111/1111-11")]
[DataRow("22.222.222/2222-22")]
[DataRow("33.333.333/3333-33")]
[DataRow("44.444.444/4444-44")]
[DataRow("55.555.555/5555-55")]
[DataRow("66.666.666/6666-66")]
[DataRow("77.777.777/7777-77")]
[DataRow("88.888.888/8888-88")]
[DataRow("99.999.999/9999-99")]
[DataRow(null)]
public void IsCnpjOrCpf_InValid(string value)
{
var wrong = new Contract()
.IsCnpjOrCPF(value, "document", "Invalid document");
Assert.IsFalse(wrong.Valid);
}

[TestMethod]
[DataRow("16880249000179")]
[DataRow("45.448.421/0001-18")]
[DataRow("08381614996")]
[DataRow("489.062.735-92")]
public void IsCnpjOrCpf_Valid(string value)
{
var right = new Contract()
.IsCnpjOrCPF(value, "document", "Invalid document");
Assert.IsTrue(right.Valid);
}


[TestMethod]
[DataRow("123456789101112")]
Expand Down Expand Up @@ -118,4 +166,4 @@ public void IsCnh_Valid(string value)
Assert.IsTrue(right.Valid);
}
}
}
}

0 comments on commit 40f8be8

Please sign in to comment.