Skip to content
Merged
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
29 changes: 13 additions & 16 deletions src/lib/schemaEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ function schemaInfo<T extends AnyZodObject>(schema: T) {
export function valueOrDefault(
value: unknown,
strict: boolean,
implicitDefaults: true,
schemaInfo: ZodTypeInfo
) {
if (value) return value;
Expand All @@ -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;
}
Expand Down Expand Up @@ -279,7 +276,7 @@ export function defaultValues<T extends ZodValidation<AnyZodObject>>(
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];
})
Expand Down
12 changes: 6 additions & 6 deletions src/lib/superValidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function formDataToValidation<T extends AnyZodObject>(
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,
Expand Down Expand Up @@ -437,10 +437,10 @@ function validateResult<T extends AnyZodObject, M>(

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) => [
Expand Down Expand Up @@ -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];
Expand Down