Obtain defaults even on failed validation
See original GitHub issueIs 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:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@juni0r I gave up with defining constant default value for each object. I also want good way to achive this…
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()
?