koblas / stdnum-js

A JavaScript library to provide functions to handle, parse and validate standard numbers.
MIT License
39 stars 26 forks source link

Polish PESEL - check digit 0 #64

Closed teebu closed 1 year ago

teebu commented 1 year ago

https://en.wikipedia.org/wiki/PESEL

Checksum calculation Having a PESEL in the form of ABCDEFGHIJK, one can check the validity of the number by computing the following expression:

A×1 + B×3 + C×7 + D×9 + E×1 + F×3 + G×7 + H×9 + I×1 + J×3

The checksum is the last digit of result of the above expression subtracted from 10. If this last digit is 0 then the checksum is 0.

If the result of the last operation is not equal to the last digit (K) of a given PESEL, the PESEL is incorrect. This system works reliably well for catching one-digit mistakes and digit swaps.

test value: 09222509560 should be valid.

Running it against https://calculla.com/calculators/decoder/poland_pesel_decoder validator

Also tested it against this lib https://github.com/radarsu/validate-polish/blob/master/src/index.ts#L50 return 10 - (control === 0 ? 10 : control) === dig11;

PDF article: https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Poland-TIN.pdf it doesnt say what happens when its 10?

teebu commented 1 year ago

proposed fix:

 if (String(10 - (sum === 0 ? 10 : sum)) !== check) {
      return { isValid: false, error: new exceptions.InvalidChecksum() };
}
koblas commented 1 year ago

The better fix in these cases is (10 - sum) % 10)

koblas commented 1 year ago

Fixed in PR #65