Skip to content

Commit 03b43c3

Browse files
committed
chore: update
1 parent f5679f0 commit 03b43c3

1 file changed

Lines changed: 89 additions & 6 deletions

File tree

apps/web/src/utils/upload2.ts

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ namespace Zod3 {
5959

6060
type ZodObjectArrayDef = z3.ZodArrayDef & { type: z3.AnyZodObject };
6161

62+
type ZodTypeNameResult = {
63+
enumValues?: readonly string[];
64+
isOptional: boolean;
65+
multiKeys?: string[];
66+
multiValues?: ZodTypeNameResult[];
67+
typeName: RequiredZodTypeName;
68+
};
69+
6270
function isZodEffects(value: unknown): value is z3.ZodEffects<z3.ZodType> {
6371
return isObjectLike(value) && value.constructor.name === 'ZodEffects';
6472
}
@@ -95,11 +103,11 @@ namespace Zod3 {
95103
return isZodArrayDef(def) && isZodObject(def.type);
96104
}
97105

98-
function interpetZodArray(..._args: any[]) {
99-
return null as unknown;
106+
function interpetZodArray(..._args: any[]): ZodTypeNameResult {
107+
return null!;
100108
}
101109

102-
function getZodTypeName(schema: z3.ZodTypeAny, isOptional?: boolean): unknown {
110+
function getZodTypeName(schema: z3.ZodTypeAny, isOptional?: boolean): ZodTypeNameResult {
103111
const def: unknown = schema._def;
104112
if (!isZodTypeDef(def)) {
105113
console.error(`Cannot parse ZodType from schema: ${JSON.stringify(schema)}`);
@@ -110,7 +118,6 @@ namespace Zod3 {
110118
return {
111119
enumValues: def.values,
112120
isOptional: Boolean(isOptional),
113-
success: true,
114121
typeName: def.typeName
115122
};
116123
} else if (isZodObjectArrayDef(def)) {
@@ -127,18 +134,94 @@ namespace Zod3 {
127134
return {
128135
enumValues: innerDef.values,
129136
isOptional: Boolean(isOptional),
130-
success: true,
131137
typeName: def.typeName
132138
};
133139
}
134140
}
135141
return {
136142
isOptional: Boolean(isOptional),
137-
success: true,
138143
typeName: def.typeName satisfies ZodTypeName as RequiredZodTypeName
139144
};
140145
}
141146

147+
function formatTypeInfo(s: string, isOptional: boolean) {
148+
return isOptional ? `${s} (optional)` : s;
149+
}
150+
151+
function generateSampleData({
152+
enumValues,
153+
isOptional,
154+
multiKeys,
155+
multiValues,
156+
typeName
157+
}: Exclude<ZodTypeNameResult, 'ZodEffects'>) {
158+
switch (typeName) {
159+
case 'ZodBoolean':
160+
return formatTypeInfo('true/false', isOptional);
161+
case 'ZodDate':
162+
return formatTypeInfo('yyyy-mm-dd', isOptional);
163+
case 'ZodNumber':
164+
return formatTypeInfo('number', isOptional);
165+
case 'ZodSet':
166+
try {
167+
if (enumValues) {
168+
return formatTypeInfo(`SET(${enumValues.join('/')}, ...)`, isOptional);
169+
}
170+
return formatTypeInfo('SET(a,b,c)', isOptional);
171+
} catch {
172+
throw new UploadError({
173+
en: `Failed to generate sample data for ZodSet`,
174+
fr: `Échec de la génération de données d'exemple pour ZodSet`
175+
});
176+
}
177+
case 'ZodString':
178+
return formatTypeInfo('string', isOptional);
179+
case 'ZodEnum':
180+
try {
181+
let possibleEnumOutputs = '';
182+
for (const val of enumValues!) {
183+
possibleEnumOutputs += val + '/';
184+
}
185+
possibleEnumOutputs = possibleEnumOutputs.slice(0, -1);
186+
return formatTypeInfo(possibleEnumOutputs, isOptional);
187+
} catch {
188+
throw new UploadError({
189+
en: 'Invalid Enum error',
190+
fr: 'Erreur Enum invalide'
191+
});
192+
}
193+
case 'ZodArray':
194+
case 'ZodObject':
195+
try {
196+
let multiString = 'RECORD_ARRAY( ';
197+
if (multiValues && multiKeys) {
198+
for (let i = 0; i < multiValues.length; i++) {
199+
const inputData = multiValues[i]!;
200+
// eslint-disable-next-line max-depth
201+
if (i === multiValues.length - 1 && multiValues[i] !== undefined) {
202+
multiString += multiKeys[i] + ':' + generateSampleData(inputData);
203+
} else {
204+
multiString += multiKeys[i] + ':' + generateSampleData(inputData) + ',';
205+
}
206+
}
207+
multiString += ';)';
208+
}
209+
return multiString;
210+
} catch (e) {
211+
throw new UploadError({
212+
en: `Invalid Record Array Error` + (e instanceof UploadError ? `: ${e.description.en}` : ''),
213+
// prettier-ignore
214+
fr: `Erreur de tableau d'enregistrements invalide` + (e instanceof UploadError ? ` : ${e.description.fr}` : '')
215+
});
216+
}
217+
default:
218+
throw new UploadError({
219+
en: `Invalid zod schema: unexpected type name '${typeName satisfies never}'`,
220+
fr: `Schéma zod invalide : nom de type inattendu '${typeName satisfies never}'`
221+
});
222+
}
223+
}
224+
142225
export function createUploadTemplateCSV(
143226
instrumentSchema: z3.ZodType<FormTypes.Data>,
144227
instrumentInternal: AnyUnilingualFormInstrument['internal']

0 commit comments

Comments
 (0)