asma-core-helpers 0.24.0 → 0.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/helpers/generateSyntheticNorwegianNumber.js +4 -4
- package/lib/helpers/generateSyntheticNorwegianNumber.js.map +1 -1
- package/lib/validation/index.d.ts +9 -0
- package/lib/validation/index.d.ts.map +1 -0
- package/lib/validation/index.js +9 -0
- package/lib/validation/index.js.map +1 -0
- package/lib/validation/messages.d.ts +43 -0
- package/lib/validation/messages.d.ts.map +1 -0
- package/lib/validation/messages.js +44 -0
- package/lib/validation/messages.js.map +1 -0
- package/lib/validation/rules.d.ts +11 -0
- package/lib/validation/rules.d.ts.map +1 -0
- package/lib/validation/rules.js +28 -0
- package/lib/validation/rules.js.map +1 -0
- package/package.json +5 -1
- package/src/helpers/generateSyntheticNorwegianNumber.test.ts +67 -0
- package/src/helpers/generateSyntheticNorwegianNumber.ts +4 -4
- package/src/validation/index.ts +17 -0
- package/src/validation/messages.test.ts +41 -0
- package/src/validation/messages.ts +66 -0
- package/src/validation/rules.test.ts +72 -0
- package/src/validation/rules.ts +32 -0
|
@@ -10,12 +10,12 @@ function generateSyntheticPnr({ dateOfBirth, gender, factor = 80, }) {
|
|
|
10
10
|
else {
|
|
11
11
|
individualNumber = 1;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
|
-
//
|
|
15
|
-
|
|
13
|
+
// The individual number encodes the birth century: 000–499 → 1900–1999, 500–999 → 2000–2039.
|
|
14
|
+
// A mismatched range makes BankID RA resolve the birth year into the 1800s and reject the number (ASMA-7830).
|
|
15
|
+
const centuryBase = dateOfBirth.getFullYear() >= 2000 ? 500 : 0;
|
|
16
|
+
individualNumber = centuryBase + Math.floor(Math.random() * 250) * 2 + individualNumber; // Ensures gender parity
|
|
16
17
|
const individualNumberString = individualNumber.toString().padStart(3, '0');
|
|
17
18
|
const basicNumber = `${day}${month}${year}${individualNumberString}`;
|
|
18
|
-
console.log({ individualNumber, basicNumber, individualNumberString });
|
|
19
19
|
// Calculate control numbers
|
|
20
20
|
const control1 = (11 -
|
|
21
21
|
((3 * parseInt(basicNumber[0]) +
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateSyntheticNorwegianNumber.js","sourceRoot":"","sources":["../../src/helpers/generateSyntheticNorwegianNumber.ts"],"names":[],"mappings":"AAEA,SAAS,oBAAoB,CAAC,EAC1B,WAAW,EACX,MAAM,EACN,MAAM,GAAG,EAAE,GAKd;IACG,MAAM,GAAG,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IAC7D,MAAM,KAAK,GAAG,CAAC,WAAW,CAAC,QAAQ,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA,CAAC,sBAAsB;IAEtG,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,EAAE,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IAE3D,8BAA8B;IAC9B,IAAI,gBAAwB,CAAA;IAC5B,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;QACtB,gBAAgB,GAAG,CAAC,CAAA;IACxB,CAAC;SAAM,CAAC;QACJ,gBAAgB,GAAG,CAAC,CAAA;IACxB,CAAC;IACD,
|
|
1
|
+
{"version":3,"file":"generateSyntheticNorwegianNumber.js","sourceRoot":"","sources":["../../src/helpers/generateSyntheticNorwegianNumber.ts"],"names":[],"mappings":"AAEA,SAAS,oBAAoB,CAAC,EAC1B,WAAW,EACX,MAAM,EACN,MAAM,GAAG,EAAE,GAKd;IACG,MAAM,GAAG,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAA;IAC7D,MAAM,KAAK,GAAG,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,EAAE,CAAA;IAC3D,IAAI,YAAY,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAC7B,OAAO,SAAS,CAAA;IACpB,CAAC;IACD,OAAO,YAAY,CAAA;AACvB,CAAC;AACD;;;;;;GAMG;AACH,MAAM,UAAU,+BAA+B,CAAC,IAI/C;IACG,OAAO,IAAI,EAAE,CAAC;QACV,MAAM,GAAG,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAA;QAEtC,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACpB,OAAO,GAAG,CAAA;QACd,CAAC;IACL,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure validation messages + rule primitives for ASMA forms.
|
|
3
|
+
*
|
|
4
|
+
* Published as `asma-core-helpers/validation` (not the root barrel) so Node/Bun
|
|
5
|
+
* consumers never pull browser-only helpers that read `window` on import.
|
|
6
|
+
*/
|
|
7
|
+
export { errorWithHint, getValidationMessages, validationMessages, type ValidationLocale, type ValidationMessageKey, type ValidationMessages, } from './messages.js';
|
|
8
|
+
export { email, emailRegex, name, nameRegex, pattern, phoneNr, phoneNrRegex, pnr, required } from './rules.js';
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACH,aAAa,EACb,qBAAqB,EACrB,kBAAkB,EAClB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,kBAAkB,GAC1B,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure validation messages + rule primitives for ASMA forms.
|
|
3
|
+
*
|
|
4
|
+
* Published as `asma-core-helpers/validation` (not the root barrel) so Node/Bun
|
|
5
|
+
* consumers never pull browser-only helpers that read `window` on import.
|
|
6
|
+
*/
|
|
7
|
+
export { errorWithHint, getValidationMessages, validationMessages, } from './messages.js';
|
|
8
|
+
export { email, emailRegex, name, nameRegex, pattern, phoneNr, phoneNrRegex, pnr, required } from './rules.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/validation/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EACH,aAAa,EACb,qBAAqB,EACrB,kBAAkB,GAIrB,MAAM,eAAe,CAAA;AAEtB,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default NB/EN validation catalog for ASMA forms (ASMA-7729).
|
|
3
|
+
* Apps override via shallow merge — translations stay in app i18n.
|
|
4
|
+
*/
|
|
5
|
+
export type ValidationLocale = 'nb' | 'en';
|
|
6
|
+
export type ValidationMessageKey = 'required' | 'invalid_email' | 'invalid_name' | 'invalid_phone' | 'invalid_personal_number' | 'duplicate_email' | 'duplicate_journal_id' | 'duplicate_personal_number' | 'duplicate_employee_no' | 'duplicate_phone' | 'duplicate_username';
|
|
7
|
+
export type ValidationMessages = Record<ValidationMessageKey, string>;
|
|
8
|
+
export declare const validationMessages: {
|
|
9
|
+
readonly nb: {
|
|
10
|
+
readonly required: 'Dette feltet er obligatorisk';
|
|
11
|
+
readonly invalid_email: 'Ugyldig epost';
|
|
12
|
+
readonly invalid_name: 'Ugyldig navn';
|
|
13
|
+
readonly invalid_phone: 'Ugyldig telefon';
|
|
14
|
+
readonly invalid_personal_number: 'Ugyldig personnummer';
|
|
15
|
+
readonly duplicate_email: 'Duplikat epost';
|
|
16
|
+
readonly duplicate_journal_id: 'Duplikat journal id';
|
|
17
|
+
readonly duplicate_personal_number: 'Duplikat personnummer';
|
|
18
|
+
readonly duplicate_employee_no: 'Duplikat Ansattnr.';
|
|
19
|
+
readonly duplicate_phone: 'Duplikat telefon';
|
|
20
|
+
readonly duplicate_username: 'Duplikat brukernavn';
|
|
21
|
+
};
|
|
22
|
+
readonly en: {
|
|
23
|
+
readonly required: 'This field is required';
|
|
24
|
+
readonly invalid_email: 'Invalid email';
|
|
25
|
+
readonly invalid_name: 'Invalid name';
|
|
26
|
+
readonly invalid_phone: 'Invalid phone';
|
|
27
|
+
readonly invalid_personal_number: 'Invalid personal number';
|
|
28
|
+
readonly duplicate_email: 'Duplicate email';
|
|
29
|
+
readonly duplicate_journal_id: 'Duplicate journal id';
|
|
30
|
+
readonly duplicate_personal_number: 'Duplicate personal number';
|
|
31
|
+
readonly duplicate_employee_no: 'Duplicate employee no.';
|
|
32
|
+
readonly duplicate_phone: 'Duplicate phone';
|
|
33
|
+
readonly duplicate_username: 'Duplicate username';
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
/** Shallow-merge overrides over the locale catalog (per-app i18n). */
|
|
37
|
+
export declare function getValidationMessages(locale: ValidationLocale, overrides?: Partial<ValidationMessages>): ValidationMessages;
|
|
38
|
+
/**
|
|
39
|
+
* REQ-003: actionable "what's wrong — how to fix" composition.
|
|
40
|
+
* e.g. errorWithHint('Invalid email', 'name@carasent.com') → 'Invalid email — name@carasent.com'
|
|
41
|
+
*/
|
|
42
|
+
export declare function errorWithHint(error: string, hint: string): string;
|
|
43
|
+
//# sourceMappingURL=messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/validation/messages.ts"],"names":[],"mappings":"AAAA;;;GA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AEjE"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default NB/EN validation catalog for ASMA forms (ASMA-7729).
|
|
3
|
+
* Apps override via shallow merge — translations stay in app i18n.
|
|
4
|
+
*/
|
|
5
|
+
export const validationMessages = {
|
|
6
|
+
nb: {
|
|
7
|
+
required: 'Dette feltet er obligatorisk',
|
|
8
|
+
invalid_email: 'Ugyldig epost',
|
|
9
|
+
invalid_name: 'Ugyldig navn',
|
|
10
|
+
invalid_phone: 'Ugyldig telefon',
|
|
11
|
+
invalid_personal_number: 'Ugyldig personnummer',
|
|
12
|
+
duplicate_email: 'Duplikat epost',
|
|
13
|
+
duplicate_journal_id: 'Duplikat journal id',
|
|
14
|
+
duplicate_personal_number: 'Duplikat personnummer',
|
|
15
|
+
duplicate_employee_no: 'Duplikat Ansattnr.',
|
|
16
|
+
duplicate_phone: 'Duplikat telefon',
|
|
17
|
+
duplicate_username: 'Duplikat brukernavn',
|
|
18
|
+
},
|
|
19
|
+
en: {
|
|
20
|
+
required: 'This field is required',
|
|
21
|
+
invalid_email: 'Invalid email',
|
|
22
|
+
invalid_name: 'Invalid name',
|
|
23
|
+
invalid_phone: 'Invalid phone',
|
|
24
|
+
invalid_personal_number: 'Invalid personal number',
|
|
25
|
+
duplicate_email: 'Duplicate email',
|
|
26
|
+
duplicate_journal_id: 'Duplicate journal id',
|
|
27
|
+
duplicate_personal_number: 'Duplicate personal number',
|
|
28
|
+
duplicate_employee_no: 'Duplicate employee no.',
|
|
29
|
+
duplicate_phone: 'Duplicate phone',
|
|
30
|
+
duplicate_username: 'Duplicate username',
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
/** Shallow-merge overrides over the locale catalog (per-app i18n). */
|
|
34
|
+
export function getValidationMessages(locale, overrides) {
|
|
35
|
+
return { ...validationMessages[locale], ...overrides };
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* REQ-003: actionable "what's wrong — how to fix" composition.
|
|
39
|
+
* e.g. errorWithHint('Invalid email', 'name@carasent.com') → 'Invalid email — name@carasent.com'
|
|
40
|
+
*/
|
|
41
|
+
export function errorWithHint(error, hint) {
|
|
42
|
+
return `${error} — ${hint}`;
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/validation/messages.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAmBH,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAC9B,EAAE,EAAE;QACA,QAAQ,EAAE,8BAA8B;QACxC,aAAa,EAAE,eAAe;QAC9B,YAAY,EAAE,cAAc;QAC5B,aAAa,EAAE,iBAAiB;QAChC,uBAAuB,EAAE,sBAAsB;QAC/C,eAAe,EAAE,gBAAgB;QACjC,oBAAoB,EAAE,qBAAqB;QAC3C,yBAAyB,EAAE,uBAAuB;QAClD,qBAAqB,EAAE,oBAAoB;QAC3C,eAAe,EAAE,kBAAkB;QACnC,kBAAkB,EAAE,qBAAqB;KAC5C;IACD,EAAE,EAAE;QACA,QAAQ,EAAE,wBAAwB;QAClC,aAAa,EAAE,eAAe;QAC9B,YAAY,EAAE,cAAc;QAC5B,aAAa,EAAE,eAAe;QAC9B,uBAAuB,EAAE,yBAAyB;QAClD,eAAe,EAAE,iBAAiB;QAClC,oBAAoB,EAAE,sBAAsB;QAC5C,yBAAyB,EAAE,2BAA2B;QACtD,qBAAqB,EAAE,wBAAwB;QAC/C,eAAe,EAAE,iBAAiB;QAClC,kBAAkB,EAAE,oBAAoB;KAC3C;CAC4D,CAAA;AAEjE,sEAAsE;AACtE,MAAM,UAAU,qBAAqB,CACjC,MAAwB,EACxB,SAAuC;IAEvC,OAAO,EAAE,GAAG,kBAAkB,CAAC,MAAM,CAAC,EAAE,GAAG,SAAS,EAAE,CAAA;AAC1D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,KAAa,EAAE,IAAY;IACrD,OAAO,GAAG,KAAK,MAAM,IAAI,EAAE,CAAA;AAC/B,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Copied from directory/asma-app-directory ActorToUpsertValidator regex helpers — keep pilot behavior identical. */
|
|
2
|
+
export declare const phoneNrRegex: RegExp;
|
|
3
|
+
export declare const emailRegex: RegExp;
|
|
4
|
+
export declare const nameRegex: RegExp;
|
|
5
|
+
export declare function required(value: unknown): boolean;
|
|
6
|
+
export declare function email(value: string): boolean;
|
|
7
|
+
export declare function name(value: string): boolean;
|
|
8
|
+
export declare function phoneNr(value: string): boolean;
|
|
9
|
+
export declare function pattern(value: string, regex: RegExp): boolean;
|
|
10
|
+
export declare function pnr(value: string): boolean;
|
|
11
|
+
//# sourceMappingURL=rules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.d.ts","sourceRoot":"","sources":["../../src/validation/rules.ts"],"names":[],"mappings":"AAEA,qHAAqH;AACrH,eAAO,MAAM,YAAY,QAAkB,CAAA;AAC3C,eAAO,MAAM,UAAU,QAAoC,CAAA;AAC3D,eAAO,MAAM,SAAS,QAAuD,CAAA;AAE7E,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAIhD;AAED,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE5C;AAED,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE9C;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAE7D;AAED,wBAAgB,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE1C"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getValidNorwegianPersonalNumber } from '../helpers/validateNorwegianPersonalNumber.js';
|
|
2
|
+
/** Copied from directory/asma-app-directory ActorToUpsertValidator regex helpers — keep pilot behavior identical. */
|
|
3
|
+
export const phoneNrRegex = /^\+?\d{6,13}$/;
|
|
4
|
+
export const emailRegex = /^[\w.-]+@([\w-]+\.)+[\w-]{2,4}$/;
|
|
5
|
+
export const nameRegex = /^\s*\p{Lu}[\p{L}0-9-]*(\s+\p{L}[\p{L}0-9-]*)*\s*$/u;
|
|
6
|
+
export function required(value) {
|
|
7
|
+
if (value == null)
|
|
8
|
+
return false;
|
|
9
|
+
if (typeof value === 'string')
|
|
10
|
+
return value.trim().length > 0;
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
export function email(value) {
|
|
14
|
+
return emailRegex.test(value);
|
|
15
|
+
}
|
|
16
|
+
export function name(value) {
|
|
17
|
+
return nameRegex.test(value);
|
|
18
|
+
}
|
|
19
|
+
export function phoneNr(value) {
|
|
20
|
+
return phoneNrRegex.test(value);
|
|
21
|
+
}
|
|
22
|
+
export function pattern(value, regex) {
|
|
23
|
+
return regex.test(value);
|
|
24
|
+
}
|
|
25
|
+
export function pnr(value) {
|
|
26
|
+
return getValidNorwegianPersonalNumber(value) != null;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=rules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rules.js","sourceRoot":"","sources":["../../src/validation/rules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,+BAA+B,EAAE,MAAM,+CAA+C,CAAA;AAE/F,qHAAqH;AACrH,MAAM,CAAC,MAAM,YAAY,GAAG,eAAe,CAAA;AAC3C,MAAM,CAAC,MAAM,UAAU,GAAG,iCAAiC,CAAA;AAC3D,MAAM,CAAC,MAAM,SAAS,GAAG,oDAAoD,CAAA;AAE7E,MAAM,UAAU,QAAQ,CAAC,KAAc;IACnC,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,KAAK,CAAA;IAC/B,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAA;IAC7D,OAAO,IAAI,CAAA;AACf,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,KAAa;IAC/B,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACjC,CAAC;AAED,MAAM,UAAU,IAAI,CAAC,KAAa;IAC9B,OAAO,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AAChC,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,KAAa;IACjC,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AACnC,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,KAAa,EAAE,KAAa;IAChD,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC;AAED,MAAM,UAAU,GAAG,CAAC,KAAa;IAC7B,OAAO,+BAA+B,CAAC,KAAK,CAAC,IAAI,IAAI,CAAA;AACzD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "asma-core-helpers",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "Core helper utilities for ASMA applications",
|
|
5
5
|
"files": [
|
|
6
6
|
"lib/**/*",
|
|
@@ -26,6 +26,10 @@
|
|
|
26
26
|
"./html-text": {
|
|
27
27
|
"types": "./lib/helpers/htmlText.d.ts",
|
|
28
28
|
"default": "./lib/helpers/htmlText.js"
|
|
29
|
+
},
|
|
30
|
+
"./validation": {
|
|
31
|
+
"types": "./lib/validation/index.d.ts",
|
|
32
|
+
"default": "./lib/validation/index.js"
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
"scripts": {
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { generateSyntheticPnrWithRetries } from './generateSyntheticNorwegianNumber.js'
|
|
5
|
+
|
|
6
|
+
const K1_WEIGHTS = [3, 7, 6, 1, 8, 9, 4, 5, 2]
|
|
7
|
+
const K2_WEIGHTS = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
|
|
8
|
+
|
|
9
|
+
function hasValidControlDigits(pnr: string): boolean {
|
|
10
|
+
const control = (weights: number[]) => {
|
|
11
|
+
const sum = weights.reduce((acc, weight, idx) => acc + weight * Number(pnr.charAt(idx)), 0)
|
|
12
|
+
const remainder = sum % 11
|
|
13
|
+
return remainder === 0 ? 0 : 11 - remainder
|
|
14
|
+
}
|
|
15
|
+
return control(K1_WEIGHTS) === Number(pnr.charAt(9)) && control(K2_WEIGHTS) === Number(pnr.charAt(10))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function individualNumberOf(pnr: string): number {
|
|
19
|
+
return Number(pnr.slice(6, 9))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const RUNS = 50
|
|
23
|
+
|
|
24
|
+
describe('generateSyntheticPnrWithRetries', () => {
|
|
25
|
+
it('uses individual numbers 000–499 for births in 1900–1999 (BankID RA century rule)', () => {
|
|
26
|
+
for (let i = 0; i < RUNS; i++) {
|
|
27
|
+
const pnr = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(1998, 7, 12), gender: 'MALE' })
|
|
28
|
+
assert.ok(
|
|
29
|
+
individualNumberOf(pnr) <= 499,
|
|
30
|
+
`individual number ${individualNumberOf(pnr)} of ${pnr} encodes the wrong century for a 1998 birth`,
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it('uses individual numbers 500–999 for births in 2000–2039', () => {
|
|
36
|
+
for (let i = 0; i < RUNS; i++) {
|
|
37
|
+
const pnr = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(2005, 2, 7), gender: 'FEMALE' })
|
|
38
|
+
const individualNumber = individualNumberOf(pnr)
|
|
39
|
+
assert.ok(
|
|
40
|
+
individualNumber >= 500 && individualNumber <= 999,
|
|
41
|
+
`individual number ${individualNumber} of ${pnr} encodes the wrong century for a 2005 birth`,
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('encodes gender in the individual number parity (even = female, odd = male)', () => {
|
|
47
|
+
for (let i = 0; i < RUNS; i++) {
|
|
48
|
+
const female = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(1992, 7, 11), gender: 'FEMALE' })
|
|
49
|
+
const male = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(1992, 7, 11), gender: 'MALE' })
|
|
50
|
+
assert.equal(individualNumberOf(female) % 2, 0, `female pnr ${female} has an odd individual number`)
|
|
51
|
+
assert.equal(individualNumberOf(male) % 2, 1, `male pnr ${male} has an even individual number`)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('marks the number as synthetic by adding 80 to the month by default', () => {
|
|
56
|
+
const pnr = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(1998, 7, 12), gender: 'MALE' })
|
|
57
|
+
assert.equal(pnr.slice(0, 6), '128898')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('produces 11 digits with valid mod-11 control digits', () => {
|
|
61
|
+
for (let i = 0; i < RUNS; i++) {
|
|
62
|
+
const pnr = generateSyntheticPnrWithRetries({ dateOfBirth: new Date(1965, 1, 8), gender: 'MALE' })
|
|
63
|
+
assert.match(pnr, /^\d{11}$/)
|
|
64
|
+
assert.ok(hasValidControlDigits(pnr), `control digits of ${pnr} are invalid`)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
})
|
|
@@ -21,13 +21,13 @@ function generateSyntheticPnr({
|
|
|
21
21
|
} else {
|
|
22
22
|
individualNumber = 1
|
|
23
23
|
}
|
|
24
|
-
|
|
25
|
-
//
|
|
26
|
-
|
|
24
|
+
// The individual number encodes the birth century: 000–499 → 1900–1999, 500–999 → 2000–2039.
|
|
25
|
+
// A mismatched range makes BankID RA resolve the birth year into the 1800s and reject the number (ASMA-7830).
|
|
26
|
+
const centuryBase = dateOfBirth.getFullYear() >= 2000 ? 500 : 0
|
|
27
|
+
individualNumber = centuryBase + Math.floor(Math.random() * 250) * 2 + individualNumber // Ensures gender parity
|
|
27
28
|
|
|
28
29
|
const individualNumberString = individualNumber.toString().padStart(3, '0')
|
|
29
30
|
const basicNumber = `${day}${month}${year}${individualNumberString}`
|
|
30
|
-
console.log({ individualNumber, basicNumber, individualNumberString })
|
|
31
31
|
// Calculate control numbers
|
|
32
32
|
const control1 =
|
|
33
33
|
(11 -
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure validation messages + rule primitives for ASMA forms.
|
|
3
|
+
*
|
|
4
|
+
* Published as `asma-core-helpers/validation` (not the root barrel) so Node/Bun
|
|
5
|
+
* consumers never pull browser-only helpers that read `window` on import.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
errorWithHint,
|
|
10
|
+
getValidationMessages,
|
|
11
|
+
validationMessages,
|
|
12
|
+
type ValidationLocale,
|
|
13
|
+
type ValidationMessageKey,
|
|
14
|
+
type ValidationMessages,
|
|
15
|
+
} from './messages.js'
|
|
16
|
+
|
|
17
|
+
export { email, emailRegex, name, nameRegex, pattern, phoneNr, phoneNrRegex, pnr, required } from './rules.js'
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { errorWithHint, getValidationMessages, validationMessages } from './messages.js'
|
|
5
|
+
|
|
6
|
+
describe('validationMessages catalog', () => {
|
|
7
|
+
it('exposes distinct NB and EN strings for every key', () => {
|
|
8
|
+
const keys = Object.keys(validationMessages.en) as (keyof typeof validationMessages.en)[]
|
|
9
|
+
assert.ok(keys.length > 0)
|
|
10
|
+
for (const key of keys) {
|
|
11
|
+
assert.notEqual(
|
|
12
|
+
validationMessages.nb[key],
|
|
13
|
+
validationMessages.en[key],
|
|
14
|
+
`${key} must differ between nb and en`,
|
|
15
|
+
)
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('MUT-004: wrong language must fail — nb required is not the en string', () => {
|
|
20
|
+
assert.notEqual(validationMessages.nb.required, validationMessages.en.required)
|
|
21
|
+
assert.equal(getValidationMessages('nb').required, 'Dette feltet er obligatorisk')
|
|
22
|
+
assert.equal(getValidationMessages('en').required, 'This field is required')
|
|
23
|
+
assert.notEqual(getValidationMessages('nb').invalid_email, getValidationMessages('en').invalid_email)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('getValidationMessages applies shallow overrides', () => {
|
|
27
|
+
const msgs = getValidationMessages('en', { invalid_email: 'App override' })
|
|
28
|
+
assert.equal(msgs.invalid_email, 'App override')
|
|
29
|
+
assert.equal(msgs.required, validationMessages.en.required)
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('errorWithHint', () => {
|
|
34
|
+
it('composes actionable "what — how to fix" (REQ-003)', () => {
|
|
35
|
+
assert.equal(errorWithHint('Invalid email', 'name@carasent.com'), 'Invalid email — name@carasent.com')
|
|
36
|
+
assert.equal(
|
|
37
|
+
errorWithHint(validationMessages.nb.invalid_email, 'name@carasent.com'),
|
|
38
|
+
'Ugyldig epost — name@carasent.com',
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default NB/EN validation catalog for ASMA forms (ASMA-7729).
|
|
3
|
+
* Apps override via shallow merge — translations stay in app i18n.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type ValidationLocale = 'nb' | 'en'
|
|
7
|
+
|
|
8
|
+
export type ValidationMessageKey =
|
|
9
|
+
| 'required'
|
|
10
|
+
| 'invalid_email'
|
|
11
|
+
| 'invalid_name'
|
|
12
|
+
| 'invalid_phone'
|
|
13
|
+
| 'invalid_personal_number'
|
|
14
|
+
| 'duplicate_email'
|
|
15
|
+
| 'duplicate_journal_id'
|
|
16
|
+
| 'duplicate_personal_number'
|
|
17
|
+
| 'duplicate_employee_no'
|
|
18
|
+
| 'duplicate_phone'
|
|
19
|
+
| 'duplicate_username'
|
|
20
|
+
|
|
21
|
+
export type ValidationMessages = Record<ValidationMessageKey, string>
|
|
22
|
+
|
|
23
|
+
export const validationMessages = {
|
|
24
|
+
nb: {
|
|
25
|
+
required: 'Dette feltet er obligatorisk',
|
|
26
|
+
invalid_email: 'Ugyldig epost',
|
|
27
|
+
invalid_name: 'Ugyldig navn',
|
|
28
|
+
invalid_phone: 'Ugyldig telefon',
|
|
29
|
+
invalid_personal_number: 'Ugyldig personnummer',
|
|
30
|
+
duplicate_email: 'Duplikat epost',
|
|
31
|
+
duplicate_journal_id: 'Duplikat journal id',
|
|
32
|
+
duplicate_personal_number: 'Duplikat personnummer',
|
|
33
|
+
duplicate_employee_no: 'Duplikat Ansattnr.',
|
|
34
|
+
duplicate_phone: 'Duplikat telefon',
|
|
35
|
+
duplicate_username: 'Duplikat brukernavn',
|
|
36
|
+
},
|
|
37
|
+
en: {
|
|
38
|
+
required: 'This field is required',
|
|
39
|
+
invalid_email: 'Invalid email',
|
|
40
|
+
invalid_name: 'Invalid name',
|
|
41
|
+
invalid_phone: 'Invalid phone',
|
|
42
|
+
invalid_personal_number: 'Invalid personal number',
|
|
43
|
+
duplicate_email: 'Duplicate email',
|
|
44
|
+
duplicate_journal_id: 'Duplicate journal id',
|
|
45
|
+
duplicate_personal_number: 'Duplicate personal number',
|
|
46
|
+
duplicate_employee_no: 'Duplicate employee no.',
|
|
47
|
+
duplicate_phone: 'Duplicate phone',
|
|
48
|
+
duplicate_username: 'Duplicate username',
|
|
49
|
+
},
|
|
50
|
+
} as const satisfies Record<ValidationLocale, ValidationMessages>
|
|
51
|
+
|
|
52
|
+
/** Shallow-merge overrides over the locale catalog (per-app i18n). */
|
|
53
|
+
export function getValidationMessages(
|
|
54
|
+
locale: ValidationLocale,
|
|
55
|
+
overrides?: Partial<ValidationMessages>,
|
|
56
|
+
): ValidationMessages {
|
|
57
|
+
return { ...validationMessages[locale], ...overrides }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* REQ-003: actionable "what's wrong — how to fix" composition.
|
|
62
|
+
* e.g. errorWithHint('Invalid email', 'name@carasent.com') → 'Invalid email — name@carasent.com'
|
|
63
|
+
*/
|
|
64
|
+
export function errorWithHint(error: string, hint: string): string {
|
|
65
|
+
return `${error} — ${hint}`
|
|
66
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
|
|
4
|
+
import { email, name, pattern, phoneNr, pnr, required } from './rules.js'
|
|
5
|
+
|
|
6
|
+
describe('required', () => {
|
|
7
|
+
it('rejects empty, whitespace, null, undefined', () => {
|
|
8
|
+
assert.equal(required(''), false)
|
|
9
|
+
assert.equal(required(' '), false)
|
|
10
|
+
assert.equal(required(null), false)
|
|
11
|
+
assert.equal(required(undefined), false)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('accepts non-empty strings and other values', () => {
|
|
15
|
+
assert.equal(required('x'), true)
|
|
16
|
+
assert.equal(required(0), true)
|
|
17
|
+
assert.equal(required(false), true)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('email', () => {
|
|
22
|
+
it('MUT-004: email without @ must fail', () => {
|
|
23
|
+
assert.equal(email('notanemail'), false)
|
|
24
|
+
assert.equal(email('user.domain.com'), false)
|
|
25
|
+
assert.equal(email('user@'), false)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('accepts a normal address', () => {
|
|
29
|
+
assert.equal(email('name@carasent.com'), true)
|
|
30
|
+
assert.equal(email('a.b-c@example.co.uk'), true)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('name', () => {
|
|
35
|
+
it('requires a Unicode uppercase first letter', () => {
|
|
36
|
+
assert.equal(name('John'), true)
|
|
37
|
+
assert.equal(name('John Doe'), true)
|
|
38
|
+
assert.equal(name('john'), false)
|
|
39
|
+
assert.equal(name(''), false)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
describe('phoneNr', () => {
|
|
44
|
+
it('matches the directory pilot regex', () => {
|
|
45
|
+
assert.equal(phoneNr('+47123456'), true)
|
|
46
|
+
assert.equal(phoneNr('123456'), true)
|
|
47
|
+
assert.equal(phoneNr('12345'), false)
|
|
48
|
+
assert.equal(phoneNr('abc'), false)
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
describe('pattern', () => {
|
|
53
|
+
it('delegates to the given regex', () => {
|
|
54
|
+
assert.equal(pattern('abc', /^[a-z]+$/), true)
|
|
55
|
+
assert.equal(pattern('123', /^[a-z]+$/), false)
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
describe('pnr', () => {
|
|
60
|
+
// Temporary FNR: valid date + 11111 suffix (no modulus check).
|
|
61
|
+
const validTemporary = '01019011111'
|
|
62
|
+
|
|
63
|
+
it('accepts a valid Norwegian personal number via getValidNorwegianPersonalNumber', () => {
|
|
64
|
+
assert.equal(pnr(validTemporary), true)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('rejects invalid values', () => {
|
|
68
|
+
assert.equal(pnr(''), false)
|
|
69
|
+
assert.equal(pnr('123'), false)
|
|
70
|
+
assert.equal(pnr('00000000000'), false)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getValidNorwegianPersonalNumber } from '../helpers/validateNorwegianPersonalNumber.js'
|
|
2
|
+
|
|
3
|
+
/** Copied from directory/asma-app-directory ActorToUpsertValidator regex helpers — keep pilot behavior identical. */
|
|
4
|
+
export const phoneNrRegex = /^\+?\d{6,13}$/
|
|
5
|
+
export const emailRegex = /^[\w.-]+@([\w-]+\.)+[\w-]{2,4}$/
|
|
6
|
+
export const nameRegex = /^\s*\p{Lu}[\p{L}0-9-]*(\s+\p{L}[\p{L}0-9-]*)*\s*$/u
|
|
7
|
+
|
|
8
|
+
export function required(value: unknown): boolean {
|
|
9
|
+
if (value == null) return false
|
|
10
|
+
if (typeof value === 'string') return value.trim().length > 0
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function email(value: string): boolean {
|
|
15
|
+
return emailRegex.test(value)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function name(value: string): boolean {
|
|
19
|
+
return nameRegex.test(value)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function phoneNr(value: string): boolean {
|
|
23
|
+
return phoneNrRegex.test(value)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function pattern(value: string, regex: RegExp): boolean {
|
|
27
|
+
return regex.test(value)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function pnr(value: string): boolean {
|
|
31
|
+
return getValidNorwegianPersonalNumber(value) != null
|
|
32
|
+
}
|