Skip to content

Commit

Permalink
Merge pull request #61 from koblas/al_nipt_fix
Browse files Browse the repository at this point in the history
fix: al/nipt validation improvements
  • Loading branch information
koblas committed Jun 30, 2023
2 parents f5177f8 + e0344f4 commit 339a3d5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/al/nipt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ describe('al/nibt', () => {
expect(result).toEqual('J91402501L');
});

test.each(['I05101999Q', 'K52224002A', 'L42307014K'])(
'validate:%s',
value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
},
);

it('validate:AL J 91402501 L', () => {
const result = validate('AL J 91402501 L');

Expand Down
31 changes: 29 additions & 2 deletions src/al/nipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as exceptions from '../exceptions';
import { ValidateReturn, Validator } from '../types';
import { strings } from '../util';
import { strings, isValidDate } from '../util';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
// eslint-disable-next-line prefer-const
Expand Down Expand Up @@ -68,9 +68,36 @@ const impl: Validator = {
if (value.length !== 10) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!/^[JKL]\d{8}[A-Z]$/.test(value)) {
if (!/^[A-M]\d{8}[A-Z]$/.test(value)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
const [ccode, ydigit, month_district, day, _, check] = strings.splitAt(
value,
1,
2,
4,
6,
9,
);
const month = ((parseInt(month_district, 10) - 1) % 12) + 1;
const yearVal = ccode.charCodeAt(0) - 65;
const year = 1900 + yearVal * 10 + parseInt(ydigit, 10);
if (!isValidDate(String(year), String(month), day)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}
if (!/^[A-Z]$/.test(check)) {
return { isValid: false, error: new exceptions.InvalidComponent() };
}

// TODO: check calculation is not understood
// const sumValue = `${yearVal}${value.substring(1, 9)}`;
// const checkValue = check.charCodeAt(0) - 65;

// const sum = weightedSum(sumValue, { weights: [1], modulus: 26 });
// console.log(value, sumValue, sum, checkValue);
// if (sum !== checkValue) {
// return { isValid: false, error: new exceptions.InvalidChecksum() };
// }

return {
isValid: true,
Expand Down

0 comments on commit 339a3d5

Please sign in to comment.