Skip to content

Instantly share code, notes, and snippets.

@marcelocmenezes
Created August 25, 2020 20:12
Show Gist options
  • Save marcelocmenezes/bddaa515fc2a9af4d466b829af0a8820 to your computer and use it in GitHub Desktop.
Save marcelocmenezes/bddaa515fc2a9af4d466b829af0a8820 to your computer and use it in GitHub Desktop.
import * as Yup from 'yup';
import { validateCpf } from '../../utils/validateCpf';
import { validatePhone } from '../../utils/validatePhone';
function isValidCPF(this: Yup.StringSchema) {
return this.test({ name: 'cpf', message: 'CPF Inválido', test: value => validateCpf(value) });
}
function isValidPhoneNumber(this: Yup.StringSchema) {
return this.test({ name: 'sms', message: 'Número inválido', test: value => validatePhone(value) });
}
Yup.addMethod<Yup.StringSchema>(Yup.string, 'isValidCPF', isValidCPF);
Yup.addMethod<Yup.StringSchema>(Yup.string, 'isValidPhoneNumber', isValidPhoneNumber);
declare module 'yup' {
interface StringSchema {
isValidCPF(): Yup.StringSchema;
isValidPhoneNumber(): Yup.StringSchema;
}
}
export default Yup;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment