Skip to content

Commit 6dc5b83

Browse files
committed
API getting there
1 parent cfdea24 commit 6dc5b83

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

packages/validation/src/se.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ export function validateOrganizationNumber(
8484
return /^\d{10}$/.test(value);
8585
}
8686

87+
type NationalIdentityNumberFormat = 'short' | 'long';
8788
type NationalIdenityNumberOptions = ValidatorOptions & {
88-
format?: ['long', 'short'];
89+
/** By default, both formats are allowed */
90+
format?: NationalIdentityNumberFormat;
8991
};
9092

9193
/**
@@ -111,25 +113,25 @@ export function validateNationalIdentityNumber(
111113
value = stripFormatting(value);
112114
}
113115

114-
// this allows us to handle both YYYYMMDD and YYMMDD when extracting the date
115-
const offset = value.length === 12 ? 2 : 0;
116+
const isLongFormat = value.length === 12;
116117

117118
// when verifying the value, we must always use the short format.
118119
// because the long format would generate a different checksum
119-
const isValid = mod10(offset ? value.substring(2) : value);
120+
const isValid = mod10(isLongFormat ? value.substring(2) : value);
120121
if (!isValid) {
121122
return false;
122123
}
123124

125+
// this allows us to handle both YYYYMMDD and YYMMDD when extracting the date
126+
const offset = isLongFormat ? 2 : 0;
124127
// copy/inspiration from NAV https://github.com/navikt/fnrvalidator/blob/77e57f0bc8e3570ddc2f0a94558c58d0f7259fe0/src/validator.ts#L108
125128
let year = Number(value.substring(0, 2 + offset));
126129
const month = Number(value.substring(2 + offset, 4 + offset));
127130
let day = Number(value.substring(4 + offset, 6 + offset));
128131

129132
// 1900 isn't a leap year, but 2000 is. Since JS two digits years to the Date constructor is an offset from the year 1900
130133
// we need to special handle that case. For other cases it doesn't really matter if the year is 1925 or 2025.
131-
// if (options.format === 'short' && year === 0) {
132-
if (value.length === 10 && year === 0) {
134+
if (!isLongFormat && year === 0) {
133135
year = 2000;
134136
}
135137

packages/validation/src/validation.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,22 @@ describe('se', () => {
194194
for (let i = 0; i < 1000; ++i) {
195195
const pnr = swedishPersonNummer({ format: 'short' });
196196
expect(
197-
se.validateNationalIdentityNumber(pnr, { allowFormatting: true }),
197+
se.validateNationalIdentityNumber(pnr, {
198+
allowFormatting: true,
199+
format: 'short',
200+
}),
198201
`${pnr} is valid`,
199202
).toBe(true);
200203
}
201204
});
202205

203-
// 204101052241
204-
// 211802018075
205-
// 196304076083
206-
test.only('validateNationalIdentityNumber() - validates long format personnummer', () => {
206+
test('validateNationalIdentityNumber() - validates long format personnummer', () => {
207207
for (let i = 0; i < 1000; ++i) {
208208
const pnr = swedishPersonNummer({ format: 'long' });
209-
expect(se.validateNationalIdentityNumber(pnr), `${pnr} is valid`).toBe(
210-
true,
211-
);
209+
expect(
210+
se.validateNationalIdentityNumber(pnr, { format: 'long' }),
211+
`${pnr} is valid`,
212+
).toBe(true);
212213
}
213214
});
214215
});

0 commit comments

Comments
 (0)