Skip to content

Commit 05de41c

Browse files
author
Grace Chong
committed
test: add test to make sure only writeErrors key has validation settings toggled
1 parent 745bf66 commit 05de41c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/cmap/commands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,9 @@ export class BinMsg {
838838
const promoteValues = options.promoteValues ?? this.opts.promoteValues;
839839
const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
840840
const bsonRegExp = options.bsonRegExp ?? this.opts.bsonRegExp;
841+
if (options.validation && Object.keys(options.validation.utf8)[0] !== 'writeErrors') {
842+
throw new Error('Can only toggle validation settings for writeErrors key');
843+
}
841844
const validation = options.validation ?? { utf8: { writeErrors: false } };
842845

843846
// Set up the options

test/unit/commands.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ const invalidUtf8InOtherKeyJSON = {
7171
};
7272

7373
describe('BinMsg', function () {
74+
it('should throw error if trying to toggle validation settings for keys other than writeErrors', function () {
75+
const binMsgInvalidUtf8ErrorMsg = new BinMsg(
76+
Buffer.alloc(0),
77+
msgHeader,
78+
msgBodyInvalidUtf8WriteErrors
79+
);
80+
expect(() => binMsgInvalidUtf8ErrorMsg.parse({ validation: { utf8: { n: false } } })).to.throw(
81+
Error,
82+
'Can only toggle validation settings for writeErrors key'
83+
);
84+
});
85+
7486
it('should not throw invalid utf8 error when validation disabled for writeErrors', function () {
7587
const binMsgInvalidUtf8ErrorMsg = new BinMsg(
7688
Buffer.alloc(0),

0 commit comments

Comments
 (0)