Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/purl-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,12 @@ module.exports = {
return false
}
if (code0 !== 64 /*'@'*/) {
throw new PurlError(
`npm "namespace" component must start with an "@" character`
)
if (throws) {
throw new PurlError(
`npm "namespace" component must start with an "@" character`
)
}
return false
}
const namespaceWithoutAtSign = namespace.slice(1)
if (
Expand Down
22 changes: 21 additions & 1 deletion test/package-url.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const TEST_FILE = [
...require('./data/contrib-tests.json')
]

const { PackageURL } = require('../src/package-url')
const { PackageURL, PurlType } = require('../src/package-url')

function getNpmId(purl) {
const { name, namespace } = purl
Expand Down Expand Up @@ -545,4 +545,24 @@ describe('PackageURL', function () {
)
})
})

describe('npm validate', function () {
it('should return false rather than throw for a bad namespace when throws is false', function () {
const purl = { type: 'npm', namespace: 'foo', name: 'bar' }
assert.strictEqual(PurlType.npm.validate(purl, false), false)
})

it('should still throw for a bad namespace when throws is true', function () {
const purl = { type: 'npm', namespace: 'foo', name: 'bar' }
assert.throws(
() => PurlType.npm.validate(purl, true),
/npm "namespace" component must start with an "@" character/
)
})

it('should accept a scoped namespace when throws is false', function () {
const purl = { type: 'npm', namespace: '@scope', name: 'bar' }
assert.strictEqual(PurlType.npm.validate(purl, false), true)
})
})
})