Skip to content

Commit

Permalink
fix(validate): only accept leading digit or v character
Browse files Browse the repository at this point in the history
  • Loading branch information
omichelsen committed Dec 10, 2021
1 parent 0fc5477 commit bac8066
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default function compareVersions(v1, v2) {
return 0;
}

export const validate = (v) => typeof v === 'string' && semver.test(v);
export const validate = (v) =>
typeof v === 'string' && /^[v\d]/.test(v) && semver.test(v);

export const compare = (v1, v2, operator) => {
// validate input operator
Expand Down
3 changes: 3 additions & 0 deletions test/validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('validate versions', () => {
[{}, false],
[[], false],
[() => undefined, false],
['foo', false],
['6.3.', false],
['1.2.3a', false],
['1.2.-3a', false],
Expand All @@ -21,6 +22,8 @@ describe('validate versions', () => {
['1.0.0+20130313144700', true],
['1.2.3.100', true],
['2020', true],
['=1.0', false],
['>1.0.0', false],
].forEach(([v, expected]) => {
it(`${v}`, () => {
assert.equal(validate(v), expected);
Expand Down

0 comments on commit bac8066

Please sign in to comment.