Skip to content

Commit 6caf49c

Browse files
authored
feat: add pub(dart) validation according to spec (#51)
1 parent d57f6ff commit 6caf49c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/package-url.js

+9
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,22 @@ class PackageURL {
7474
_handlePyPi() {
7575
this.name = this.name.toLowerCase().replace(/_/g, '-');
7676
}
77+
_handlePub() {
78+
this.name = this.name.toLowerCase();
79+
if (!/^[a-z0-9_]+$/i.test(this.name)) {
80+
throw new Error('Invalid purl: contains an illegal character.');
81+
}
82+
}
7783

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

8187
if (this.type === 'pypi') {
8288
this._handlePyPi();
8389
}
90+
if (this.type === 'pub') {
91+
this._handlePub();
92+
}
8493

8594
if (this.namespace) {
8695
purl.push(

test/data/test-suite-data.json

+24
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,30 @@
323323
"subpath": null,
324324
"is_invalid": true
325325
},
326+
{
327+
"description": "checks for invalid characters",
328+
"purl": "pkg:pub/flutter_downloader@1.0.0",
329+
"canonical_purl": "pkg:pub/flutter_downloader@1.0.0",
330+
"type": "pub",
331+
"namespace": null,
332+
"name": "flutter_downloader",
333+
"version": "1.0.0",
334+
"qualifiers": null,
335+
"subpath": null,
336+
"is_invalid": false
337+
},
338+
{
339+
"description": "checks for invalid characters",
340+
"purl": "pkg:pub/flutter--downloader@1.0.0",
341+
"canonical_purl": "pkg:pub/flutter--downloader@1.0.0",
342+
"type": "pub",
343+
"namespace": null,
344+
"name": null,
345+
"version": null,
346+
"qualifiers": null,
347+
"subpath": null,
348+
"is_invalid": true
349+
},
326350
{
327351
"description": "namespace can contain special characters",
328352
"purl": "pkg:npm/%40foo%40%3F%23/bar@1.0.0",

0 commit comments

Comments
 (0)