question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello,

I’m giving a try of your lib in my project, and it looks quite nice.

I have some questions, sorry if this is not the best place to do it, but I didn’t find a tag in stackoverflow…

1 - Can I use this lib in the backend and frontend, or just in one of two?

2 - I’m trying to do the following rule: The bank property (an object) of an object (a React form) is required. But I don’t care about bank properties, just if it is informed (not undefined) or not, because it comes from an autocomplete component.

I’m doing like this:

z.object({
  bank: z
    .object({
      id: z.union([z.number(), z.string()])
    })
.nonstrict(),

Is this the best way?

3 - How to extend this lib with custom methods? For example, a string validation method to validate CPF (a SSN like number in my country). I did a refine and it worked well, but I’m thinking about extending the schema, like this:

z.string().cpf()

Can I just reexport zod with my custom methods like this:

const CustomZod = {
  string: () =>
    z.string().refine(
      arg => validateCPF(arg),
      { message: "CPF inválido" }
    )
};

Is this the best way?

4 - I’m using Formik, and in Formik the validation function receives an object of values and must return an object with the error messages, one message for each key. I’m doing like this:

const validateZod = (values: unknown): object => {
    const parsingResult = props.validationSchema.safeParse(values);

    if (parsingResult.success) {
      return {};
    }

    const flattenedErrors = parsingResult.error.flatten();

    const errors = {};

    for (const [key, value] of Object.entries(flattenedErrors.fieldErrors)) {
      errors[key] = value[0];
    }

    return errors;
  };

Is this the best way?

5 - I want to add a functionality to add asterisks to required fields in Formik. So basically I would need to read the Zod schema passed to form, and check what fields of schema are required to put the asterisk. Basically a reverse engineering of sorts. How can I do that with Zod?

Thanks a lot.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
colinhackscommented, Sep 16, 2020

@julianomqs @tmtron just thought of a better isOptional implementation!

const isOptional = (schema: z.ZodType<any, any>): boolean => schema.safeParse(undefined).success;

So might be not be worth putting into core after all. 😛

1reaction
julianomqscommented, Sep 11, 2020

Thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

100 Getting to Know You Questions - SignUpGenius
100 Getting to Know You Questions · Who is your hero? · If you could live anywhere, where would it be? · What...
Read more >
450 Fun Questions to Ask People in ANY Situation (That Work!)
Deep Questions to Ask People · Who knows you best? · Where do you see yourself in 10 years? · What makes you...
Read more >
500 Good Questions to Ask - Conversation Starters World
GOOD QUESTIONS TO ASK · What weird food combinations do you really enjoy? · What social stigma does society need to get over?...
Read more >
400 Fun Questions to Ask People (Friends, Family, Strangers)
400 Wacky, Wild & Totally Fun Questions to Ask Anyone—Including Friends, Family & Even Strangers! Find a good, interesting, and random question ......
Read more >
272 Deep Questions to Ask: A Guy, Girl, Friend, or Anyone
One way is to ask them deep questions. So here are some deep questions you can ask different people--people like your partner, friends,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found