Skip to content
13 changes: 9 additions & 4 deletions src/helpers/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ export function validateURL(url: string): boolean {
return validator.isURL(url, { require_protocol: true })
}

export function validateFlags(flag: string): boolean {
export function isValidFlag(flag: string): boolean {
// eslint-disable-next-line no-useless-escape
const mask = /^[\w\.\-]{1,45}$/
if (flag.length !== 0 && mask.test(flag) !== true) {
return flag.length === 0 || mask.test(flag)
}

export function validateFlags(flags: string[]): void {
const invalidFlags = flags.filter(flag => isValidFlag(flag) !== true)

if (invalidFlags.length > 0) {
throw new Error(
`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}`,
)
}
return true
}

/**
Expand Down
11 changes: 3 additions & 8 deletions src/helpers/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
UploaderInputs,
} from '../types'
import { info } from './logger'
import * as validateHelpers from './validate'

/**
*
Expand All @@ -32,15 +31,11 @@ export function populateBuildParams(
serviceParams.name = args.name || envs.CODECOV_NAME || ''
serviceParams.tag = args.tag || ''

let flags: string[]
if (typeof args.flags === 'object') {
flags = [...args.flags]
if (typeof args.flags === "string") {
serviceParams.flags = args.flags
} else {
flags = String(args.flags || '').split(',')
serviceParams.flags = args.flags.join(',')
}
serviceParams.flags = flags
.filter(flag => validateHelpers.validateFlags(flag))
.join(',')

serviceParams.parent = args.parent || ''
return serviceParams
Expand Down
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { generateGcovCoverageFiles } from './helpers/gcov'
import { generateXcodeCoverageFiles } from './helpers/xcode'
import { argAsArray } from './helpers/util'
import { checkSlug } from './helpers/checkSlug'
import { validateFlags } from './helpers/validate'

/**
*
Expand Down Expand Up @@ -119,6 +120,15 @@ export async function main(

info(generateHeader(getVersion()))

let flags: string[]
if (typeof args.flags === 'object') {
flags = [...args.flags]
} else {
flags = String(args.flags || '').split(',')
}

validateFlags(flags)

// #endregion
// #region == Step 2: detect if we are in a git repo
const projectRoot = args.rootDir || fetchGitRoot()
Expand Down
10 changes: 5 additions & 5 deletions test/helpers/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ describe('Input Validators', () => {

describe('Flags', () => {
it('Should pass without a dash', () => {
expect(validate.validateFlags('moo')).toBe(true)
expect(validate.isValidFlag('moo')).toBe(true)
})
it('Should pass with a dash', () => {
expect(validate.validateFlags('moo-foor')).toBe(true)
expect(validate.isValidFlag('moo-foor')).toBe(true)
})

it('Should pass with a period in the middle', () => {
expect(validate.validateFlags('moo.foor')).toBe(true)
expect(validate.isValidFlag('moo.foor')).toBe(true)
})

it('Should pass with a dash at the start', () => {
expect(validate.validateFlags('-moo-foor')).toBe(true)
expect(validate.isValidFlag('-moo-foor')).toBe(true)
})

it("should throw when they they not match the pattern", () => {
Expand All @@ -55,7 +55,7 @@ describe('Input Validators', () => {
const expectedErrorMessage = "Flags must consist only of alphanumeric characters, '_', '-', or '.' and not exceed 45 characters. Received flag/subflag"

// act
expect(() => validate.validateFlags(invalidFlagName)).toThrowError(expectedErrorMessage)
expect(() => validate.validateFlags([invalidFlagName])).toThrowError(expectedErrorMessage)
})
})

Expand Down