Skip to content

fix: getFallbacks use fallback value of an object #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions library/src/methods/fallback/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ import { getFallback } from '../getFallback/index.ts';
*/
export type Fallback<
TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>,
TFallbackSchema = MaybeReadonly<InferOutput<TSchema>>
> =
| MaybeReadonly<InferOutput<TSchema>>
| {
[key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback<any, any> ? never : key]: TFallbackSchema[key];
} & {
[key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback<any, any> ? key : never]?: TFallbackSchema[key];
}
| ((
dataset?: OutputDataset<InferOutput<TSchema>, InferIssue<TSchema>>,
config?: Config<InferIssue<TSchema>>
) => MaybeReadonly<InferOutput<TSchema>>);
) => ({
[key in keyof TFallbackSchema]: TFallbackSchema[key] extends SchemaWithFallback<any, any>
? TFallbackSchema[key]
: TFallbackSchema[key];
} & {
[key in keyof TFallbackSchema as TFallbackSchema[key] extends SchemaWithFallback<any, any> ? key : never]?: TFallbackSchema[key];
}));

/**
* Schema with fallback type.
Expand Down
9 changes: 9 additions & 0 deletions library/src/methods/getFallbacks/getFallbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
boolean,
number,
object,
optional,
strictObject,
strictTuple,
string,
Expand Down Expand Up @@ -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(),
})
)
Expand All @@ -65,6 +73,7 @@ describe('getFallbacks', () => {
key2: 123,
key3: false,
},
objectWithFallback: { foo: 3 },
other: undefined,
});
});
Expand Down
6 changes: 6 additions & 0 deletions library/src/methods/getFallbacks/getFallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export function getFallbacks<
ErrorMessage<TupleWithRestIssue> | undefined
>,
>(schema: TSchema): InferFallbacks<TSchema> {

// If it has a fallback, return it.
if ('fallback' in schema) {
return schema.fallback as InferFallbacks<TSchema>;
}

// If it is an object schema, return fallbacks of entries
if ('entries' in schema) {
const object: Record<string, unknown> = {};
Expand Down