Skip to content

Commit 20a5ccc

Browse files
authored
NOT_AN_OBJECT: throw for null; add additional tests (#8930)
* NOT_AN_OBJECT: throw for null; add additional tests Also separates test cases so specific failures can be understood. * extract function, make null check stricter
1 parent 16b6c07 commit 20a5ccc

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

packages/node_modules/pouchdb-core/src/adapter.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ function attachmentNameError(name) {
190190
return false;
191191
}
192192

193+
function isNotSingleDoc(doc) {
194+
return doc === null || typeof doc !== 'object' || Array.isArray(doc);
195+
}
196+
193197
function isValidRev(rev) {
194198
return typeof rev === 'string' && /^\d+-/.test(rev);
195199
}
@@ -201,7 +205,7 @@ class AbstractPouchDB extends EventEmitter {
201205
callback = opts;
202206
opts = {};
203207
}
204-
if (typeof doc !== 'object' || Array.isArray(doc)) {
208+
if (isNotSingleDoc(doc)) {
205209
return callback(createError(NOT_AN_OBJECT));
206210
}
207211
this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id));
@@ -212,7 +216,7 @@ class AbstractPouchDB extends EventEmitter {
212216
cb = opts;
213217
opts = {};
214218
}
215-
if (typeof doc !== 'object' || Array.isArray(doc)) {
219+
if (isNotSingleDoc(doc)) {
216220
return cb(createError(NOT_AN_OBJECT));
217221
}
218222
invalidIdError(doc._id);
@@ -785,7 +789,7 @@ class AbstractPouchDB extends EventEmitter {
785789
}
786790

787791
for (var i = 0; i < req.docs.length; ++i) {
788-
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
792+
if (isNotSingleDoc(req.docs[i])) {
789793
return callback(createError(NOT_AN_OBJECT));
790794
}
791795
}

tests/integration/test.basics.js

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -794,23 +794,39 @@ adapters.forEach(function (adapter) {
794794
});
795795
});
796796

797-
it('Error when document is not an object', function (done) {
798-
var db = new PouchDB(dbs.name);
799-
var doc1 = [{ _id: 'foo' }, { _id: 'bar' }];
800-
var doc2 = 'this is not an object';
801-
var count = 5;
802-
var callback = function (err) {
803-
should.exist(err);
804-
count--;
805-
if (count === 0) {
806-
done();
807-
}
808-
};
809-
db.post(doc1, callback);
810-
db.post(doc2, callback);
811-
db.put(doc1, callback);
812-
db.put(doc2, callback);
813-
db.bulkDocs({docs: [doc1, doc2]}, callback);
797+
[
798+
undefined,
799+
null,
800+
[],
801+
[{ _id: 'foo' }, { _id: 'bar' }],
802+
'this is not an object',
803+
String('this is not an object'),
804+
//new String('this is not an object'), actually, this _is_ an object
805+
].forEach((badDoc, idx) => {
806+
describe(`Should error when document is not an object #${idx}`, () => {
807+
let db;
808+
809+
const expectNotAnObject = fn => async () => {
810+
let threw;
811+
try {
812+
await fn();
813+
} catch (err) {
814+
threw = true;
815+
err.message.should.equal('Document must be a JSON object');
816+
}
817+
if (!threw) {
818+
throw new Error('should have thrown');
819+
}
820+
};
821+
822+
beforeEach(() => {
823+
db = new PouchDB(dbs.name);
824+
});
825+
826+
it('should error for .post()', expectNotAnObject(() => db.post(badDoc)));
827+
it('should error for .put()', expectNotAnObject(() => db.put(badDoc)));
828+
it('should error for .bulkDocs()', expectNotAnObject(() => db.bulkDocs({docs: [badDoc]})));
829+
});
814830
});
815831

816832
it('Test instance update_seq updates correctly', function (done) {

0 commit comments

Comments
 (0)