From 6bddaf9d27e6d0a72e67f86390f612047e9c1e9d Mon Sep 17 00:00:00 2001 From: Rokas Muningis <28229273+muningis@users.noreply.github.com> Date: Sat, 12 Apr 2025 22:20:26 +0300 Subject: [PATCH 1/3] fix: getFallbacks use fallback value of an object --- library/src/methods/getFallbacks/getFallbacks.test.ts | 4 +++- library/src/methods/getFallbacks/getFallbacks.ts | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/library/src/methods/getFallbacks/getFallbacks.test.ts b/library/src/methods/getFallbacks/getFallbacks.test.ts index a14182a2c..85f6112b3 100644 --- a/library/src/methods/getFallbacks/getFallbacks.test.ts +++ b/library/src/methods/getFallbacks/getFallbacks.test.ts @@ -47,7 +47,7 @@ describe('getFallbacks', () => { }); }); - test('for nested object', () => { + test.only('for nested object', () => { expect( getFallbacks( object({ @@ -56,6 +56,7 @@ describe('getFallbacks', () => { key2: fallback(number(), () => 123 as const), key3: fallback(boolean(), false as const), }), + objectWithFallback: fallback(object({ foo: string() }), { foo: "bar" }), other: string(), }) ) @@ -65,6 +66,7 @@ describe('getFallbacks', () => { key2: 123, key3: false, }, + objectWithFallback: { foo: "bar" }, other: undefined, }); }); diff --git a/library/src/methods/getFallbacks/getFallbacks.ts b/library/src/methods/getFallbacks/getFallbacks.ts index afcc9995f..8f3f5b784 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's and object schema and it has a fallback, return it + if (schema.type === 'object' && '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 = {}; From aa1ad6698154791e502d7910302029f60b193502 Mon Sep 17 00:00:00 2001 From: Rokas Muningis <28229273+muningis@users.noreply.github.com> Date: Sat, 12 Apr 2025 22:27:43 +0300 Subject: [PATCH 2/3] test: re-enable other tests --- library/src/methods/getFallbacks/getFallbacks.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/methods/getFallbacks/getFallbacks.test.ts b/library/src/methods/getFallbacks/getFallbacks.test.ts index 85f6112b3..31a41ddea 100644 --- a/library/src/methods/getFallbacks/getFallbacks.test.ts +++ b/library/src/methods/getFallbacks/getFallbacks.test.ts @@ -47,7 +47,7 @@ describe('getFallbacks', () => { }); }); - test.only('for nested object', () => { + test('for nested object', () => { expect( getFallbacks( object({ From e22d40a050c87a443b65a868a1356acfbd217eba Mon Sep 17 00:00:00 2001 From: Rokas Muningis <28229273+muningis@users.noreply.github.com> Date: Sun, 13 Apr 2025 10:12:30 +0300 Subject: [PATCH 3/3] fix: simplify check --- library/src/methods/fallback/fallback.ts | 15 +++++++++++++-- .../src/methods/getFallbacks/getFallbacks.test.ts | 11 +++++++++-- library/src/methods/getFallbacks/getFallbacks.ts | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) 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 31a41ddea..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,7 +57,13 @@ describe('getFallbacks', () => { key2: fallback(number(), () => 123 as const), key3: fallback(boolean(), false as const), }), - objectWithFallback: fallback(object({ foo: string() }), { foo: "bar" }), + objectWithFallback: fallback(object({ + foo: number(), + bar: fallback(number(), 5) + + }), { + foo: 3 + }), other: string(), }) ) @@ -66,7 +73,7 @@ describe('getFallbacks', () => { key2: 123, key3: false, }, - objectWithFallback: { foo: "bar" }, + objectWithFallback: { foo: 3 }, other: undefined, }); }); diff --git a/library/src/methods/getFallbacks/getFallbacks.ts b/library/src/methods/getFallbacks/getFallbacks.ts index 8f3f5b784..a47d6700e 100644 --- a/library/src/methods/getFallbacks/getFallbacks.ts +++ b/library/src/methods/getFallbacks/getFallbacks.ts @@ -65,8 +65,8 @@ export function getFallbacks< >, >(schema: TSchema): InferFallbacks { - // If it's and object schema and it has a fallback, return it - if (schema.type === 'object' && 'fallback' in schema) { + // If it has a fallback, return it. + if ('fallback' in schema) { return schema.fallback as InferFallbacks; }