Skip to content

Commit

Permalink
NOT_AN_OBJECT: throw for null; add additional tests (#8930)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alxndrsn committed Apr 13, 2024
1 parent 16b6c07 commit 20a5ccc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
10 changes: 7 additions & 3 deletions packages/node_modules/pouchdb-core/src/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ function attachmentNameError(name) {
return false;
}

function isNotSingleDoc(doc) {
return doc === null || typeof doc !== 'object' || Array.isArray(doc);
}

function isValidRev(rev) {
return typeof rev === 'string' && /^\d+-/.test(rev);
}
Expand All @@ -201,7 +205,7 @@ class AbstractPouchDB extends EventEmitter {
callback = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (isNotSingleDoc(doc)) {
return callback(createError(NOT_AN_OBJECT));
}
this.bulkDocs({docs: [doc]}, opts, yankError(callback, doc._id));
Expand All @@ -212,7 +216,7 @@ class AbstractPouchDB extends EventEmitter {
cb = opts;
opts = {};
}
if (typeof doc !== 'object' || Array.isArray(doc)) {
if (isNotSingleDoc(doc)) {
return cb(createError(NOT_AN_OBJECT));
}
invalidIdError(doc._id);
Expand Down Expand Up @@ -785,7 +789,7 @@ class AbstractPouchDB extends EventEmitter {
}

for (var i = 0; i < req.docs.length; ++i) {
if (typeof req.docs[i] !== 'object' || Array.isArray(req.docs[i])) {
if (isNotSingleDoc(req.docs[i])) {
return callback(createError(NOT_AN_OBJECT));
}
}
Expand Down
50 changes: 33 additions & 17 deletions tests/integration/test.basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -794,23 +794,39 @@ adapters.forEach(function (adapter) {
});
});

it('Error when document is not an object', function (done) {
var db = new PouchDB(dbs.name);
var doc1 = [{ _id: 'foo' }, { _id: 'bar' }];
var doc2 = 'this is not an object';
var count = 5;
var callback = function (err) {
should.exist(err);
count--;
if (count === 0) {
done();
}
};
db.post(doc1, callback);
db.post(doc2, callback);
db.put(doc1, callback);
db.put(doc2, callback);
db.bulkDocs({docs: [doc1, doc2]}, callback);
[
undefined,
null,
[],
[{ _id: 'foo' }, { _id: 'bar' }],
'this is not an object',
String('this is not an object'),
//new String('this is not an object'), actually, this _is_ an object
].forEach((badDoc, idx) => {
describe(`Should error when document is not an object #${idx}`, () => {
let db;

const expectNotAnObject = fn => async () => {
let threw;
try {
await fn();
} catch (err) {
threw = true;
err.message.should.equal('Document must be a JSON object');
}
if (!threw) {
throw new Error('should have thrown');
}
};

beforeEach(() => {
db = new PouchDB(dbs.name);
});

it('should error for .post()', expectNotAnObject(() => db.post(badDoc)));
it('should error for .put()', expectNotAnObject(() => db.put(badDoc)));
it('should error for .bulkDocs()', expectNotAnObject(() => db.bulkDocs({docs: [badDoc]})));
});
});

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

0 comments on commit 20a5ccc

Please sign in to comment.