Skip to content

Commit 3c85203

Browse files
authored
db.post(), db.bulkDocs(): throw INVALID_REV consistently (#8934)
Follow-up to #8931
1 parent 20a5ccc commit 3c85203

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,9 +789,13 @@ class AbstractPouchDB extends EventEmitter {
789789
}
790790

791791
for (var i = 0; i < req.docs.length; ++i) {
792-
if (isNotSingleDoc(req.docs[i])) {
792+
const doc = req.docs[i];
793+
if (isNotSingleDoc(doc)) {
793794
return callback(createError(NOT_AN_OBJECT));
794795
}
796+
if ('_rev' in doc && !isValidRev(doc._rev)) {
797+
return callback(createError(INVALID_REV));
798+
}
795799
}
796800

797801
var attachmentError;

tests/integration/test.basics.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,25 @@ adapters.forEach(function (adapter) {
293293
({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:'hi' }),
294294
({ rev }) => ({ toString:() => rev, indexOf:() => 12, substring:() => 'hi' }),
295295
].forEach((generateRev, idx) => {
296+
it(`post doc with illegal rev value #${idx}`, async () => {
297+
const db = new PouchDB(dbs.name);
298+
299+
let threw;
300+
try {
301+
await db.post({
302+
_rev: generateRev({ rev:'1-valid' }),
303+
another: 'test'
304+
});
305+
} catch (err) {
306+
threw = true;
307+
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
308+
}
309+
310+
if (!threw) {
311+
throw new Error('db.put() should have thrown.');
312+
}
313+
});
314+
296315
it(`Modify a doc with illegal rev value #${idx}`, async () => {
297316
const db = new PouchDB(dbs.name);
298317

@@ -314,6 +333,52 @@ adapters.forEach(function (adapter) {
314333
throw new Error('db.put() should have thrown.');
315334
}
316335
});
336+
337+
it(`bulkDocs with illegal rev value #${idx} (existing doc)`, async () => {
338+
const db = new PouchDB(dbs.name);
339+
340+
const info = await db.post({ test: 'somestuff' });
341+
342+
let threw;
343+
try {
344+
await db.bulkDocs({
345+
docs: [ {
346+
_id: info.id,
347+
_rev: generateRev(info),
348+
another: 'test'
349+
} ],
350+
});
351+
} catch (err) {
352+
threw = true;
353+
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
354+
}
355+
356+
if (!threw) {
357+
throw new Error('db.put() should have thrown.');
358+
}
359+
});
360+
361+
it(`bulkDocs with illegal rev value #${idx} (new doc)`, async () => {
362+
const db = new PouchDB(dbs.name);
363+
364+
let threw;
365+
try {
366+
await db.bulkDocs({
367+
docs: [ {
368+
_id: '1',
369+
_rev: generateRev({ rev:'1_valid' }),
370+
another: 'test'
371+
} ],
372+
});
373+
} catch (err) {
374+
threw = true;
375+
err.message.should.equal('Invalid rev format'); // TODO should be err.reason?
376+
}
377+
378+
if (!threw) {
379+
throw new Error('db.put() should have thrown.');
380+
}
381+
});
317382
});
318383

319384
it('Remove doc', function (done) {

0 commit comments

Comments
 (0)