fix: throw error when flags do not match pattern#831
Merged
mitchell-codecov merged 7 commits intomainfrom Aug 1, 2022
Merged
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.
|
mitchell-codecov
suggested changes
Jul 26, 2022
src/helpers/validate.ts
Outdated
| } | ||
|
|
||
| export function validateFlags(flags: string): boolean { | ||
| export function validateFlags(flag: string): boolean { |
Contributor
There was a problem hiding this comment.
Suggested change
| export function validateFlags(flag: string): boolean { | |
| export function validateFlags(flags: string[]): void { |
Contributor
Author
There was a problem hiding this comment.
This is being passed a single flag as part of a filter command, I believe, @mitchell-codecov
Comment on lines
18
to
19
| // eslint-disable-next-line no-useless-escape | ||
| const mask = /^[\w\.\-]{1,45}$/ |
Contributor
There was a problem hiding this comment.
Suggested change
| // eslint-disable-next-line no-useless-escape | |
| const mask = /^[\w\.\-]{1,45}$/ |
Contributor
Author
There was a problem hiding this comment.
@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) { |
Contributor
There was a problem hiding this comment.
Suggested change
| 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}`, |
Contributor
There was a problem hiding this comment.
Suggested change
| `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 |
Contributor
There was a problem hiding this comment.
Suggested change
| return true |
Contributor
|
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
mitchell-codecov
approved these changes
Aug 1, 2022
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prior behavior was to strip out invalid flag names. The validation function will now throw.
Fixes #829