feat: support bindary buffer that has been JSON.parse(JSON.strinified()) #2960
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What?
This adds to binary type handler the ability to utilize node.JS's built in Buffer JSON encoding and appropriate tests for the functionality.
What issues does this address?
#2954
What can be improved?
Why?
As a part of a larger request, it is helpful to be able to pass a binary value over a larger JSON model. I ran into this recently when passing request handlers over an HTTP call initiating a browser session that then had an array of request replacements. It worked great for JS and CSS, but small images would fail to decode properly.
Available workarounds?
This can also be achieved via doing the following in with the schema:
Why should this be apart of the core framework?
I believe that the inclusion of allowing this to be coerced via Buffer.from() indicates that node.js meant for this data type to be both encoded and decoded. I feel like this change follows the "least surprise" API: If a buffer snuck into a data structure that was passed to a JSON encoder (like when posting an HTTP call), then parsed by a HTTP reciever's middleware, then the least surprising outcome would be to have it be a Buffer in the end. I believe that if projects were to add the workaround to their projects, it would add to the cognitive load of their projects.
Arguments against this being apart of Joi?
Argument: @Marsup brought up the inefficiency of encoding binary in JSON.
Response: While I agree it is inefficient overall, but I pose the purpose of JSON was never extreme efficiency. It's strength is in clarity and easy of reading. I agree that there are other formats that are better for binary transfer, but I think preventing this based on efficiency argument forces premature optimization of software.
Conclusion:
I appreciate your consideration of this PR. If you don't want to commit to adding this to the project, please indicate such and decline this PR, so that downstream projects can act upon that information. Thank you for the awesome tool!