Next.js Discord

Discord Forum

zod schema issue

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
i have a form where some values depend from others (such as: if you trigger a button, other option will appear and you have to select one of those) and im struggling to create a schema for this type of form. does anybody have a solution? thank you
Answered by Arinji
const schema = z.object({
  categories: z.string(),
  subCategories: z.string(),
}).refine(data => data.categories === "partners" && data.subCategories === "partners", {
  message: "Invalid categories or subCategories"
});
View full answer

55 Replies

@American black bear make the schema have undefined or values
lemme show some examples
@Arinji lemme show some examples
American black bearOP
thank you
const schema = {
  canBeUndefined: z.string().optional(),
  isPresent: z.string(),
}
@American black bear
American black bearOP
the main issue i have is that if the user triggers the switch, 2 options come up and he needs to choose one of the two options, none of them could be false, either the first, the second or both need to be true.
and i cant seem to get the schema to do this @Arinji
so like
const var: false | true | undefined = undefined

etc
I've sent you a DM
@Norwegian Forest Cat Hi
make a forum post, i dont check dms
i have too many from ppl on this discord xD
@Arinji make a forum post, i dont check dms
Norwegian Forest Cat
but its not related to any issue
@Norwegian Forest Cat I've sent you a DM
American black bearOP
please if you dont contribute to the thread can you talk to him in #off-topic? thanks :)
only saying so cause ill get notifications from the thread even if they are not related to my issue
agreed
American Crow
I think the op is talking about dynamic schemas
think of category: fashion | car
and subcategory depends on what was chosen in category
e.g. t-shirt | sweater if category === fashion and bmw | audi if car.

Which is not possible in Typescript since that would be at runtime.
Only way I know of to do that would be zod's .refine
@Arinji agreed
Norwegian Forest Cat
I'm talking about if u want to join my dev team
@Norwegian Forest Cat I'm talking about if u want to join my dev team
sorry, no. Better off asking in off topic
@Arinji sorry, no. Better off asking in off topic
Norwegian Forest Cat
Ok
@Arinji <@489852928341835776> can you confirm if this is what you need
American black bearOP
yes thats what im looking for
thank you @American Crow
ok yea then use refines, lemme show how i use them
@Arinji ok yea then use refines, lemme show how i use them
American black bearOP
thank you very much
displayLink: z
    .string()
    .min(
      CONSTRAINTS.PLAYLIST.MIN_LENGTH,
      `Display Link must be ${CONSTRAINTS.PLAYLIST.MAX_LENGTH} long`
    )
    .max(
      CONSTRAINTS.PLAYLIST.MAX_LENGTH,
      `Display Link must be ${CONSTRAINTS.PLAYLIST.MAX_LENGTH} long`
    )
    .refine((data) => !data.startsWith("vibeify.arinji.com"), {
      message: "Display link cannot start with vibeify.arinji.com",
      path: ["displayLink"],
    }),


ok so focus on the refine here
so basically i check that the url input by the user, dosent start with my domain.
so what you would do, is instead of putting them under like categories.. you put it under the object directly
its even easier for me i think cause all of my form is booleans basically
at least this part
const schema = z.object({
  categories: z.string(),
  subCategories: z.string(),
}).refine(data => data.categories === "partners" && data.subCategories === "partners", {
  message: "Invalid categories or subCategories"
});
Answer
this is an example of if you chose a category partners, you can only chose a subcategory of partners
makes sense?
American Crow
First argument being the validation function. Truthy values will pass the validation
Second argument is for error-handling

(just for completeness)
American black bearOP
kinda made a clearer rappresentation
not sure if it helps
@American black bear kinda made a clearer rappresentation
looks fine to me, refine works to do this
do you want to show errors?
or do you want to change the data in your schema
based on their options selected
@Arinji do you want to show errors?
American black bearOP
yes i wanna show errors, if a user dosent set true either "Switch2" or "Switch3" i want to show errors
Norwegian Forest Cat
Why I’m I talking here
Why was I even in this place
I just realised it’s help forum
American black bearOP
:meow_stare:
American Crow
good thing that guy got a new user badge..
Anyways give the mark answer to @Arinji if you have no further questions

I am out eating bb
yea lol
thank you everybody that helped!