Browse Source

fix(@vben-core/form-ui): 兼容 zod v4 的 ZodArray,避免误用 unwrap() 丢失数组外壳导致数组校验失败 (#8207)

pull/8206/head
JyQAQ 1 month ago
committed by GitHub
parent
commit
b04d728249
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 9
      packages/@core/ui-kit/form-ui/src/form-render/helper.ts

9
packages/@core/ui-kit/form-ui/src/form-render/helper.ts

@ -22,6 +22,15 @@ export function getBaseRules(schema?: null | string | ZodType): null | 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?.();
if (unwrappedSchema && unwrappedSchema !== rawSchema) {
return getBaseRules(unwrappedSchema);

Loading…
Cancel
Save