diff --git a/src/lib/schemaEntity.ts b/src/lib/schemaEntity.ts index 355bccba..60f6cca1 100644 --- a/src/lib/schemaEntity.ts +++ b/src/lib/schemaEntity.ts @@ -214,7 +214,6 @@ function schemaInfo(schema: T) { export function valueOrDefault( value: unknown, strict: boolean, - implicitDefaults: true, schemaInfo: ZodTypeInfo ) { if (value) return value; @@ -235,21 +234,19 @@ export function valueOrDefault( if (isNullable) return null; if (isOptional) return undefined; - if (implicitDefaults) { - if (zodType._def.typeName == 'ZodString') return ''; - if (zodType._def.typeName == 'ZodNumber') return 0; - if (zodType._def.typeName == 'ZodBoolean') return false; - // Cannot add default for ZodDate due to https://github.com/Rich-Harris/devalue/issues/51 - //if (zodType._def.typeName == "ZodDate") return new Date(NaN); - if (zodType._def.typeName == 'ZodArray') return []; - if (zodType._def.typeName == 'ZodObject') { - return defaultValues(zodType as AnyZodObject); - } - if (zodType._def.typeName == 'ZodSet') return new Set(); - if (zodType._def.typeName == 'ZodRecord') return {}; - if (zodType._def.typeName == 'ZodBigInt') return BigInt(0); - if (zodType._def.typeName == 'ZodSymbol') return Symbol(); + if (zodType._def.typeName == 'ZodString') return ''; + if (zodType._def.typeName == 'ZodNumber') return 0; + if (zodType._def.typeName == 'ZodBoolean') return false; + // Cannot add default for ZodDate due to https://github.com/Rich-Harris/devalue/issues/51 + //if (zodType._def.typeName == "ZodDate") return new Date(NaN); + if (zodType._def.typeName == 'ZodArray') return []; + if (zodType._def.typeName == 'ZodObject') { + return defaultValues(zodType as AnyZodObject); } + if (zodType._def.typeName == 'ZodSet') return new Set(); + if (zodType._def.typeName == 'ZodRecord') return {}; + if (zodType._def.typeName == 'ZodBigInt') return BigInt(0); + if (zodType._def.typeName == 'ZodSymbol') return Symbol(); return undefined; } @@ -279,7 +276,7 @@ export function defaultValues>( return Object.fromEntries( fields.map((field) => { const typeInfo = schemaTypeInfo[field]; - const newValue = valueOrDefault(undefined, false, true, typeInfo); + const newValue = valueOrDefault(undefined, false, typeInfo); return [field, newValue]; }) diff --git a/src/lib/superValidate.ts b/src/lib/superValidate.ts index 5ecf7dea..ffefcc78 100644 --- a/src/lib/superValidate.ts +++ b/src/lib/superValidate.ts @@ -198,7 +198,7 @@ function formDataToValidation( value: string | null, typeInfo: ZodTypeInfo ): unknown { - const newValue = valueOrDefault(value, strict ?? false, true, typeInfo); + const newValue = valueOrDefault(value, strict ?? false, typeInfo); const zodType = typeInfo.zodType; // If the value was empty, it now contains the default value, @@ -437,10 +437,10 @@ function validateResult( let data; - if (zodKeyStatus == 'passthrough') { - data = { ...clone(entityInfo.defaultEntity), ...partialData } - } else if (options.strict) { - data= parsed.data; + if (options.strict) { + data = parsed.data; + } else if (zodKeyStatus == 'passthrough') { + data = { ...clone(entityInfo.defaultEntity), ...partialData }; } else { data = Object.fromEntries( schemaKeys.map((key) => [ @@ -629,7 +629,7 @@ export async function superValidate< const { parsed, result } = await parseRequest(); if (options?.strict) { - for (const key of Object.keys(parsed.data ?? {})) { + for (const key of Object.keys(parsed.data ?? {})) { const isKeyInSchema = schemaData.schemaKeys.includes(key); if (!isKeyInSchema && parsed.data) { delete parsed.data[key];