Skip to content

Commit

Permalink
fix: add doc validation to ensure $id is not undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Jun 17, 2022
1 parent 49087dc commit 2b71925
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/design/validateDocUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ module.exports = function (newDoc, oldDoc, userCtx) {
validateDates(newDoc, oldDoc);
} else if (newDoc.$type === 'entry') {
validateOwners(newDoc);
if (newDoc.$id === undefined) {
throw { forbidden: '$id must be defined' };
}
validateDates(newDoc, oldDoc);
if (oldDoc) {
if (Array.isArray(newDoc.$id) && Array.isArray(oldDoc.$id)) {
Expand Down
5 changes: 5 additions & 0 deletions test/unit/design/validateDocUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe('validate_doc_update', () => {
});
describe('$type: entry', () => {
test('id', () => {
assert(
addOwners(addDate({ $type: 'entry' })),
null,
'$id must be defined',
);
assert(
addOwners(addDate({ $type: 'entry', $id: 'abc' })),
addOwners(addDate({ $type: 'entry', $id: 'xyz' })),
Expand Down
9 changes: 9 additions & 0 deletions test/unit/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,15 @@ describe('entry creation and editions', () => {
expect(entry1.info.isNew).toBe(true);
expect(entry2.info.isNew).toBe(true);
});

test('$id is null by default', async () => {
const entry = await couch.insertEntry({ $content: 'A' }, 'a@a.com');
expect(entry.action).toEqual('created');

const dbEntry = await couch.getEntry(entry.info.id, 'a@a.com');
expect(dbEntry.$id).toBe(null);
expect(dbEntry.$content).toBe('A');
});
});

describe('entry rights', () => {
Expand Down

0 comments on commit 2b71925

Please sign in to comment.