Skip to content

Commit

Permalink
feat: add pub(dart) validation according to spec (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
topaztee authored Oct 17, 2023
1 parent d57f6ff commit 6caf49c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/package-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,22 @@ class PackageURL {
_handlePyPi() {
this.name = this.name.toLowerCase().replace(/_/g, '-');
}
_handlePub() {
this.name = this.name.toLowerCase();
if (!/^[a-z0-9_]+$/i.test(this.name)) {
throw new Error('Invalid purl: contains an illegal character.');
}
}

toString() {
var purl = ['pkg:', encodeURIComponent(this.type), '/'];

if (this.type === 'pypi') {
this._handlePyPi();
}
if (this.type === 'pub') {
this._handlePub();
}

if (this.namespace) {
purl.push(
Expand Down
24 changes: 24 additions & 0 deletions test/data/test-suite-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,30 @@
"subpath": null,
"is_invalid": true
},
{
"description": "checks for invalid characters",
"purl": "pkg:pub/[email protected]",
"canonical_purl": "pkg:pub/[email protected]",
"type": "pub",
"namespace": null,
"name": "flutter_downloader",
"version": "1.0.0",
"qualifiers": null,
"subpath": null,
"is_invalid": false
},
{
"description": "checks for invalid characters",
"purl": "pkg:pub/[email protected]",
"canonical_purl": "pkg:pub/[email protected]",
"type": "pub",
"namespace": null,
"name": null,
"version": null,
"qualifiers": null,
"subpath": null,
"is_invalid": true
},
{
"description": "namespace can contain special characters",
"purl": "pkg:npm/%40foo%40%3F%23/[email protected]",
Expand Down

0 comments on commit 6caf49c

Please sign in to comment.