Skip to content

Commit fb29ae3

Browse files
committed
feat: update interetpretZodConvertResult to handle objects, add french for error messages
1 parent df15dcd commit fb29ae3

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

apps/web/src/utils/upload.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ class UnexpectedZodTypeError extends UploadError {
9494

9595
static forTypeName(typeName: string | undefined) {
9696
return new this({
97-
en: `Unexpected Zod type name '${typeName}'`
97+
en: `Unexpected Zod type name '${typeName}'`,
98+
fr: `Nom de type Zod inattendu '${typeName}'`
9899
});
99100
}
100101
}
101102

102103
function extractSetEntry(entry: string) {
103104
const result = /SET\(\s*(.*?)\s*\)/.exec(entry);
105+
104106
if (!result?.[1]) {
105107
throw new UploadError({
106108
en: `Failed to extract set value from entry: '${entry}'`,
@@ -116,15 +118,18 @@ function extractSetEntry(entry: string) {
116118
}
117119

118120
function extractRecordArrayEntry(entry: string): { [key: string]: string }[] {
119-
const result = /RECORD_ARRAY\(\s*(.*?)[\s;]*\)/.exec(entry);
120-
if (!result?.[1]) {
121+
entry = entry.trim();
122+
const opening = 'RECORD_ARRAY(';
123+
const closing = ')';
124+
if (!entry.startsWith(opening) && !entry.endsWith(closing)) {
121125
throw new UploadError({
122-
en: `Failed to extract record array value from entry: '${entry}'`,
123-
fr: `Échec de l'extraction de la valeur du tableau d'enregistrements de l'entrée : '${entry}'`
126+
en: `Syntax error in RECORD_ARRAY declaration: ${entry}`,
127+
fr: `Erreur de syntaxe dans la déclaration RECORD_ARRAY : ${entry}`
124128
});
125129
}
130+
const result = entry.slice(opening.length, -1);
126131

127-
const recordArrayDataList = result[1].split(';');
132+
const recordArrayDataList = result.split(';');
128133

129134
if (recordArrayDataList.at(-1) === '') {
130135
recordArrayDataList.pop();
@@ -149,7 +154,8 @@ function extractRecordArrayEntry(entry: string): { [key: string]: string }[] {
149154
const [recordKey, recordValue] = rawRecord.split(':').map((s) => s.trim());
150155
if (!(recordKey && recordValue)) {
151156
throw new UploadError({
152-
en: `Malformed record at index ${i}`
157+
en: `Malformed record at index ${i}`,
158+
fr: `Enregistrement mal formé à l'index ${i}`
153159
});
154160
}
155161
records[recordKey] = recordValue;
@@ -365,8 +371,9 @@ export namespace Zod3 {
365371
} catch (e) {
366372
throw new UploadError({
367373
en: `Invalid Record Array Error` + (e instanceof UploadError ? `: ${e.description.en}` : ''),
368-
// prettier-ignore
369-
fr: `Erreur de tableau d'enregistrements invalide` + (e instanceof UploadError ? ` : ${e.description.fr}` : '')
374+
fr:
375+
`Erreur de tableau d'enregistrements invalide` +
376+
(e instanceof UploadError ? ` : ${e.description.fr}` : '')
370377
});
371378
}
372379
default:
@@ -682,9 +689,16 @@ export namespace Zod4 {
682689
switch (convertResult.typeName) {
683690
case 'array':
684691
return extractRecordArrayEntry(entry).map((parsedRecord) => {
685-
return mapValues(parsedRecord, (entry): unknown => interpretZodConvertResult(convertResult.innerType, entry));
692+
if (convertResult.innerType.typeName !== 'object') {
693+
throw new UploadError({
694+
en: `Unsupported type for innerType of array record '${convertResult.innerType.typeName}': must be 'object'`,
695+
fr: `Type non pris en charge pour innerType d'enregistrement de tableau '${convertResult.innerType.typeName}': doit être « objet »`
696+
});
697+
}
698+
return mapValues(parsedRecord, (value, key): unknown =>
699+
interpretZodConvertResult((convertResult.innerType as ObjectZodConvertResult).dimensions[key]!, value)
700+
);
686701
});
687-
688702
case 'boolean':
689703
return parseBooleanEntry(entry);
690704
case 'date':
@@ -751,7 +765,8 @@ export namespace Zod4 {
751765
) {
752766
if (!(instrumentSchema instanceof z4.ZodObject)) {
753767
throw new UploadError({
754-
en: 'Expected schema to be instance of ZodObject'
768+
en: 'Expected schema to be instance of ZodObject',
769+
fr: 'Le schéma attendu doit être une instance de ZodObject'
755770
});
756771
}
757772

@@ -775,7 +790,8 @@ export namespace Zod4 {
775790
): Promise<FormTypes.Data[]> {
776791
if (!(instrument.validationSchema instanceof z4.ZodObject)) {
777792
throw new UploadError({
778-
en: 'Expected schema to be instance of ZodObject'
793+
en: 'Expected schema to be instance of ZodObject',
794+
fr: 'Le schéma attendu doit être une instance de ZodObject'
779795
});
780796
}
781797

@@ -859,7 +875,8 @@ export namespace Zod4 {
859875
console.error(`Failed to parse data: ${JSON.stringify(jsonLine)}`);
860876
return reject(
861877
new UploadError({
862-
en: 'Schema parsing failed: refer to the browser console for further details'
878+
en: 'Schema parsing failed: refer to the browser console for further details',
879+
fr: `Échec de l'analyse du schéma : reportez-vous à la console du navigateur pour plus de détails`
863880
})
864881
);
865882
}

0 commit comments

Comments
 (0)