Comments (12)
Yes, I will improve the docs here. Thank you for your feedback!
from valibot.
I have checked this and for me only the properties description
and isActive
are optional. Can you send me your whole schema code?
from valibot.
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 transform
, the object can be transformed to any other data type. For example a JSON string. If Valibot or Zod have no guarantee about the outgoing data, they must either discard the transformation like Valibot or prevent merging completely like Zod.
Furthermore, when merging, Valibot allows overwriting data types if the same key occurs in multiple objects. This is another reason why transform
cannot be executed in this case, since otherwise incorrect data in transform
or the pipeline is expected.
To make it short. If merge
or another object method is applied, transform
, as well as the pipeline must be added to the merged schema all over again. I will try to document this better in the long run.
from valibot.
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) })
);
from valibot.
For me your code works without problems and the types are displayed correctly. Does strict
in the tsconfig.json
have the value true
?
from valibot.
Added the info to the docs. Thanks!
from valibot.
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>;
- All properties will mark as optional
- The new model after transform does not contain the
slug
field 🤔
Hope you can check this.
from valibot.
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:
from valibot.
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.
- I don't know how to execute the pipe of function in order with valibot. Might be when you execute inside to outside, the result may be correct like the expected result: Remain the object type --> transform with slug --> merge with another schema.
- I might think this stuff is really hard technically, but merge and transform it into basic things which anyone using
zod
did. I guess Zod has a trade-off to do the complicated kinds of stuff like this about bundle size or performances. - If you think this was expected behavior, I think you should talk with more people using this and Zod on their production, might you can find the solutions 😆.
from valibot.
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>;
from valibot.
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)
from valibot.
Ohhh - It worked! Thank you for your quick response! This should be in the document 😆.
from valibot.
Related Issues (20)
- The forwardAsync method is not available HOT 3
- 0.28.0 bug: when inside union, transform is not returning the transformed result HOT 2
- Avoid barrel files and create distinct files for exports HOT 6
- Best way for validation optional strings HOT 11
- Docs: Add ctrl + k hotkey for search to enable searching on windows (cmd is reserved) HOT 10
- Method of modifying input type to account for acceptable invalid inputs HOT 5
- Transformation action now executes after failed schema check HOT 41
- [i18n] Add Dutch HOT 5
- Enable `v.recursive()` getter to accept a `value` to improving usage flexibility HOT 10
- Schema with interpolated value fails HOT 5
- [docs] Command + K doesn't prevent default, opening the URL bar in Firefox HOT 2
- Tree shaking is not performed on `RegExp` in Webpack HOT 4
- Custom validator: what to put in expect and actionIssue arguments HOT 2
- Add possibility to convert entries to input types if possible HOT 1
- Nullable form field with constraints HOT 4
- flatten return type/FlatErrors type is incorrect for object schemas that contain properties with async schemas HOT 4
- Request for Enhancement: Support for Multiple Keys in variant Function HOT 3
- Derived Schema Validations? HOT 1
- Partial variant schema HOT 1
- Can't get `instance` to work HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.