Skip to content

Commit 0e798ec

Browse files
committed
chore: error message now include column name, made subjectid validation schema variable
1 parent cff813c commit 0e798ec

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ export function interpretZodValue(
236236
} else if (entry.toLowerCase() === 'false') {
237237
return { success: true, value: false };
238238
}
239-
return { message: `Undecipherable Boolean Type: ${entry}`, success: false };
239+
return { message: `Undecipherable Boolean Type: '${entry}'`, success: false };
240240
case 'ZodDate': {
241241
const date = new Date(entry);
242242
if (isNaN(date.getTime())) {
243-
return { message: `Failed to parse date: ${entry}`, success: false };
243+
return { message: `Failed to parse date: '${entry}'`, success: false };
244244
}
245245
return { success: true, value: date };
246246
}
@@ -250,7 +250,7 @@ export function interpretZodValue(
250250
if (isNumberLike(entry)) {
251251
return { success: true, value: parseNumber(entry) };
252252
}
253-
return { message: `Invalid number type: ${entry}`, success: false };
253+
return { message: `Invalid number type: '${entry}'`, success: false };
254254
case 'ZodSet':
255255
try {
256256
if (entry.startsWith('SET(')) {
@@ -268,7 +268,7 @@ export function interpretZodValue(
268268
return { message: 'Error occurred interpreting set entry', success: false };
269269
}
270270

271-
return { message: `Invalid ZodSet: ${entry}`, success: false };
271+
return { message: `Invalid ZodSet: '${entry}'`, success: false };
272272
case 'ZodString':
273273
return { success: true, value: entry };
274274
default:
@@ -479,21 +479,22 @@ export async function processInstrumentCSV(
479479
let instrumentSchemaWithInternal: z.AnyZodObject;
480480

481481
const instrumentSchemaDef: unknown = instrumentSchema._def;
482+
const $SubjectIdValidation = z
483+
.string()
484+
.regex(/^[^$\s]+$/, 'Subject ID has to be at least 1 character long, without a $ and no whitespaces');
482485

483486
if (isZodTypeDef(instrumentSchemaDef) && isZodEffectsDef(instrumentSchemaDef)) {
484487
//TODO make this type safe without having to cast z.AnyZodObject
485488
instrumentSchemaWithInternal = (instrumentSchemaDef.schema as z.AnyZodObject).extend({
486489
date: z.coerce.date(),
487-
subjectID: z
488-
.string()
489-
.regex(/^[^$\s]+$/, 'Subject ID has to be at least 1 character long, without a $ and no whitespaces')
490+
subjectID: $SubjectIdValidation
490491
});
491492

492493
shape = instrumentSchemaWithInternal._def.shape() as { [key: string]: z.ZodTypeAny };
493494
} else {
494495
instrumentSchemaWithInternal = instrumentSchema.extend({
495496
date: z.coerce.date(),
496-
subjectID: z.string().regex(/^[^$\s]+$/, 'Subject ID has to be at least 1 character long and without a $')
497+
subjectID: $SubjectIdValidation
497498
});
498499
shape = instrumentSchemaWithInternal.shape as { [key: string]: z.ZodTypeAny };
499500
}
@@ -563,15 +564,15 @@ export async function processInstrumentCSV(
563564
interpreterResult = interpretZodValue(rawValue, typeNameResult.typeName, typeNameResult.isOptional);
564565
}
565566
if (!interpreterResult.success) {
566-
return resolve({ message: interpreterResult.message, success: false });
567+
return resolve({ message: `${interpreterResult.message} at column name: '${key}'`, success: false });
567568
}
568569
jsonLine[headers[j]!] = interpreterResult.value;
569570
}
570571
const zodCheck = instrumentSchemaWithInternal.safeParse(jsonLine);
571572
if (!zodCheck.success) {
572573
console.error(zodCheck.error.issues);
573574
const zodIssues = zodCheck.error.issues.map((issue) => {
574-
return `issue message: \n ${issue.message} \n path: ${issue.path.toString()}"`;
575+
return `issue message: \n ${issue.message} \n path: ${issue.path.toString()}`;
575576
});
576577
console.error(`Failed to parse data: ${JSON.stringify(jsonLine)}`);
577578
return resolve({ message: zodIssues.join(), success: false });

0 commit comments

Comments
 (0)