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)
- Allow errorMap (like zod) message for literal HOT 11
- `intersect` of `object` and `record` does not work like Typescript HOT 9
- using `union` as key of `record` adds `undefined` to the value type HOT 13
- `isoTimestamp` only accepts UTC timestamps HOT 3
- Not able to validate file uploads on the backend, even though it works on the frontend HOT 4
- Search fails to work HOT 1
- Timestamp regex doesn't support timezone other than Zulu HOT 1
- Can't use Valibot with H3 Data validation HOT 6
- optional string field with string min/max options set wrong meta in veevalidate HOT 2
- How to conditionally require object with all its fields HOT 2
- `<select>` custom validation inside a `variant` schema HOT 4
- Function to support for json type HOT 2
- Number literal don't trigger variant validation HOT 7
- `_addIssue` blocks `every`, `some` & `check` actions rewrite due to a possible type issue HOT 2
- Email regex pattern does not allow all valid characters HOT 1
- Are there any similarities about zod `.superRefine`? HOT 1
- Error when building HOT 6
- How to access other field? HOT 2
- Allow null default value while enforcing required validation HOT 1
- Force typeing on object schema HOT 2
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.