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.

z.object().passthrough() not working with objects like Buffer

See original GitHub issue

Thanks for Zod!

Perhaps I am using this incorrectly, or missed something in the docs. I wanted to use a zod schema to parse an object containing a field that points to a Buffer , more generally an object that implements Reader (Buffer does), but that detail isn’t important for this. I didn’t expect native support for Buffer, so I figured I could use z.object({ ... }).passthrough(), checking for the methods on Buffer I cared about, but then passing through the whole object to keep the internals working.

What seems to happen is that I get back an object that has the bytes() function, but the rest of the fields do not passthrough. So fundamentally, it seems that passthrough() isn’t working for this case, unless i’m using it incorrectly.

Here is a reproduction using Deno:

import * as z from "https://cdn.skypack.dev/zod@^3";
import { Buffer } from "https://deno.land/std@0.125.0/io/buffer.ts";

const FooSchema = z.object({
  bar: z.string(),
  // stream is a Buffer, but passthrough all fields
  stream: z.object({
    bytes: z.function(),
  }).passthrough(),
});

const throws = FooSchema.parse({
  bar: "fizz",
  stream: new Buffer(new Uint8Array(4).buffer),
});

console.log("typeof Buffer: ", typeof new Buffer(new Uint8Array(4).buffer));

// this throws because another method has been stripped that bytes() uses internally
console.log("throws.stream.bytes(): ", throws.stream.bytes());

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
EdsterGcommented, Feb 11, 2022

@TillaTheHun0 the type you need to handle this case is not possible to specify in TypeScript. So your best bet is it let the type be any but add an refinement to validate the input further.

const FooSchema = z.object({
  bar: z.string(),
  stream: z.any().refine(
      (val: any) => val !== undefined /* or some other check to ensure proper format */,
      message: "stream must be defined",
  ),
});
0reactions
TillaTheHun0commented, Feb 11, 2022

Looks great! I’ll go ahead and use that approach. I’ll go ahead and close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The "chunk" argument must be of type string or an instance of ...
It's because data 's data type is an object/json , and res.write() accepts data type string or buffer . JSON.stringify converts the object...
Read more >
Stream | Node.js v19.3.0 Documentation
Data is buffered in Readable streams when the implementation calls stream.push(chunk) . If the consumer of the Stream does not call stream.read() ,...
Read more >
Node.js Streams: Everything you need to know - freeCodeCamp
This reportProgress stream is a simple pass-through stream, but it reports the progress to standard out as well. Note how I used the...
Read more >
Chapter 5. Streams: Node's most powerful and misunderstood ...
The idea of a stream of data isn't new, but it's an important concept and integral ... science have been used to solve...
Read more >
Tutorial2: VAOs, VBOs, Vertex and Fragment Shaders (C / SDL)
A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information...
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