Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add function support for form rules validation #5679

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ const shouldRequired = computed(() => {
return ['required', 'selectRequired'].includes(currentRules.value);
}

if (isFunction(currentRules.value)) {
return true;
}

let isOptional = currentRules?.value?.isOptional?.();

// 如果有设置默认值,则不是必填,需要特殊处理
Expand Down Expand Up @@ -140,6 +144,10 @@ const fieldRules = computed(() => {
return rules;
}

if (isFunction(rules)) {
return rules;
}

const isOptional = !shouldRequired.value;
if (!isOptional) {
const unwrappedRules = (rules as any)?.unwrap?.();
Expand Down
24 changes: 19 additions & 5 deletions packages/@core/ui-kit/form-ui/src/form-render/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import type {
import { computed } from 'vue';

import { Form } from '@vben-core/shadcn-ui';
import { cn, isString, mergeWithArrayOverride } from '@vben-core/shared/utils';
import {
cn,
isFunction,
isString,
mergeWithArrayOverride,
} from '@vben-core/shared/utils';

import { provideFormRenderProps } from './context';
import { useExpandable } from './expandable';
Expand Down Expand Up @@ -48,15 +53,24 @@ const shapes = computed(() => {

let typeName = '';
if (rules && !isString(rules)) {
typeName = rules._def.typeName;
typeName = isFunction(rules) ? 'ZodFunction' : rules._def.typeName;
}

const baseRules = getBaseRules(rules) as ZodTypeAny;
const baseRules =
typeName === 'ZodFunction' ? rules : (getBaseRules(rules) as ZodTypeAny);

const _default =
typeName === 'ZodFunction' ? undefined : getDefaultValueInZodStack(rules);

const required =
typeName === 'ZodFunction'
? true
: !['ZodNullable', 'ZodOptional'].includes(typeName);

resultShapes.push({
default: getDefaultValueInZodStack(rules),
default: _default,
fieldName,
required: !['ZodNullable', 'ZodOptional'].includes(typeName),
required,
rules: baseRules,
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/@core/ui-kit/form-ui/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type CustomRenderType = (() => Component | string) | string;
export type FormSchemaRuleType =
| 'required'
| 'selectRequired'
| ((v: any) => (boolean | string) | Promise<boolean | string>)
| null
| (Record<never, never> & string)
| ZodTypeAny;
Expand Down
15 changes: 12 additions & 3 deletions packages/@core/ui-kit/form-ui/src/use-form-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ExtendedFormApi, FormActions, VbenFormProps } from './types';
import { computed, unref, useSlots } from 'vue';

import { createContext } from '@vben-core/shadcn-ui';
import { isString } from '@vben-core/shared/utils';
import { isFunction, isString } from '@vben-core/shared/utils';

import { useForm } from 'vee-validate';
import { object } from 'zod';
Expand Down Expand Up @@ -48,8 +48,17 @@ export function useFormInitial(
(unref(props).schema || []).forEach((item) => {
if (Reflect.has(item, 'defaultValue')) {
initialValues[item.fieldName] = item.defaultValue;
} else if (item.rules && !isString(item.rules)) {
zodObject[item.fieldName] = item.rules;
} else if (item.rules) {
if (
typeof item.rules === 'object' &&
'parse' in item.rules &&
typeof item.rules.parse === 'function' &&
'_def' in item.rules
) {
zodObject[item.fieldName] = item.rules;
} else if (isFunction(item.rules) || isString(item.rules)) {
initialValues[item.fieldName] = undefined;
}
}
});

Expand Down
Loading