Skip to content

Commit

Permalink
Validate CPF and CNPJ
Browse files Browse the repository at this point in the history
  • Loading branch information
shff committed Mar 22, 2018
1 parent a951251 commit 00dfbdc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
25 changes: 20 additions & 5 deletions UI/app/components/elements/input-box.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

<script>
import format from "v-mask/src/format";
import { CPF, CNPJ } from "cpf_cnpj";
export default {
name: "InputBox",
Expand Down Expand Up @@ -104,6 +105,18 @@
type: Boolean,
default: false,
},
cpf: {
type: Boolean,
default: false,
},
cnpj: {
type: Boolean,
default: false,
},
validations: {
type: Array,
default: () => [],
},
},
data() {
return {
Expand All @@ -119,11 +132,13 @@
return this.value && this.value.length;
},
localErrors() {
return [
this.required && !this.length && "Campo obrigatório",
this.required && this.length < this.minSize && "Valor inválido",
this.email && !(/^.+@.+\..+$/.test(this.value)) && "E-mail inválido",
].filter(a => a && this.validate);
return this.validations.concat([
_ => this.required && !this.length && "Campo obrigatório",
_ => this.required && this.length < this.minSize && "Valor inválido",
a => this.email && !(/^.+@.+\..+$/.test(a)) && "E-mail inválido",
a => this.cpf && !CPF.isValid(a) && "CPF Inválido!",
a => this.cnpj && !CNPJ.isValid(a) && "CNPJ Inválido!",
]).map(a => a(this.value)).filter(a => a && this.validate);
},
focused() {
return this.focus || (this.value && this.value.length);
Expand Down
8 changes: 8 additions & 0 deletions UI/app/components/pages/enrollment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,17 @@
v-model="data.responsible.cpf"
v-if="data.responsible.discriminator == 'Person'"
:size="4"
:min-size="14"
cpf
label="CPF"
mask="###.###.###-##"
hint="Ex: 000.000.000-00" />
<InputBox
v-model="data.responsible.cnpj"
v-if="data.responsible.discriminator == 'Company'"
:size="4"
:min-size="18"
cnpj
label="CNPJ"
mask="##.###.###/####-##"
hint="Ex: 00.000.000/0000-00" />
Expand Down Expand Up @@ -390,13 +394,17 @@
v-model="data.guarantor.cpf"
v-if="data.guarantor.discriminator == 'Person'"
:size="4"
:min-size="14"
cpf
label="CPF"
mask="###.###.###-##"
hint="Ex: 000.000.000-00" />
<InputBox
v-model="data.guarantor.cnpj"
v-if="data.guarantor.discriminator == 'Company'"
:size="4"
:min-size="18"
cnpj
label="CNPJ"
mask="##.###.###/####-##"
hint="Ex: 00.000.000/0000-00" />
Expand Down
1 change: 1 addition & 0 deletions UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"ace-css": "^1.1.0",
"axios": "^0.18.0",
"cpf_cnpj": "^0.2.0",
"lodash": "^4.17.5",
"v-mask": "^1.3.2",
"vue": "^2.5.15",
Expand Down
4 changes: 4 additions & 0 deletions UI/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,10 @@ cosmiconfig@^2.1.0, cosmiconfig@^2.1.1:
parse-json "^2.2.0"
require-from-string "^1.1.0"

cpf_cnpj@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/cpf_cnpj/-/cpf_cnpj-0.2.0.tgz#a032c19242a38e8363edce4e78bb1fbe9d664aa5"

create-ecdh@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/create-ecdh/-/create-ecdh-4.0.0.tgz#888c723596cdf7612f6498233eebd7a35301737d"
Expand Down

0 comments on commit 00dfbdc

Please sign in to comment.