diff --git a/package.json b/package.json index 20918e9..bc9115e 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "credits": "npx @opengovsg/credits-generator" }, "peerDependencies": { - "@volverjs/ui-vue": "0.0.10-beta.36", + "@volverjs/ui-vue": "0.0.10-beta.41", "@vueuse/core": "^10.11.0", "ts-dot-prop": "^2.1.4", "vue": "^3.4.29", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3ccfd19..f59225b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@volverjs/ui-vue': - specifier: 0.0.10-beta.36 - version: 0.0.10-beta.36(@volverjs/style@0.1.12-beta.15)(typescript@5.4.5)(vitest@1.6.0(@types/node@20.14.2)(happy-dom@14.12.0)(jsdom@24.1.0)) + specifier: 0.0.10-beta.41 + version: 0.0.10-beta.41(@volverjs/style@0.1.12-beta.15)(typescript@5.4.5)(vitest@1.6.0(@types/node@20.14.2)(happy-dom@14.12.0)(jsdom@24.1.0)) '@vueuse/core': specifier: ^10.11.0 version: 10.11.0(vue@3.4.29(typescript@5.4.5)) @@ -835,8 +835,8 @@ packages: resolution: {integrity: sha512-hZH382STKlF9V5L1zHb78vA5/8JkgM7EnJTwqrVfcqiyn/h0T0m3N+IXsbJLh7MLGTzc9em1z0rcP21vukAE9g==} engines: {node: '>= 12.x'} - '@volverjs/ui-vue@0.0.10-beta.36': - resolution: {integrity: sha512-T0a/sJXoSmSzAuid3Xp5z1pQDvRh0OKGiINPV5UXsPgBz7+YP6HzdpaegiopFH0WmIHcu0+p21ziArTSIW78eA==} + '@volverjs/ui-vue@0.0.10-beta.41': + resolution: {integrity: sha512-PXMHFCtC5OoqDn5n5rIx6GVf6UJGB5SnjvW5FRFqz3clVKYNsbXLSsJFzu/86/rplz7jN3/xOa0/RUGOpmMifg==} hasBin: true peerDependencies: '@volverjs/style': 0.* @@ -4194,7 +4194,7 @@ snapshots: '@volverjs/style@0.1.12-beta.15': {} - '@volverjs/ui-vue@0.0.10-beta.36(@volverjs/style@0.1.12-beta.15)(typescript@5.4.5)(vitest@1.6.0(@types/node@20.14.2)(happy-dom@14.12.0)(jsdom@24.1.0))': + '@volverjs/ui-vue@0.0.10-beta.41(@volverjs/style@0.1.12-beta.15)(typescript@5.4.5)(vitest@1.6.0(@types/node@20.14.2)(happy-dom@14.12.0)(jsdom@24.1.0))': dependencies: '@floating-ui/vue': 1.0.6(vue@3.4.29(typescript@5.4.5)) '@iconify/tools': 4.0.4 diff --git a/src/utils.ts b/src/utils.ts index 2fc4bf4..279e6e7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -14,7 +14,7 @@ import { import type { FormSchema } from './types' export function defaultObjectBySchema(schema: Schema, original: Partial> & Record = {}): Partial> { - const getInnerType = ( + const getSchemaInnerType = ( schema: | Type | ZodEffects @@ -30,7 +30,23 @@ export function defaultObjectBySchema(schema: Schema, } return toReturn } - const innerType = getInnerType(schema) + const isSchemaOptional = ( + schema: + | Type + | ZodEffects + | ZodEffects> + | ZodOptional, + ) => { + let toReturn = schema + while (toReturn instanceof ZodEffects) { + toReturn = toReturn.innerType() + } + if (toReturn instanceof ZodOptional) { + return true + } + return false + } + const innerType = getSchemaInnerType(schema) const unknownKeys = innerType instanceof ZodObject ? innerType._def.unknownKeys === 'passthrough' @@ -41,7 +57,8 @@ export function defaultObjectBySchema(schema: Schema, (Object.entries(innerType.shape) as [string, ZodTypeAny][]).map( ([key, subSchema]) => { const originalValue = original[key] - let innerType = getInnerType(subSchema) + const isOptional = isSchemaOptional(subSchema) + let innerType = getSchemaInnerType(subSchema) let defaultValue: Partial> | undefined if (innerType instanceof ZodDefault) { defaultValue = innerType._def.defaultValue() @@ -53,6 +70,9 @@ export function defaultObjectBySchema(schema: Schema, ) { return [key, originalValue] } + if ((originalValue === undefined || originalValue === null) && isOptional) { + return [key, defaultValue] + } if (innerType instanceof ZodSchema) { const parse = subSchema.safeParse(originalValue) if (parse.success) { @@ -64,7 +84,7 @@ export function defaultObjectBySchema(schema: Schema, && Array.isArray(originalValue) && originalValue.length ) { - const arrayType = getInnerType(innerType._def.type) + const arrayType = getSchemaInnerType(innerType._def.type) if (arrayType instanceof ZodObject) { return [ key, @@ -82,7 +102,7 @@ export function defaultObjectBySchema(schema: Schema, } } if (innerType instanceof ZodRecord && originalValue) { - const valueType = getInnerType(innerType._def.valueType) + const valueType = getSchemaInnerType(innerType._def.valueType) if (valueType instanceof ZodObject) { return [key, Object.keys(originalValue).reduce((acc: Record, recordKey: string) => { acc[recordKey] = defaultObjectBySchema(valueType, originalValue[recordKey])