Skip to content

Commit 93f5c48

Browse files
committed
chore: update
1 parent 03b43c3 commit 93f5c48

2 files changed

Lines changed: 33 additions & 84 deletions

File tree

apps/web/src/utils/upload.ts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -600,87 +600,6 @@ function interpretZodObjectValue(
600600
}
601601
}
602602

603-
function formatTypeInfo(s: string, isOptional: boolean) {
604-
return isOptional ? `${s} (optional)` : s;
605-
}
606-
607-
function generateSampleData({
608-
enumValues,
609-
isOptional,
610-
multiKeys,
611-
multiValues,
612-
typeName
613-
}: Extract<Exclude<ZodTypeNameResult, 'ZodEffects'>, { success: true }>) {
614-
switch (typeName) {
615-
case 'ZodBoolean':
616-
return formatTypeInfo('true/false', isOptional);
617-
case 'ZodDate':
618-
return formatTypeInfo('yyyy-mm-dd', isOptional);
619-
case 'ZodNumber':
620-
return formatTypeInfo('number', isOptional);
621-
case 'ZodSet':
622-
try {
623-
if (enumValues) return formatTypeInfo(`SET(${enumValues.join('/')}, ...)`, isOptional);
624-
625-
return formatTypeInfo('SET(a,b,c)', isOptional);
626-
} catch {
627-
throw new Error(
628-
`Failed to generate sample data for ZodSet / Échec de la génération de données d'exemple pour ZodSet`
629-
);
630-
}
631-
632-
case 'ZodString':
633-
return formatTypeInfo('string', isOptional);
634-
case 'ZodEnum':
635-
try {
636-
let possibleEnumOutputs = '';
637-
for (const val of enumValues!) {
638-
possibleEnumOutputs += val + '/';
639-
}
640-
possibleEnumOutputs = possibleEnumOutputs.slice(0, -1);
641-
return formatTypeInfo(possibleEnumOutputs, isOptional);
642-
} catch {
643-
throw new Error('Invalid Enum error / Erreur Enum invalide');
644-
}
645-
case 'ZodArray':
646-
case 'ZodObject':
647-
try {
648-
let multiString = 'RECORD_ARRAY( ';
649-
if (multiValues && multiKeys) {
650-
for (let i = 0; i < multiValues.length; i++) {
651-
const inputData = multiValues[i];
652-
// eslint-disable-next-line max-depth
653-
if (!inputData?.success) {
654-
console.error({ inputData });
655-
throw new Error(
656-
`input data is undefined or unsuccessful / les données d'entrée ne sont pas définies ou ne sont pas réussies`
657-
);
658-
}
659-
// eslint-disable-next-line max-depth
660-
if (i === multiValues.length - 1 && multiValues[i] !== undefined) {
661-
multiString += multiKeys[i] + ':' + generateSampleData(inputData);
662-
} else {
663-
multiString += multiKeys[i] + ':' + generateSampleData(inputData) + ',';
664-
}
665-
}
666-
multiString += ';)';
667-
}
668-
return multiString;
669-
} catch (e) {
670-
if (e instanceof Error && e.message.includes('input data is undefined or unsuccessful')) {
671-
throw new Error(
672-
"Unsuccessful input data transfer or undefined data / Transfert de données d'entrée non réussi ou données non définies"
673-
);
674-
}
675-
676-
throw new Error("Invalid Record Array Error / Erreur de tableau d'enregistrements invalide");
677-
}
678-
default:
679-
throw new Error(
680-
`Invalid zod schema: unexpected type name '${typeName satisfies never}' / Schéma zod invalide : nom de type inattendu '${typeName satisfies never}'`
681-
);
682-
}
683-
}
684603
function jsonToZod(givenType: unknown): RequiredZodTypeName {
685604
if (typeof givenType === 'string') {
686605
switch (givenType) {

apps/web/src/utils/upload2.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,38 @@ namespace Zod3 {
103103
return isZodArrayDef(def) && isZodObject(def.type);
104104
}
105105

106-
function interpetZodArray(..._args: any[]): ZodTypeNameResult {
107-
return null!;
106+
function interpretZodArray(def: ZodObjectArrayDef, isOptional?: boolean): ZodTypeNameResult {
107+
const listOfZodElements: ZodTypeNameResult[] = [];
108+
const listOfZodKeys: string[] = [];
109+
110+
const shape = def.type.shape as { [key: string]: z3.ZodTypeAny };
111+
112+
for (const [key, insideType] of Object.entries(shape)) {
113+
const def: unknown = insideType._def;
114+
if (isZodTypeDef(def)) {
115+
const innerTypeName = getZodTypeName(insideType);
116+
listOfZodElements.push(innerTypeName);
117+
listOfZodKeys.push(key);
118+
} else {
119+
console.error({ def });
120+
throw new UploadError({
121+
en: `Unhandled case!`,
122+
fr: `Cas non géré !`
123+
});
124+
}
125+
}
126+
if (listOfZodElements.length === 0) {
127+
throw new UploadError({
128+
en: 'Failure to interpret Zod Object or Array',
129+
fr: "Échec de l'interprétation de l'objet ou du tableau Zod"
130+
});
131+
}
132+
return {
133+
isOptional: Boolean(isOptional),
134+
multiKeys: listOfZodKeys,
135+
multiValues: listOfZodElements,
136+
typeName: def.typeName
137+
};
108138
}
109139

110140
function getZodTypeName(schema: z3.ZodTypeAny, isOptional?: boolean): ZodTypeNameResult {
@@ -121,7 +151,7 @@ namespace Zod3 {
121151
typeName: def.typeName
122152
};
123153
} else if (isZodObjectArrayDef(def)) {
124-
return interpetZodArray(schema, def.typeName, isOptional);
154+
return interpretZodArray(def, isOptional);
125155
} else if (isZodSetDef(def)) {
126156
const innerDef: unknown = def.valueType._def;
127157
if (!isZodTypeDef(innerDef)) {

0 commit comments

Comments
 (0)