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.

[Discussion] Parsing input that's a superset of the schema type

See original GitHub issue

Given a zod schema like

const ZRecord = z.object({
    id: z.number(),
    name: z.string()
});

how can I use ZRecord to extract the schema from a conforming object that has additional fields? E.g.

const result = ZRecord.safeParse({ id: 123, name: "Joe", notes:"enjoys hiking" });

Is there any option that I can pass to the parse methods (e.g. ZRecord.safeParse) that will ignore additional keys, but also mention them in the result.error.errors list? I was hoping that even when result.success was false, result.data would still have the parsed hierarchy. I don’t see a way to do this other than iterating through the ZAddEntryParams.shape hierarchy and pulling the relevant attributes from the input into a new “clipped” object before parsing.

It would be valuable to extract the schema from a larger object for something like a web request, to give the API caller a message that they had extra keys in their request.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
pjdoncommented, Sep 27, 2020

Oh I see, that was just a secondary thing and .passthrough covers my use case pretty well. I kinda merged the two questions into one and it wasn’t clear. The reply with the link was for posterity.

Thanks for making this library. The more I use it the more I’m impressed with how complete and well conceived it is!

0reactions
colinhackscommented, Sep 28, 2020

Thank you! That’s always great to hear 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

[fix] Fixing SQL parsing issue by john-bodley · Pull Request #7374 ...
This PR fixes an issue with parsing SQL where iterating over the identifiers associated with a sqlparser IdentifierList yields a Token (as opposed...
Read more >
A Guide To Parsing: Algorithms And Terminology
An in-depth coverage of parsing terminology an issues, together with an explanation for each one of the major algorithms and when to use...
Read more >
XML Schema Part 2: Datatypes Second Edition - W3C
The datatype language, which is itself represented in XML 1.0, provides a superset of the capabilities found in XML 1.0 document type ...
Read more >
Understanding JSON Schema
JSON Schema is a powerful tool for validating the structure of JSON data. However, learning to use it by reading its.
Read more >
Apache Avro™ 1.8.2 Specification
Schema Resolution; Parsing Canonical Form for Schemas. Transforming into Parsing Canonical Form; Schema Fingerprints. Logical Types.
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