Skip to content

Commit 3d97cc5

Browse files
committed
feat: deal with situation where required is undefined (all questions optional)
1 parent 9ab413d commit 3d97cc5

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

apps/web/src/utils/upload2.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ namespace Zod4 {
312312
function parseJSONSchema(jsonInstrumentSchema: z4.core.JSONSchema.ObjectSchema) {
313313
// TODO - these could actually not exist
314314
// prettier-ignore
315-
if (!(jsonInstrumentSchema.properties && jsonInstrumentSchema.required && Array.isArray(jsonInstrumentSchema.required))) {
315+
if (!(jsonInstrumentSchema.properties)) {
316316
throw new UploadError({
317317
en: "Failed to interpret JSON schema",
318318
fr: "Échec de l'interprétation du schéma JSON"
@@ -326,8 +326,12 @@ namespace Zod4 {
326326

327327
for (const col of jsonColumnNames) {
328328
let optional = true;
329-
// let data: ZodTypeNameResult;
330-
if (jsonInstrumentSchema.required.includes(col)) {
329+
330+
if (
331+
jsonInstrumentSchema.required &&
332+
Array.isArray(jsonInstrumentSchema.required) &&
333+
jsonInstrumentSchema.required.includes(col)
334+
) {
331335
optional = false;
332336
}
333337

@@ -352,12 +356,21 @@ namespace Zod4 {
352356
let i = 0;
353357

354358
for (const val of values) {
355-
if (val.type && Array.isArray(itemsSchema.required) && keys[i]) {
359+
if (val.type && keys[i]) {
356360
// optional is false if the key is included in the required items
357-
multiVals.push({
358-
isOptional: !itemsSchema.required.includes(keys[i]!),
359-
typeName: jsonToZod(val.type)
360-
});
361+
362+
if (itemsSchema && Array.isArray(itemsSchema.required)) {
363+
multiVals.push({
364+
isOptional: !itemsSchema.required.includes(keys[i]!),
365+
typeName: jsonToZod(val.type)
366+
});
367+
} else {
368+
multiVals.push({
369+
isOptional: false,
370+
typeName: jsonToZod(val.type)
371+
});
372+
}
373+
361374
i++;
362375
}
363376
}

0 commit comments

Comments
 (0)