Skip to content
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

Merged
merged 7 commits into from
Aug 1, 2022

Conversation

drazisil-codecov
Copy link
Contributor

@drazisil-codecov drazisil-codecov commented Jul 25, 2022

Prior behavior was to strip out invalid flag names. The validation function will now throw.

Fixes #829

@codecov
Copy link

codecov bot commented Jul 25, 2022

Codecov Report

Merging #831 (59d606e) into main (fecf6b4) will decrease coverage by 0.12%.
The diff coverage is 85.71%.

@@            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     
Flag Coverage Δ
alpine 91.94% <85.71%> (-0.13%) ⬇️
alpine-proxy 91.94% <85.71%> (-0.13%) ⬇️
alpine-without-git 91.94% <85.71%> (-0.13%) ⬇️
linux 91.94% <85.71%> (-0.13%) ⬇️
linux-without-git 91.94% <85.71%> (-0.13%) ⬇️
macos 91.94% <85.71%> (-0.13%) ⬇️
macos-without-git 91.94% <85.71%> (-0.13%) ⬇️
windows 91.94% <85.71%> (-0.13%) ⬇️
windows-without-git 91.94% <85.71%> (-0.13%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/index.ts 74.67% <60.00%> (-0.50%) ⬇️
src/helpers/validate.ts 100.00% <100.00%> (ø)
src/helpers/web.ts 80.00% <100.00%> (-0.96%) ⬇️

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function validateFlags(flag: string): boolean {
export function validateFlags(flags: string[]): void {

Copy link
Contributor Author

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

Comment on lines 18 to 19
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/

Copy link
Contributor Author

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?

// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
return mask.test(flags)
if (flag.length !== 0 && mask.test(flag) !== true) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (flag.length !== 0 && mask.test(flag) !== true) {
const invalidFlags = flags.filter(isValidFlag)
if (invalidFlags.length > 0) {

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}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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}`,

`Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received ${flag}`,
)
}
return true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return true

@mitchell-codecov
Copy link
Contributor

mitchell-codecov commented Jul 26, 2022

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 mitchell-codecov merged commit cf2585c into main Aug 1, 2022
@mitchell-codecov mitchell-codecov deleted the drazisil-codecov/issue829 branch August 1, 2022 15:37
This was referenced Aug 1, 2022
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.

Flag silently ignored when contains invalid characters
2 participants