fix(@vben-core/form-ui): 兼容 zod v4 的 ZodArray,避免误用 unwrap() 丢失数组外壳导致数组校验失败 (#8207)
This commit is contained in:
@@ -22,6 +22,15 @@ export function getBaseRules(schema?: null | string | ZodType): null | ZodType {
|
|||||||
return getBaseRules(rawSchema.in as ZodType);
|
return getBaseRules(rawSchema.in as ZodType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// In zod v4, ZodArray also has an unwrap() that returns its element type
|
||||||
|
// (not an outer wrapper). We must not unwrap it, otherwise z.array(T) loses
|
||||||
|
// its array shell and array values fail validation.
|
||||||
|
const defType = (rawSchema as unknown as { _zod?: { def: { type: string } } })
|
||||||
|
._zod?.def?.type;
|
||||||
|
if (defType === 'array') {
|
||||||
|
return rawSchema;
|
||||||
|
}
|
||||||
|
|
||||||
const unwrappedSchema = (rawSchema as UnwrappableZodType).unwrap?.();
|
const unwrappedSchema = (rawSchema as UnwrappableZodType).unwrap?.();
|
||||||
if (unwrappedSchema && unwrappedSchema !== rawSchema) {
|
if (unwrappedSchema && unwrappedSchema !== rawSchema) {
|
||||||
return getBaseRules(unwrappedSchema);
|
return getBaseRules(unwrappedSchema);
|
||||||
|
|||||||
Reference in New Issue
Block a user