paemuri / brdoc

Brazilian docs validator for Go - CPF, CNPJ, CEP, RENAVAM, CNH and license plate!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CPF without special characters

filipesilvaa opened this issue · comments

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

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! ;)

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 ?

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! 😉