Skip to content

Commit 1a81317

Browse files
authored
Merge pull request #52 from multiformats/fix/tighten-addr-validation
Tighten validation on addr when creating from existing address
2 parents 3bad57b + 0097036 commit 1a81317

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ function Multiaddr (addr) {
2828
}
2929

3030
// default
31-
addr = addr || ''
31+
if (addr == null) {
32+
addr = ''
33+
}
3234

3335
if (addr instanceof Buffer) {
3436
/**

test/index.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,25 @@ describe('construction', () => {
4040
expect(multiaddr('').toString()).to.equal('/')
4141
})
4242

43-
it('throws on non string or buffer', () => {
44-
expect(() => multiaddr({})).to.throw(/addr must be a string/)
43+
it('null/undefined construct still works', () => {
44+
expect(multiaddr().toString()).to.equal('/')
45+
expect(multiaddr(null).toString()).to.equal('/')
46+
expect(multiaddr(undefined).toString()).to.equal('/')
47+
})
48+
49+
it('throws on truthy non string or buffer', () => {
50+
const errRegex = /addr must be a string/
51+
expect(() => multiaddr({})).to.throw(errRegex)
52+
expect(() => multiaddr([])).to.throw(errRegex)
53+
expect(() => multiaddr(138)).to.throw(errRegex)
54+
expect(() => multiaddr(true)).to.throw(errRegex)
55+
})
56+
57+
it('throws on falsy non string or buffer', () => {
58+
const errRegex = /addr must be a string/
59+
expect(() => multiaddr(NaN)).to.throw(errRegex)
60+
expect(() => multiaddr(false)).to.throw(errRegex)
61+
expect(() => multiaddr(0)).to.throw(errRegex)
4562
})
4663
})
4764

0 commit comments

Comments
 (0)