diff --git a/src/fr/siret.spec.ts b/src/fr/siret.spec.ts index a8f90bb..3bf249e 100644 --- a/src/fr/siret.spec.ts +++ b/src/fr/siret.spec.ts @@ -8,11 +8,21 @@ describe('fr/siret', () => { expect(result).toEqual('732 829 320 00074'); }); - it('validate:73282932000074', () => { - const result = validate('73282932000074'); - - expect(result.isValid && result.compact).toEqual('73282932000074'); + test.each([ + '73282932000074', + '356 000 000 09075', + '35600000000048', + '35600000049837', + ])('validate:%s', value => { + const result = validate(value); + + expect(result.isValid).toEqual(true); }); + // it('validate:73282932000074', () => { + // const result = validate('73282932000074'); + + // expect(result.isValid && result.compact).toEqual('73282932000074'); + // }); it('validate:12345678', () => { const result = validate('12345678'); @@ -20,8 +30,8 @@ describe('fr/siret', () => { expect(result.error).toBeInstanceOf(InvalidLength); }); - it('validate:73282932000075', () => { - const result = validate('73282932000075'); + test.each(['35600000049838', '73282932000075'])('validate:%s', value => { + const result = validate(value); expect(result.error).toBeInstanceOf(InvalidChecksum); }); diff --git a/src/fr/siret.ts b/src/fr/siret.ts index 5d0c085..8cf3e16 100644 --- a/src/fr/siret.ts +++ b/src/fr/siret.ts @@ -3,7 +3,11 @@ * * The SIRET (Système d'Identification du Répertoire des Établissements) * is a 14 digit number used to identify French companies' establishments - * and facilities. The Luhn checksum is used to validate the numbers. + * and facilities. The Luhn checksum is used to validate the numbers (except + * for La Poste). + * + * Sources: + * https://fr.wikipedia.org/wiki/Système_d'identification_du_répertoire_des_établissements * * ENTITY */ @@ -11,7 +15,7 @@ import * as exceptions from '../exceptions'; import { strings } from '../util'; import { Validator, ValidateReturn } from '../types'; -import { luhnChecksumValidate } from '../util/checksum'; +import { luhnChecksumValidate, weightedSum } from '../util/checksum'; function clean(input: string): ReturnType { return strings.cleanUnicode(input, ' -/'); @@ -50,7 +54,16 @@ const impl: Validator = { return { isValid: false, error: new exceptions.InvalidFormat() }; } - if (!luhnChecksumValidate(value)) { + if (value.startsWith('356000000') && value !== '35600000000048') { + const sum = weightedSum(value, { + weights: [1], + modulus: 5, + }); + console.log('HERE', value, sum); + if (sum !== 0) { + return { isValid: false, error: new exceptions.InvalidChecksum() }; + } + } else if (!luhnChecksumValidate(value)) { return { isValid: false, error: new exceptions.InvalidChecksum() }; }