Skip to content

Return false instead of throwing in validate() for a bad npm namespace#97

Open
arpitjain099 wants to merge 1 commit into
package-url:masterfrom
arpitjain099:chore/npm-namespace-validate-throws-guard
Open

Return false instead of throwing in validate() for a bad npm namespace#97
arpitjain099 wants to merge 1 commit into
package-url:masterfrom
arpitjain099:chore/npm-namespace-validate-throws-guard

Conversation

@arpitjain099

Copy link
Copy Markdown

The validators in src/purl-type.js share the (purl, throws) => boolean contract: on a violation they throw only when throws is true, and otherwise return false. Of the sixteen throw sites in the file, fifteen wrap the throw in if (throws). The npm namespace check that requires a leading @ was the odd one out and threw unconditionally.

Because of that, PurlType.npm.validate(purl, false) throws a PurlError for an npm purl whose namespace does not start with @, when the non-throwing path is supposed to return false. The constructor always calls with throws=true, so this only shows up when calling the boolean validator directly.

The fix wraps that one throw in the same if (throws) guard the sibling checks use and returns false when throws is false. Same shape as the golang parameter fix in #88, just a different site.

I added a few tests: a bad namespace with throws=false now returns false, a bad namespace with throws=true still throws, and a scoped namespace with throws=false returns true. Ran the full suite with mocha and it passes; the new false-return test fails on master before the change.

Signed-off-by per DCO.

The npm type validator follows the (purl, throws) => boolean contract used
across purl-type.js: on a violation it throws only when throws is true and
otherwise returns false. Fifteen of the sixteen throw sites guard the throw
with if (throws). The npm namespace check that requires a leading @ was the
one exception, so PurlType.npm.validate(purl, false) threw a PurlError for a
namespace not starting with @ instead of returning false, breaking the
documented non-throwing boolean path.

Wrap that throw in the same if (throws) guard as its siblings and return
false when throws is false. Added tests covering the throws=false and
throws=true paths for a bad namespace plus a scoped namespace.

Signed-off-by: arpitjain099 <arpitjain099@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant