sinclairzx81 / typebox

Json Schema Type Builder with Static Type Resolution for TypeScript
Other
4.77k stars 152 forks source link

fix: avoid Errors call for nested Encode/Decode #781

Closed aleclarson closed 5 months ago

aleclarson commented 6 months ago

Use a stack for Encode/Decode to detect when it‘s a nested call. If nested, the Errors function is not called, since the produced error would only be a fraction of the full JSON path. This change is beneficial for certain Transform usage, as described in #780.

Closes #780

sinclairzx81 commented 6 months ago

@aleclarson Hey, thanks again for these PRs!

Give me a few days to review this and https://github.com/sinclairzx81/typebox/pull/777 as I'm currently unavailable this weekend. Will give this PR some thought, there may be a way to propagate the path into Encode/Decode, but might require an update to the TransformEncode, TransformDecode internal functions (making them work a bit like errors), but will need to review when I'm back.

I do think the Transform functions require a bit of rethinking in terms of error handling, and there may be cross over between this PR and https://github.com/sinclairzx81/typebox/pull/777. Will try get back to you early next week.

Thanks again!

sinclairzx81 commented 5 months ago

@aleclarson Hiya,

I think this PR may be resolved by https://github.com/sinclairzx81/typebox/pull/791 where the thrown Error was added to the Transform{Encode|Decode}Error type which is now accessible via the error property. https://github.com/sinclairzx81/typebox/pull/791 also adds the path to this error type indicating which interior value caused the error to be thrown during Encode / Decode.

I think this resolves the original linked issue on https://github.com/sinclairzx81/typebox/issues/780 (which has since been closed, but relates specifically to obtaining error paths). However, can reopen and reinvestigate if I'm missing something with respect to the details around decodeStack provided on this implementation. If so, let's discuss via GH issues/discussions!

Thanks again for the PR, will close off this one for now. All the best! S