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

CPF without special characters #5

Closed
filipesilvaa opened this issue Jul 16, 2019 · 3 comments
Closed

CPF without special characters #5

filipesilvaa opened this issue Jul 16, 2019 · 3 comments

Comments

@filipesilvaa
Copy link

filipesilvaa commented Jul 16, 2019

Maybe a mismatch, if someone input "24843803480" like in your documentation (at README.md), it couldn't be valid, cause you are matching with a regex with special characters... and I saw that in your tests, you didn't test CPF like this.

one possible solution, you could clean all special characters before everything and match with length 11 (regex like ^\d{11}$ or just len(doc) == 11)

https://github.com/Nhanderu/brdoc/blob/8e80a668ca902bcc443abacacbacbd47c3503674/cpfcnpj.go#L45

@paemuri
Copy link
Owner

paemuri commented Jul 17, 2019

Hey!

Thank you for taking your time checking out this project!

Even though, I don't think that is, currently, a problem. 24843803480 wouldn't be an invalid CPF because the special chars are all optional (see the question marks in the regex: ^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$). To prove my point, I added {"24843803480", true} to the test cases in TestIsCPF (cpfcnpj_test.go#7), ran it locally and all the tests passed. 👍
Did you experience any problem while using the lib?

About your tip: I didn't want to clean it because that would prevent the code to validate invalid special characters (actually, that was the old solution). Those documents have a specific mask/format, and that should be validated too, not just the numbers. For example, and ignoring the consistency of the numbers here, 000.000.000-00 is a valid format, but 00.00.00.00-000 is not.

There is still room for improvement. The current solution checks every special char individually, which can lead to return something like a 000.000.00000 as a false positive, since it's not in the correct format. Still, I didn't want a giant complex regex pattern... Do you have anything in mind for that?

I'll let this issue open for awhile if there's anything more you'd like to discuss! ;)

@filipesilvaa
Copy link
Author

filipesilvaa commented Jul 22, 2019

Awesome, you're right, I didn't notice the ? at your regex, you can close the issue.

About your specific case 000.000.00000 maybe you can match as a group and test two specific cases using OR operator, you will test a case with special characters and other without special characters
(^\d{3}\.\d{3}\.\d{3}\-\d{2}$|^\d{3}\d{3}\d{3}\d{2}$), you can optimize this regex, to this (^\d{3}\.\d{3}\.\d{3}\-\d{2}$|^\d{11}$), at this case you don't need to use ?

@paemuri
Copy link
Owner

paemuri commented Aug 5, 2019

That is a solution! I'll think about implementing it.
I'll close this issue for now, since the original problem was resolved.
Thank you for your help! 😉

@paemuri paemuri closed this as completed Aug 5, 2019
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