Browse Source

fix(form-ui): support native zod string format defaults

pull/8372/head
kilisamemarisaaa 4 weeks ago
parent
commit
29025b8dbc
  1. 5
      .changeset/bright-zebras-default.md
  2. 26
      packages/@core/ui-kit/form-ui/__tests__/form-integration.test.ts
  3. 13
      packages/@core/ui-kit/form-ui/src/use-form-context.ts

5
.changeset/bright-zebras-default.md

@ -0,0 +1,5 @@
---
'@vben-core/form-ui': patch
---
fix(@vben-core/form-ui): initialize native Zod string formats with empty values

26
packages/@core/ui-kit/form-ui/__tests__/form-integration.test.ts

@ -78,6 +78,32 @@ afterEach(() => {
});
describe('useVbenForm integration', () => {
it('provides empty defaults for native zod string formats', async () => {
const [Form, formApi] = useVbenForm({
schema: [
{
component: TestInput,
fieldName: 'email',
rules: z.email(),
},
{
component: TestInput,
fieldName: 'address',
rules: z.ipv4(),
},
],
showDefaultActions: false,
});
const wrapper = mount(Form);
wrappers.push(wrapper);
await flushPromises();
expect(await formApi.getValues()).toEqual({
address: '',
email: '',
});
});
it('uses model updates as the primary channel and preserves empty strings', async () => {
const validateValue = vi.fn();
const [Form, formApi] = useVbenForm({

13
packages/@core/ui-kit/form-ui/src/use-form-context.ts

@ -9,7 +9,14 @@ import { computed, toRaw, unref, useSlots } from 'vue';
import { createContext } from '@vben-core/shadcn-ui';
import { isString, mergeWithArrayOverride, set } from '@vben-core/shared/utils';
import { object, ZodIntersection, ZodNumber, ZodObject, ZodString } from 'zod';
import {
object,
ZodIntersection,
ZodNumber,
ZodObject,
ZodString,
ZodStringFormat,
} from 'zod';
import { getDefaultsForSchema } from 'zod-defaults';
import { useFormRuntime } from './form-runtime';
@ -74,8 +81,8 @@ export function useFormInitial(
// 自定义默认值提取逻辑
function getCustomDefaultValue(rule: any): any {
rule = toRaw(rule);
if (rule instanceof ZodString) {
return ''; // 默认为空字符串
if (rule instanceof ZodString || rule instanceof ZodStringFormat) {
return ''; // 字符串及其格式校验默认为空字符串
} else if (rule instanceof ZodNumber) {
return null; // 默认为 null(避免显示 0)
} else if (rule instanceof ZodObject) {

Loading…
Cancel
Save