nvie/decoders

Self-referential objects break decoders

sfarthin opened this issue · 6 comments

If an object is passed in that refers to itself (like the express req object), an infinite loop occurs.

Smallest reproducible example:

const { string } = require('decoders');

// Take any decoder and pass in some self-referential object
const req = {};
req.myself = req;

console.log(string(req)); // <--- RangeError: Maximum call stack size exceeded
nvie commented

Hey Steve, thanks for reporting this issue! This has been a known issue to me for quite some time. It's an implementation detail inside the underlying debrief library that I definitely want to fix. I'll see if I can take a look at this soon.

👍 Thanks. I think even a quick-fix such as throwing a slightly more clear error message will go a long way.

For example, if the number of nested levels goes over 50, throw an error such as "Input is too large or self-referential" (maybe even with a link to this issue?). Default error messages are kinda broken when this large anyways.

Thanks for the good work on this library, its an essential tool when working with types in JS/TS.

nvie commented

Default error messages are kinda broken when this large anyways.

If you want more succinct error messages, try using guard(..., { style: 'simple' }) instead of just the default guard(...). That will not echo back the input object, and just describe where the error is. That won't help you with this issue, but may help reduce your error messages in case of large objects.

nvie commented

This should now be fixed in v1.20.1. Please let me know if you're still seeing issues with this! Thanks for reporting. 🙏

nvie commented

This is the new behaviour under 1.20.1:

Screen Shot 2020-05-25 at 13 55 40@2x