Skip to content

Commit

Permalink
Merge pull request #57 from koblas/fr_nif_000
Browse files Browse the repository at this point in the history
fix: fr/nif all 0s is not a valid value
  • Loading branch information
koblas committed Jun 24, 2023
2 parents ff69f6f + a2094d5 commit dcbc70c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/fr/nif.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ describe('fr/nif', () => {
expect(result).toEqual('07 01 987 765 432');
});

it('validate:3023217600053', () => {
const result = validate('302 321 7600 053');
test.each(['3023217600053', '0701987765493'])('validate:%s', value => {
const result = validate(value);

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

it('validate:0701987765432', () => {
const result = validate('0701987765493');
test.each(['0000000000000'])('validate:%s', value => {
const result = validate(value);

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

it('validate:12345678', () => {
Expand Down
5 changes: 5 additions & 0 deletions src/fr/nif.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const impl: Validator = {
const [prefix, check] = strings.splitAt(value, 10);
const pvalue = parseInt(prefix, 10);

// A zero value is not a valid value
if (pvalue === 0) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

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

0 comments on commit dcbc70c

Please sign in to comment.