Skip to content

Commit

Permalink
Merge pull request #56 from koblas/fr_nif_checksum
Browse files Browse the repository at this point in the history
fix: checksum calculation for fr/nif
  • Loading branch information
koblas authored Jun 22, 2023
2 parents 2a95a45 + 78663e7 commit 1a86212
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/fr/nif.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ describe('fr/nif', () => {
expect(result).toEqual('07 01 987 765 432');
});

it('validate:3023217600053', () => {
const result = validate('302 321 7600 053');

expect(result.isValid && result.compact).toEqual('3023217600053');
});

it('validate:0701987765432', () => {
const result = validate('0701987765432');
const result = validate('0701987765493');

expect(result.isValid && result.compact).toEqual('0701987765432');
expect(result.isValid && result.compact).toEqual('0701987765493');
});

it('validate:12345678', () => {
Expand Down
7 changes: 7 additions & 0 deletions src/fr/nif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const impl: Validator = {
return { isValid: false, error: new exceptions.InvalidFormat() };
}

const [prefix, check] = strings.splitAt(value, 10);
const pvalue = parseInt(prefix, 10);

if (String(pvalue % 511).padStart(3, '0') !== check) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}

return {
isValid: true,
compact: value,
Expand Down

0 comments on commit 1a86212

Please sign in to comment.