Adicionada validação de CPF

This commit is contained in:
Luiz F Picolo 2024-05-29 20:12:33 -04:00
parent cba35fca03
commit ad8851d5d9
1 changed files with 14 additions and 5 deletions

View File

@ -3,16 +3,17 @@ import { cpf } from 'brazilian-doc-validator';
class PessoaFisica extends Pessoa { class PessoaFisica extends Pessoa {
#_cpf #_cpf
#validarCPF
constructor(nome) { constructor(nome) {
super(nome) super(nome)
} }
set cpf(_cpf) { set cpf(_cpf) {
if (cpf.validate(_cpf)){ try {
this.#_cpf = _cpf this.#_cpf = this.#validarCPF(_cpf)
} else { } catch(erro){
throw new Error("CPF Inválido") console.log(erro.message)
} }
} }
@ -20,6 +21,14 @@ class PessoaFisica extends Pessoa {
return this.#_cpf return this.#_cpf
} }
#validarCPF(_cpf){
if (!cpf.validate(_cpf)){
throw new Error("CPF Inválido")
}
return _cpf
}
mostrarDados(){ mostrarDados(){
return `Meus dados são: ${this.nome} e ${this.cpf}` return `Meus dados são: ${this.nome} e ${this.cpf}`
} }