Giter Club home page Giter Club logo

Comments (10)

sunnylost avatar sunnylost commented on September 27, 2024

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.

AmruthjithSunil avatar AmruthjithSunil commented on September 27, 2024

Thanks for responding.
image
But the type of parsed data is becoming never not SafeParseSuccess and why "const parsedError = parsedData.error;
" line is not showing error.

from zod.

sunnylost avatar sunnylost commented on September 27, 2024

You should hover over parsedData.error && ....

https://www.typescriptlang.org/docs/handbook/2/narrowing.html#truthiness-narrowing

from zod.

AmruthjithSunil avatar AmruthjithSunil commented on September 27, 2024

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.

sunnylost avatar sunnylost commented on September 27, 2024

Which part do you mean?

from zod.

AmruthjithSunil avatar AmruthjithSunil commented on September 27, 2024

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.

sunnylost avatar sunnylost commented on September 27, 2024

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.

https://www.typescriptlang.org/play/?#code/C4TwDgpgBA8g0gHgCoD4oF4oG8BQV8AmAhsEQFxRJ74QBOtA9rQPwUB2EAbnTgL445QkKADEiASwA2yNJlz4oxUqygdutalDqNaFKv0HhoAJQgBnAK6TgyADSjZsRKigAfUROkiUAgGYW2AGNgcQY2KAAPAAoASmxNAHoEqAABYDMAWnEAczYmCE1AsLNgKF1TS2sEACMGBkkIIjZ7AFF6JkcsAwVxXyjaADolIjj5BXxaCGALWjZNbomoJNUGUrYidoB3cTZsgR6+gEJB4dGl5OGoIqsCKGroXyJJMwh7MwYyq4Ybu+h4GrqDSaPnGi2WJSkkhWaw2jG2u00Ckm01m832YOS6y2O2yUGAH3+tXqjTYKHs1QspQAkoowgByUrAAAWOwA1niWWYoOIuUV6BBggNEeduaUmUQufjfmUIJISBBbttmVAAOIQDi0cSBLlNW7pOm8sLARhQ3ySBibKBNJ4gMw8zSDbRMKAAMhdXzY7waA3N2X6AydtBi6O5fUd7SD8VBn2WWLhOLxHzUPGj4Z0aN4QA

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.

AmruthjithSunil avatar AmruthjithSunil commented on September 27, 2024

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.

sunnylost avatar sunnylost commented on September 27, 2024

You can break the whole schema into requiredSchema and optionalSchema, no need to combine them togather.

from zod.

AmruthjithSunil avatar AmruthjithSunil commented on September 27, 2024

Thank you. I'll do that.

from zod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.