Skip to content

Commit 41d614e

Browse files
committed
fix: replace zod parse with custom function to prevent errors in worker with imports
1 parent a60c467 commit 41d614e

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

packages/subject-utils/src/index.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
11
import { sha256 } from '@douglasneuroinformatics/libcrypto';
2-
import { $ClinicalSubjectIdentificationData } from '@opendatacapture/schemas/subject';
32
import type { ClinicalSubjectIdentificationData, Subject } from '@opendatacapture/schemas/subject';
43
import { transliterate } from 'transliteration';
54
import type { SetNonNullable } from 'type-fest';
65

76
type SubjectPersonalInfo = Pick<Subject, 'dateOfBirth' | 'firstName' | 'id' | 'lastName' | 'sex'>;
87

8+
function parseClinicalSubjectIdentificationData({
9+
dateOfBirth,
10+
firstName,
11+
lastName,
12+
sex
13+
}: {
14+
[key: string]: unknown;
15+
}): ClinicalSubjectIdentificationData {
16+
if (!(dateOfBirth instanceof Date)) {
17+
throw new Error('Invalid or missing dateOfBirth: Expected a Date object.');
18+
} else if (typeof firstName !== 'string') {
19+
throw new Error('Invalid or missing firstName: Expected a string.');
20+
} else if (typeof lastName !== 'string') {
21+
throw new Error('Invalid or missing lastName: Expected a string.');
22+
} else if (!(sex === 'MALE' || sex === 'FEMALE')) {
23+
throw new Error('Invalid or missing sex: Expected "MALE" or "FEMALE".');
24+
}
25+
return {
26+
dateOfBirth,
27+
firstName,
28+
lastName,
29+
sex
30+
};
31+
}
32+
933
export async function generateSubjectHash(data: ClinicalSubjectIdentificationData): Promise<string> {
1034
// In case the type system breaks somewhere, this should crash at runtime instead of generating an incorrect ID which would cause chaos
11-
const { dateOfBirth, firstName, lastName, sex } = await $ClinicalSubjectIdentificationData.parseAsync(data);
35+
const { dateOfBirth, firstName, lastName, sex } = parseClinicalSubjectIdentificationData(data);
1236
const shortDateOfBirth = dateOfBirth.toISOString().split('T')[0];
1337
const info = firstName + lastName + shortDateOfBirth + sex;
1438
const source = transliterate(info.toUpperCase().replaceAll('-', ''));

0 commit comments

Comments
 (0)