Skip to content

Commit 630549f

Browse files
committed
fix: add isValid tests
1 parent c3f4a75 commit 630549f

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/binary.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,6 @@ export class UUID extends Binary {
443443
return false;
444444
}
445445

446-
if (input instanceof UUID) {
447-
return true;
448-
}
449-
450446
if (typeof input === 'string') {
451447
return UUID.isValidUUIDString(input);
452448
}
@@ -455,7 +451,11 @@ export class UUID extends Binary {
455451
return input.byteLength === UUID_BYTE_LENGTH;
456452
}
457453

458-
return false;
454+
return (
455+
input._bsontype === 'Binary' &&
456+
input.sub_type === this.SUBTYPE_UUID &&
457+
input.buffer.byteLength === 16
458+
);
459459
}
460460

461461
/**

test/node/uuid.test.ts

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,42 @@ describe('UUID', () => {
6363
expect(() => new UUID({})).to.throw(BSONError);
6464
});
6565

66-
it('should correctly check if a buffer isValid', () => {
67-
const validBuffer = Buffer.from(UPPERCASE_VALUES_ONLY_UUID_STRING, 'hex');
68-
expect(UUID.isValid(validBuffer)).to.be.true;
66+
context('isValid()', () => {
67+
it('returns true for hex string with dashes', () => {
68+
expect(UUID.isValid(UPPERCASE_VALUES_ONLY_UUID_STRING)).to.be.true;
69+
});
70+
71+
it('returns true for hex string without dashes', () => {
72+
expect(UUID.isValid(LOWERCASE_VALUES_ONLY_UUID_STRING)).to.be.true;
73+
});
74+
75+
it('returns true for hex string that is not uuid v4', () => {
76+
expect(UUID.isValid('00'.repeat(16))).to.be.true;
77+
});
78+
79+
it('returns true for buffer of length 16', () => {
80+
expect(UUID.isValid(Buffer.alloc(16))).to.be.true;
81+
});
82+
83+
it('returns false for buffer not of length 16', () => {
84+
expect(UUID.isValid(Buffer.alloc(10))).to.be.false;
85+
});
86+
87+
it('returns false for falsy inputs', () => {
88+
expect(UUID.isValid()).to.be.false;
89+
expect(UUID.isValid(null)).to.be.false;
90+
expect(UUID.isValid(false)).to.be.false;
91+
expect(UUID.isValid('')).to.be.false;
92+
});
93+
94+
it('returns true for Binary instances of UUID', () => {
95+
expect(UUID.isValid(new UUID())).to.be.true;
96+
expect(UUID.isValid(Binary.createFromHexString('00'.repeat(16), 4))).to.be.true;
97+
});
98+
99+
it('returns false for Binary instance of the wrong length', () => {
100+
expect(UUID.isValid(Binary.createFromHexString('00', 4))).to.be.false;
101+
});
69102
});
70103

71104
it('should correctly convert to and from a Binary instance', () => {

0 commit comments

Comments
 (0)