Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions ui/desktop/src/recipe/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,40 +187,30 @@ function openApiSchemaToZod(schema: Record<string, unknown>): z.ZodTypeAny {
shape[propName] = openApiSchemaToZod(propSchema as Record<string, unknown>);
}

// Make optional properties optional
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this anyway? why do we have our own validation here?

if (schema.required && Array.isArray(schema.required)) {
const optionalShape: Record<string, z.ZodTypeAny> = {};
for (const [propName, zodSchema] of Object.entries(shape)) {
if (schema.required.includes(propName)) {
optionalShape[propName] = zodSchema;
} else {
optionalShape[propName] = zodSchema.optional();
}
}
let objectSchema = z.object(optionalShape);

if (schema.additionalProperties === true) {
return schema.nullable
? objectSchema.passthrough().nullable()
: objectSchema.passthrough();
} else if (schema.additionalProperties === false) {
return schema.nullable ? objectSchema.strict().nullable() : objectSchema.strict();
// Make optional properties optional based on required array
const optionalShape: Record<string, z.ZodTypeAny> = {};
const requiredFields =
schema.required && Array.isArray(schema.required) ? schema.required : [];

for (const [propName, zodSchema] of Object.entries(shape)) {
if (requiredFields.includes(propName)) {
optionalShape[propName] = zodSchema;
} else {
optionalShape[propName] = zodSchema.optional();
}
}

return schema.nullable ? objectSchema.nullable() : objectSchema;
} else {
let objectSchema = z.object(shape);
let objectSchema = z.object(optionalShape);

if (schema.additionalProperties === true) {
return schema.nullable
? objectSchema.passthrough().nullable()
: objectSchema.passthrough();
} else if (schema.additionalProperties === false) {
return schema.nullable ? objectSchema.strict().nullable() : objectSchema.strict();
}

return schema.nullable ? objectSchema.nullable() : objectSchema;
if (schema.additionalProperties === true) {
return schema.nullable
? objectSchema.passthrough().nullable()
: objectSchema.passthrough();
} else if (schema.additionalProperties === false) {
return schema.nullable ? objectSchema.strict().nullable() : objectSchema.strict();
}

return schema.nullable ? objectSchema.nullable() : objectSchema;
}
return schema.nullable ? z.record(z.any()).nullable() : z.record(z.any());

Expand Down
Loading