-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathprogram.schema.ts
More file actions
55 lines (47 loc) · 1.38 KB
/
program.schema.ts
File metadata and controls
55 lines (47 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { z } from 'zod'
export const RawProgramSchema = z.object({
PROGRAM_STRING: z.string(),
PROGRAM_LABEL: z.string(),
PROGRAM_DESCRIPTION: z.string().optional(),
})
export const RawProgramsArraySchema = z.array(RawProgramSchema)
export const ProgramRecordSchema = z.object({
acronym: z.string(),
label: z.string(),
description: z.string().optional(),
})
export type ProgramRecord = z.infer<typeof ProgramRecordSchema>
export function transformProgramData(rawData: unknown): {
programs: ProgramRecord[]
} {
let validated
try {
validated = RawProgramsArraySchema.parse(rawData)
} catch (error) {
if (error instanceof z.ZodError) {
if (Array.isArray(rawData)) {
rawData.forEach((item, index) => {
const result = RawProgramSchema.safeParse(item)
if (!result.success) {
console.error(`\nRecord ${index} failed:`)
console.error('Data:', JSON.stringify(item, null, 2))
}
})
}
}
throw error
}
const uniquePrograms = new Map<string, ProgramRecord>()
validated.forEach((row) => {
if (!uniquePrograms.has(row.PROGRAM_STRING)) {
uniquePrograms.set(row.PROGRAM_STRING, {
acronym: row.PROGRAM_STRING,
label: row.PROGRAM_LABEL,
description: row.PROGRAM_DESCRIPTION,
})
}
})
return {
programs: Array.from(uniquePrograms.values()),
}
}