From 7dcf591aad550255e39cb14e751429ec2c27f15a Mon Sep 17 00:00:00 2001 From: Paul Grau Date: Fri, 4 Aug 2023 19:08:04 +0900 Subject: [PATCH] fix(Form): use safeParseAsync for zod (#497) --- src/runtime/components/forms/Form.ts | 34 ++++++++++++---------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/runtime/components/forms/Form.ts b/src/runtime/components/forms/Form.ts index 68dccd283f..ec5186296d 100644 --- a/src/runtime/components/forms/Form.ts +++ b/src/runtime/components/forms/Form.ts @@ -1,8 +1,11 @@ import { provide, ref, type PropType, h, defineComponent } from 'vue' import { useEventBus } from '@vueuse/core' -import type { ZodSchema, ZodError } from 'zod' +import type { ZodSchema } from 'zod' import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi' -import type { ObjectSchema as YupObjectSchema, ValidationError as YupError } from 'yup' +import type { + ObjectSchema as YupObjectSchema, + ValidationError as YupError +} from 'yup' import type { FormError, FormEvent } from '../../types' export default defineComponent({ @@ -19,7 +22,9 @@ export default defineComponent({ required: true }, validate: { - type: Function as PropType<(state: any) => Promise> | PropType<(state: any) => FormError[]>, + type: Function as + | PropType<(state: any) => Promise> + | PropType<(state: any) => FormError[]>, default: () => [] } }, @@ -111,27 +116,18 @@ function isZodSchema (schema: any): schema is ZodSchema { return schema.parse !== undefined } -function isZodError (error: any): error is ZodError { - return error.issues !== undefined -} - async function getZodErrors ( state: any, schema: ZodSchema ): Promise { - try { - schema.parse(state) - return [] - } catch (error) { - if (isZodError(error)) { - return error.issues.map((issue) => ({ - path: issue.path.join('.'), - message: issue.message - })) - } else { - throw error - } + const result = await schema.safeParseAsync(state) + if (result.success === false) { + return result.error.issues.map((issue) => ({ + path: issue.path.join('.'), + message: issue.message + })) } + return [] } function isJoiSchema (schema: any): schema is JoiSchema {