Comments (10)
Before the if(!parsedData.data)
statement, the type of parsedData
can either be SafeParseSuccess
or SafeParseError
. However, after the if statement, it is resolved to SafeParseSuccess
, which means its error
property is undefined
. Therefore, in parsedData.error && console.error(parsedData.error)
, it's console
will not execute because the precondition is false.
from zod.
Thanks for responding.
But the type of parsed data is becoming never not SafeParseSuccess and why "const parsedError = parsedData.error;
" line is not showing error.
from zod.
You should hover over parsedData.error && ...
.
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#truthiness-narrowing
from zod.
My doubt is if optional key part of schema failed at that case there would be error value right. even though parsedData.data exist.
from zod.
Which part do you mean?
from zod.
Imagine if the schema had 3 keys. 2 required and 1 optional key. If optional key part of schema failed, I'll still get parsedData.data containing two required keys right? And I'll also get parsedData.error right? since optional key part failed.
from zod.
After digging a bit deeper, I found it's more complicated than I initially thought.
I've simplified some code to explain, and I hope this will be helpful.
I suggest checking parsedData.success
instead of parsedData.data
. Since parsedData
is a discriminated union, using success
will provide a more accurate type check.
from zod.
Thank you. My goal is to do console.warn if only optional part of schema fails and do console.error if required part fail. What is the recommended way to do this?
from zod.
You can break the whole schema into requiredSchema
and optionalSchema
, no need to combine them togather.
from zod.
Thank you. I'll do that.
from zod.
Related Issues (20)
- Unable to Chain .min() and Other Validation Methods After .refine() on z.string() HOT 2
- Question: How to extend/copy array schema but only change the containing type HOT 1
- Issue: z.any() and z.unknown() are optional by the default. HOT 2
- Help about zod arrays and strings HOT 1
- Feature Proposal: Allow Support for alternative field names
- Add Optional Descriptions to Enums in Zod for Enhanced Schema Clarity
- Incorrect type derivation when using z.array() with z.transform() HOT 2
- question: How can I show fielderrors as hints?
- zod import triggers an error in typescript HOT 2
- Module '"zod"' has no exported member 'z'.ts HOT 14
- Add nonempty() method to zod.record() for empty object validation
- Idea: `.meta(...)` method on ZodType HOT 1
- How to union or merge two or more z.record schemas HOT 1
- Type narrowing not working with generic zod type HOT 1
- Allow `.extract`/`.exclude` for `nativeEnum`s
- Update repo link `zod-accelerator` in readme.
- feat: Adding json Schema to the package
- Question: TypeScript Isolated Declarations
- Problem with validation of identical fields in the schema HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zod.