Skip to content

Commit 96e8c46

Browse files
committed
chore: make error message describe issue and path for all possible zod errors
1 parent 8fec855 commit 96e8c46

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

apps/web/src/features/upload/utils.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,14 +486,14 @@ export async function processInstrumentCSV(
486486
//TODO make this type safe without having to cast z.AnyZodObject
487487
instrumentSchemaWithInternal = (instrumentSchemaDef.schema as z.AnyZodObject).extend({
488488
date: z.coerce.date(),
489-
subjectID: z.string()
489+
subjectID: z.string().regex(/^[^$]+$/, 'Subject ID has to be at least 1 character long and without a $')
490490
});
491491

492492
shape = instrumentSchemaWithInternal._def.shape() as { [key: string]: z.ZodTypeAny };
493493
} else {
494494
instrumentSchemaWithInternal = instrumentSchema.extend({
495495
date: z.coerce.date(),
496-
subjectID: z.string()
496+
subjectID: z.string().regex(/^[^$]+$/, 'Subject ID has to be at least 1 character long and without a $')
497497
});
498498
shape = instrumentSchemaWithInternal.shape as { [key: string]: z.ZodTypeAny };
499499
}
@@ -571,8 +571,11 @@ export async function processInstrumentCSV(
571571
//create error message with zodcheck error messsage + zodcheck error path
572572
//addNotification({ message: zodCheck.error.issues[0]?.message, type: 'error' });
573573
console.error(zodCheck.error.issues);
574+
const zodIssues = zodCheck.error.issues.map((issue) => {
575+
return `issue message: \n ${issue.message} \n path: ${issue.path.toString()}"`;
576+
});
574577
console.error(`Failed to parse data: ${JSON.stringify(jsonLine)}`);
575-
return resolve({ message: zodCheck.error.message, success: false });
578+
return resolve({ message: zodIssues.join(), success: false });
576579
}
577580
result.push(zodCheck.data);
578581
}

0 commit comments

Comments
 (0)