Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what about handling CPF if zero at left? #42

Open
sibelius opened this issue Nov 9, 2020 · 5 comments
Open

what about handling CPF if zero at left? #42

sibelius opened this issue Nov 9, 2020 · 5 comments

Comments

@sibelius
Copy link

sibelius commented Nov 9, 2020

example

1004218907

image

context.true(isCPF('1004218907'));

isCPF says this is an invalid CPF

sibelius added a commit to sibelius/brazilian-values that referenced this issue Nov 9, 2020
@sibelius
Copy link
Author

sibelius commented Nov 9, 2020

failing test here #43

@sibelius
Copy link
Author

sibelius commented Nov 9, 2020

my current workaround

import { isCPF } from 'brazilian-values';

/**
 * Pattern to match formatted CPF (999.999.999-99) or 11 numbers.
 */
export const CPF_PATTERN = /^(\d{11}|\d{3}\.\d{3}\.\d{3}\-\d{2})$/;

const NonNumeric = /\D/g;

const mapToNumeric = (
  value: string,
): string => value.replace(NonNumeric, '');

const padding = '00000000000';

const leftPad = (str: string) => {
  return padding.substring(0, padding.length - str.length) + str;
}

export const isSafeCPF = (value: string): boolean => {
  if (!CPF_PATTERN.test(value)) {
    const onlyNumeric = mapToNumeric(value);
    const leftPadCpf = leftPad(onlyNumeric);

    return isCPF(leftPadCpf);
  }

  return isCPF(value);
}

@mechamobau
Copy link
Contributor

@sibelius maybe this can be an issue with 4devs validator (or I'm wrong) because the verifier digit returned by validation it's 71 and not 7

@sibelius
Copy link
Author

sibelius commented Nov 9, 2020

CPF can have less than 11 digits, so the current logic is wrong

Old CPF have less than 11 digits, my father CPF for instance

You need to left pad

@sibelius
Copy link
Author

sibelius commented Nov 9, 2020

another even smaller CPF example

image

5120101

these are all real CPFs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants