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.

Obtain defaults even on failed validation

See original GitHub issue

Is there a way to obtain the defaults objects even after validation failed: I tried:

const schema = z.object({
  name: z.string(),
  age: z.number().default(3),
});
const schema2 = z.object({
  name: z.string(),
  age: z.number().default(3),
  arr: z
    .array(
      z.object({
        number: z.string(),
        type: z.string().default("cell"),
      })
    )
    .nonempty()
    .default([{}] as any),
});

console.log(schema.deepPartial().default({}).parse(undefined));
// This is ok, returns { age: 3 }
console.log(schema2.deepPartial().default({}).parse(undefined));
// This is not ok, returns { age: 3 } but wanted { age: 3, arr: [{ type:'cell' }] }

Is there a way to actually safe parse so defaults get filled and then validate for real?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tomocraftercommented, Mar 13, 2022

@juni0r I gave up with defining constant default value for each object. I also want good way to achive this…

0reactions
ifightcrimecommented, Sep 8, 2022

Interesting. It looks like #352 is close to what I’m looking for, but from the first comment on this issue it sounds like some of this should work out of the box when using .partial()?

console.log(schema.deepPartial().default({}).parse(undefined)); // This is ok, returns { age: 3 }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed validation returns default error message even though ...
Laravel's validation throws a Illuminate\Validation\ValidationError. Believe it or not, this actually defaults the error message to "The given ...
Read more >
Class property validation failure due to implicit instantiation ...
Ensure that any default value [including implicit defaults] assigned to the property meets the restrictions imposed by the specified validation. I don't think ......
Read more >
Resolve the "Parameter validation failed" error in AWS ...
When I create or update my AWS CloudFormation stack, I get the following error: "Parameter validation failed: parameter value 'abc' for ...
Read more >
Avoid Default Field Validation - Adrian Roselli
Using required on its own will not identify to all users in plain text that a field is required. You will still need...
Read more >
Don't Rely on Default Browser Error Messages - Intopia
The default error validation in browsers is almost completely inaccessible. I was hoping we'd get default “you've forgotten to fill this in” ...
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