diff --git a/library/src/methods/fallback/fallback.ts b/library/src/methods/fallback/fallback.ts index a57689fb3..c6096f0c9 100644 --- a/library/src/methods/fallback/fallback.ts +++ b/library/src/methods/fallback/fallback.ts @@ -15,12 +15,23 @@ import { getFallback } from '../getFallback/index.ts'; */ export type Fallback< TSchema extends BaseSchema>, + TFallbackSchema = MaybeReadonly> > = - | MaybeReadonly> + | { + [key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback ? never : key]: TFallbackSchema[key]; + } & { + [key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback ? key : never]?: TFallbackSchema[key]; + } | (( dataset?: OutputDataset, InferIssue>, config?: Config> - ) => MaybeReadonly>); + ) => ({ + [key in keyof TFallbackSchema]: TFallbackSchema[key] extends SchemaWithFallback + ? TFallbackSchema[key] + : TFallbackSchema[key]; + } & { + [key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback ? key : never]?: TFallbackSchema[key]; + })); /** * Schema with fallback type. diff --git a/library/src/methods/getFallbacks/getFallbacks.test.ts b/library/src/methods/getFallbacks/getFallbacks.test.ts index a14182a2c..2fc2fafb9 100644 --- a/library/src/methods/getFallbacks/getFallbacks.test.ts +++ b/library/src/methods/getFallbacks/getFallbacks.test.ts @@ -3,6 +3,7 @@ import { boolean, number, object, + optional, strictObject, strictTuple, string, @@ -56,6 +57,13 @@ describe('getFallbacks', () => { key2: fallback(number(), () => 123 as const), key3: fallback(boolean(), false as const), }), + objectWithFallback: fallback(object({ + foo: number(), + bar: fallback(number(), 5) + + }), { + foo: 3 + }), other: string(), }) ) @@ -65,6 +73,7 @@ describe('getFallbacks', () => { key2: 123, key3: false, }, + objectWithFallback: { foo: 3 }, other: undefined, }); }); diff --git a/library/src/methods/getFallbacks/getFallbacks.ts b/library/src/methods/getFallbacks/getFallbacks.ts index afcc9995f..a47d6700e 100644 --- a/library/src/methods/getFallbacks/getFallbacks.ts +++ b/library/src/methods/getFallbacks/getFallbacks.ts @@ -64,6 +64,12 @@ export function getFallbacks< ErrorMessage | undefined >, >(schema: TSchema): InferFallbacks { + + // If it has a fallback, return it. + if ('fallback' in schema) { + return schema.fallback as InferFallbacks; + } + // If it is an object schema, return fallbacks of entries if ('entries' in schema) { const object: Record = {};