-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: throw error when flags do not match pattern #831
Conversation
Codecov Report
@@ Coverage Diff @@
## main #831 +/- ##
==========================================
- Coverage 92.07% 91.94% -0.13%
==========================================
Files 34 34
Lines 1173 1179 +6
Branches 240 243 +3
==========================================
+ Hits 1080 1084 +4
- Misses 63 64 +1
- Partials 30 31 +1
Flags with carried forward coverage won't be shown. Click here to find out more.
|
src/helpers/validate.ts
Outdated
@@ -14,10 +14,15 @@ export function validateURL(url: string): boolean { | |||
return validator.isURL(url, { require_protocol: true }) | |||
} | |||
|
|||
export function validateFlags(flags: string): boolean { | |||
export function validateFlags(flag: string): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
export function validateFlags(flag: string): boolean { | |
export function validateFlags(flags: string[]): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is being passed a single flag as part of a filter command, I believe, @mitchell-codecov
// eslint-disable-next-line no-useless-escape | ||
const mask = /^[\w\.\-]{1,45}$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// eslint-disable-next-line no-useless-escape | |
const mask = /^[\w\.\-]{1,45}$/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mitchell-codecov Why are you deleting the mask?
src/helpers/validate.ts
Outdated
// eslint-disable-next-line no-useless-escape | ||
const mask = /^[\w\.\-]{1,45}$/ | ||
return mask.test(flags) | ||
if (flag.length !== 0 && mask.test(flag) !== true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (flag.length !== 0 && mask.test(flag) !== true) { | |
const invalidFlags = flags.filter(isValidFlag) | |
if (invalidFlags.length > 0) { |
src/helpers/validate.ts
Outdated
return mask.test(flags) | ||
if (flag.length !== 0 && mask.test(flag) !== true) { | ||
throw new Error( | ||
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`, | |
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flags}`, |
src/helpers/validate.ts
Outdated
`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`, | ||
) | ||
} | ||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return true |
With the above suggestions, I additionally recommend a new function for the validation: // eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
function isValidFlag(flag: string): boolean {
return flag.length > 0 && mask.test(flag)
} |
* feat: validate `flags` earlier * chore: create `isValidFlag` * refactor: `validateFlags` * refactor: use `validateFlags` * refactor: `isValidFlag` * fix: expose `isValidFlag` * chore: remove unused import * fix: filter on falsy values * fix: allow empty strings * fix: join flags * fix: validate.test.ts
Prior behavior was to strip out invalid flag names. The validation function will now throw.
Fixes #829