fix optional recipe schema zod validation (#4900)
This commit is contained in:
@@ -187,40 +187,30 @@ function openApiSchemaToZod(schema: Record<string, unknown>): z.ZodTypeAny {
|
|||||||
shape[propName] = openApiSchemaToZod(propSchema as Record<string, unknown>);
|
shape[propName] = openApiSchemaToZod(propSchema as Record<string, unknown>);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make optional properties optional
|
// Make optional properties optional based on required array
|
||||||
if (schema.required && Array.isArray(schema.required)) {
|
const optionalShape: Record<string, z.ZodTypeAny> = {};
|
||||||
const optionalShape: Record<string, z.ZodTypeAny> = {};
|
const requiredFields =
|
||||||
for (const [propName, zodSchema] of Object.entries(shape)) {
|
schema.required && Array.isArray(schema.required) ? schema.required : [];
|
||||||
if (schema.required.includes(propName)) {
|
|
||||||
optionalShape[propName] = zodSchema;
|
for (const [propName, zodSchema] of Object.entries(shape)) {
|
||||||
} else {
|
if (requiredFields.includes(propName)) {
|
||||||
optionalShape[propName] = zodSchema.optional();
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema.nullable ? objectSchema.nullable() : objectSchema;
|
|
||||||
} else {
|
|
||||||
let objectSchema = z.object(shape);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return schema.nullable ? z.record(z.any()).nullable() : z.record(z.any());
|
return schema.nullable ? z.record(z.any()).nullable() : z.record(z.any());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user