-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) { | ||||||||||||||||||||||||||||
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> = {}; | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, it would be like this, however
Suggested change
eg. const ObjectWithFallbackSchema = fallback(object({
foo: number(),
bar: fallback(number(), 5)
}), {
foo: 3
}); I would expect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can be achieve by adjusting /**
* 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 |
||||||||||||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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)