From b69d84c60946dadb74833879ad1cb02b445d6392 Mon Sep 17 00:00:00 2001 From: "Water.Li" Date: Tue, 19 Dec 2023 19:22:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=A1=A8=E5=8D=95=E6=A0=A1?= =?UTF-8?q?=E9=AA=8Cbug=EF=BC=9Aschema=E5=8F=82=E6=95=B0=E4=BC=A0=E9=94=99?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E4=B8=8D=E8=83=BD=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fieldy/fieldy-react/src/components/Field/index.tsx | 3 +-- .../src/components/XField/hooks/useCreateFieldSchema.ts | 8 +++++--- packages/fieldy/fieldy/src/classes/FormImpl.ts | 1 + .../fieldy/yup-validation/src/classes/YupValidator.ts | 5 ++--- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/fieldy/fieldy-react/src/components/Field/index.tsx b/packages/fieldy/fieldy-react/src/components/Field/index.tsx index 2bbb602e7..ff3993db7 100644 --- a/packages/fieldy/fieldy-react/src/components/Field/index.tsx +++ b/packages/fieldy/fieldy-react/src/components/Field/index.tsx @@ -18,8 +18,7 @@ export const Field = ( } ) => { const { name, value, initialValue, defaultValue, children, type, rules } = props - const fieldMeta = useCreateFieldSchema(name, type) - fieldMeta.validateRules = rules + const fieldMeta = useCreateFieldSchema(name, type, rules) return ( diff --git a/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts b/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts index 25092ffcd..052b6e249 100644 --- a/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts +++ b/packages/fieldy/fieldy-react/src/components/XField/hooks/useCreateFieldSchema.ts @@ -2,8 +2,9 @@ import { IFieldSchema } from "@rxdrag/fieldy" import { useMemo, useRef } from "react" import { useFieldPath } from "../../../hooks/useFieldPath" import { FieldType } from "@rxdrag/fieldy" +import { IYupValidateSchema } from "@rxdrag/fieldy-yup-validation" -export function useCreateFieldSchema(name: string | number, type?: FieldType) { +export function useCreateFieldSchema(name: string | number, type?: FieldType, rules?: IYupValidateSchema) { const parentPath = useFieldPath() || "" const fieldPath = useMemo(() => { return parentPath ? parentPath + "." + name : name.toString() @@ -15,9 +16,10 @@ export function useCreateFieldSchema(name: string | number, type?: FieldType) { return { path: fieldPath, type: type, - name + name, + validateRules: rules, } - }, [fieldPath, name, type]) + }, [fieldPath, name, rules, type]) return fieldSchema } \ No newline at end of file diff --git a/packages/fieldy/fieldy/src/classes/FormImpl.ts b/packages/fieldy/fieldy/src/classes/FormImpl.ts index 562566333..62cd8fd4a 100644 --- a/packages/fieldy/fieldy/src/classes/FormImpl.ts +++ b/packages/fieldy/fieldy/src/classes/FormImpl.ts @@ -113,6 +113,7 @@ export class FormImpl implements IForm { try { const value = await this.fieldy.validator.validateForm(this) + this.validationSubscriber.emitSuccess(value) return { status: ValidateStatus.success, diff --git a/packages/fieldy/yup-validation/src/classes/YupValidator.ts b/packages/fieldy/yup-validation/src/classes/YupValidator.ts index ec56de099..1ad1c9d15 100644 --- a/packages/fieldy/yup-validation/src/classes/YupValidator.ts +++ b/packages/fieldy/yup-validation/src/classes/YupValidator.ts @@ -1,6 +1,6 @@ import { IField, IFieldSchema, IForm, IValidationError, IValidator } from "@rxdrag/fieldy"; import { PredeinedValidators, YupType, IYupValidateSchema } from "../interfaces"; -import { object, string, boolean, number, ValidationError, Schema, mixed } from 'yup'; +import { object, ValidationError, Schema, mixed } from 'yup'; import { predifinedValidators } from "../predefineds"; export class YupValidator implements IValidator { @@ -17,8 +17,7 @@ export class YupValidator implements IValidator { this.validateOneObject(field?.getValue(), field?.getSubFieldSchemas() || []) } } - const rootSchemas = form.getRootFields() - return this.validateOneObject(form.getValue(), rootSchemas) + return this.validateOneObject(form.getValue(), schemas) } validateField(field: IField): Promise {