-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
[Bug] Merge/Transform make all properties in object to optional field #40
Comments
I have checked this and for me only the properties |
Sure, I combine two models together export const baseSchema = object({
createdDate: optional(string()),
updatedDate: optional(string()),
}); export enum MenuLinkType {
INTERNAL = "INTERNAL",
EXTERNAL = "EXTERNAL",
}
export enum MenuType {
NORMAL = "NORMAL",
PROMO = "PROMO",
NEWS = "NEWS",
}
export enum MenuLocation {
HEADER = "HEADER",
FOOTER = "FOOTER",
SIDEBAR = "SIDEBAR",
}
export const createMenuSchema = merge([
transform(
object({
description: optional(string()),
endIcon: optional(string()),
isActive: optional(useDefault(boolean(), true)),
link: string(),
linkType: nativeEnum(MenuLinkType),
location: nativeEnum(MenuLocation),
order: number(),
parentId: optional(string()),
startIcon: optional(string()),
title: string(),
type: nativeEnum(MenuType),
}),
(menu) => ({ ...menu, slug: slugify(menu.title) }),
),
baseSchema,
]);
export type MenuModel = Output<typeof createMenuSchema>;
Hope you can check this. |
Thank you for the details. This is an expected behavior. Please read through issue #26 and let me know what you think. The important info is in these comments: |
Thank you for your quick response, I am not good in technical but can contribute to you the user experience from my point of view.
|
Thank you for your feedback. As far as I know, Zod does not allow to merge a transformed object and this can be technically justified. With Furthermore, when merging, Valibot allows overwriting data types if the same key occurs in multiple objects. This is another reason why To make it short. If |
Hmm, can you suggest to me how to solve this problem with Valibot? Bcs after using Valibot, I use this with Zod --> It worked as I expected. The code below for you can try 😆. export const createMenuSchema = z
.object({
description: z.string().optional(),
endIcon: z.string().optional(),
isActive: z.boolean().optional().default(true),
link: z.string(),
linkType: z.nativeEnum(MenuLinkType),
location: z.nativeEnum(MenuLocation),
order: z.number(),
parentId: z.string().optional(),
startIcon: z.string().optional(),
title: z.string(),
type: z.nativeEnum(MenuType),
})
.transform((menu) => ({ ...menu, slug: slugify(menu.title) }))
.and(baseSchema);
export type MenuModel = z.infer<typeof createMenuSchema>; |
Simply apply the transformation to the merged object scheme. const createMenuSchema = transform(
merge([
object({
description: optional(string()),
endIcon: optional(string()),
isActive: optional(useDefault(boolean(), true)),
link: string(),
linkType: nativeEnum(MenuLinkType),
location: nativeEnum(MenuLocation),
order: number(),
parentId: optional(string()),
startIcon: optional(string()),
title: string(),
type: nativeEnum(MenuType),
}),
baseSchema,
]),
(menu) => ({ ...menu, slug: slugify(menu.title) })
); |
export const createMenuSchema = transform(
merge([
object({
link: string(),
title: string(),
order: number(),
linkType: nativeEnum(MenuLinkType),
location: nativeEnum(MenuLocation),
type: nativeEnum(MenuType),
description: optional(string()),
endIcon: optional(string()),
isActive: optional(useDefault(boolean(), true)),
parentId: optional(string()),
startIcon: optional(string()),
}),
baseSchema,
]),
(menu) => ({ ...menu, slug: slugify(menu.title) }),
);
export type MenuModel = Output<typeof createMenuSchema>; export const tenantSchema = object({
id: number(),
name: string([minLength(1)]),
description: string(),
});
export type TenantModel = Output<typeof tenantSchema>; Hmm, really weird! Now anything turns to optional(valibot v 0.8.0) |
For me your code works without problems and the types are displayed correctly. Does |
Ohhh - It worked! Thank you for your quick response! This should be in the document 😆. |
Yes, I will improve the docs here. Thank you for your feedback! |
Added the info to the docs. Thanks! |
Hi @fabian-hiller, I have an object like this
But when I wrap this object with transform or merge --> All properties mark optional, might be this is an expected behavior or a bug 🤔. Hope you can check this!
The text was updated successfully, but these errors were encountered: