-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Labels
featureNew functionality or improvementNew functionality or improvement
Milestone
Description
Hi! 👋
Firstly, thanks for your work on this project! 🙂
I am using joi with Hapi, and I have a route with a payload field defined as serialNumber: Joi.binary()
But when inspecting payload before validation, I see that I receive the data not as a Buffer instance, but as an object with { type: "Buffer", data: } (probably Hapi calling toJSON() on it). I think it's valid to coerce it to a Buffer instance
Here is the diff that solved my problem:
diff --git a/node_modules/joi/lib/types/binary.js b/node_modules/joi/lib/types/binary.js
index 9147166..053d022 100755
--- a/node_modules/joi/lib/types/binary.js
+++ b/node_modules/joi/lib/types/binary.js
@@ -14,13 +14,19 @@ module.exports = Any.extend({
type: 'binary',
coerce: {
- from: 'string',
+ from: ['string', 'object'],
method(value, { schema }) {
- try {
- return { value: Buffer.from(value, schema._flags.encoding) };
+ if (typeof value === 'string') {
+ try {
+ return { value: Buffer.from(value, schema._flags.encoding) };
+ }
+ catch (ignoreErr) { }
+ } else if (typeof value === 'object') {
+ if (value.type === 'Buffer' && Array.isArray(value.data)) {
+ return { value: Buffer.from(value.data) }
+ }
}
- catch (ignoreErr) { }
}
},
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
featureNew functionality or improvementNew functionality or improvement