Skip to content

Commit 3fa4485

Browse files
committed
feat(types): support casting JSON form of buffer
re: #6863
1 parent 9785cb9 commit 3fa4485

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/schema/buffer.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
117117
value._subtype = this.options.subtype;
118118
}
119119
}
120-
121120
return value;
122-
} else if (value instanceof Binary) {
121+
}
122+
123+
if (value instanceof Binary) {
123124
ret = new MongooseBuffer(value.value(true), [this.path, doc]);
124125
if (typeof value.sub_type !== 'number') {
125126
throw new CastError('buffer', value, this.path);
@@ -132,8 +133,12 @@ SchemaBuffer.prototype.cast = function(value, doc, init) {
132133
return value;
133134
}
134135

136+
135137
var type = typeof value;
136-
if (type === 'string' || type === 'number' || Array.isArray(value)) {
138+
if (
139+
type === 'string' || type === 'number' || Array.isArray(value) ||
140+
(type === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) // gh-6863
141+
) {
137142
if (type === 'number') {
138143
value = [value];
139144
}

lib/types/buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function MongooseBuffer(value, encode, offset) {
4444
var buf;
4545
if (typeof val === 'number' || val instanceof Number) {
4646
buf = Buffer.alloc(val);
47-
} else {
47+
} else { // string, array or object { type: 'Buffer', data: [...] }
4848
buf = Buffer.from(val, encoding, offset);
4949
}
5050
utils.decorate(buf, MongooseBuffer.mixin);

0 commit comments

Comments
 (0)