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 2 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
2 changes: 2 additions & 0 deletions library/src/methods/getFallbacks/getFallbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
)
Expand All @@ -65,6 +66,7 @@ describe('getFallbacks', () => {
key2: 123,
key3: false,
},
objectWithFallback: { foo: "bar" },
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's and object schema and it has a fallback, return it
if (schema.type === 'object' && 'fallback' in schema) {
Copy link
Owner

@fabian-hiller fabian-hiller Apr 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it enough to just check for 'fallback' in schema? This way we make it the default behaviour for any schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, could be done that way too.

But probably this if clause can be removed, and do this when retrieving fallbacks of an object -> #1155 (comment)

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> = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, it would be like this, however fallback requires all values to be defined.

Suggested change
// 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<TSchema>;
}
// If it is an object schema, return fallbacks of entries
if ('entries' in schema) {
const object: Record<string, unknown> = {};
// If it is an object schema, return fallbacks of entries
// If an object itself has a fallback, use it
if ('entries' in schema) {
const object: Record<string, unknown> = schema.type === 'object' && 'fallback' in schema ? schema.fallback : {};

eg.

const ObjectWithFallbackSchema = fallback(object({
  foo: number(),
  bar: fallback(number(), 5)
}), {
  foo: 3
});

I would expect bar to not be required, because it itself has a fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be achieve by adjusting Fallback type to

/**
 * Fallback type.
 */
export type Fallback<
  TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>,
  TFallbackSchema = 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>>
    ) => ({
      [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];
    }));

but that's whole other discussion

Expand Down