Skip to content

Fields depending on other fields #954

Answered by lorefnon
Southclaws asked this question in Q&A
Discussion options

You must be logged in to vote

You can express the second example in zod as follows:

const CompanyFields = z.object({ companyName: z.string(), companyNumber: z.string() })

const GeneralFields = z.object({ firstName: z.string(), lastName: z.string() })

const IndividualFields = z.object({ entity: z.literal("individual") }).and(GeneralFields)

const BusinessFields = z.object({ entity: z.literal("business") }).and(CompanyFields)

const Fields = IndividualFields.or(BusinessFields)

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
5 replies
@JacobWeisenburger
Comment options

@whengely
Comment options

@JacobWeisenburger
Comment options

@JacobWeisenburger
Comment options

@whengely
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #890 on February 21, 2022 20:04.