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: Handle empty/white space project and channel names #107

Merged
merged 3 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/commands/channels/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ the command currently requires that both the
tryDomainOptionsValidation(options: any, domainOptions: DomainOption[]) {
super.tryDomainOptionsValidation(options, domainOptions)

validateChannelName(options.name)
validateChannelName(options.name, 'name')

if (options.color !== undefined) {
validateChannelColor(options.color)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/channels/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Use this command to change the name of a channel in a project.`
tryDomainOptionsValidation(options: any, domainOptions: DomainOption[]) {
super.tryDomainOptionsValidation(options, domainOptions)

validateChannelName(options['new-name'])
validateChannelName(options['new-name'], 'new-name')
}

async buildRequestParameters(options: Partial<Output>): Promise<ChannelsRenameParams> {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/projects/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import makeDebug from 'debug'
import * as MixFlags from '../../utils/flags'
import * as ProjectsAPI from '../../mix/api/projects'
import {asArray} from '../../utils/as-array'
import {DomainOption} from '../../utils/validations'
import {DomainOption, validateChannelName} from '../../utils/validations'
import {MixClient, MixResponse, MixResult, ProjectsCreateParams} from '../../mix/types'
import MixCommand from '../../utils/base/mix-command'
import {pluralize as s} from '../../utils/format'
Expand Down Expand Up @@ -87,7 +87,7 @@ provide a description of your project using the --description flag.`

get domainOptions(): DomainOption[] {
debug('get domainOptions()')
return ['locale[]', 'organization']
return ['locale[]', 'name', 'organization']
}

async buildRequestParameters(options: Partial<flags.Output>): Promise<ProjectsCreateParams> {
Expand Down Expand Up @@ -153,6 +153,10 @@ provide a description of your project using the --description flag.`
const modes = asArray(options.modes)
this.validateChannelsAndModes(channels, modes)

for (const channel of channels) {
validateChannelName(channel, MixFlags.channelMultipleFlag.char?.toString() || 'flag')
}

const {
'child-data-compliant': isChildDataCompliant,
description,
Expand Down
7 changes: 5 additions & 2 deletions src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type DomainOption =
| 'locale'
| 'locale[]'
| 'mix-app'
| 'name'
| 'offset'
| 'organization'
| 'project'
Expand Down Expand Up @@ -70,6 +71,8 @@ const validationSchemes = {
message: `Expected each locale in flag 'locale' to match ${localeRegEx}`}).array(),
'mix-app': z.number().positive({
message: "Expected flag 'mix-app' to have a value greater than 0"}),
name: z.string().min(1, {
message: "Project name can't be empty or consist only of whitespace"}),
offset: z.number().nonnegative({
message: "Expected flag 'offset' to have a value greater than or equal to 0"}),
organization: z.number().positive({
Expand Down Expand Up @@ -125,11 +128,11 @@ export function validateChannelColor(color: string): void {
}
}

export function validateChannelName(name: string) {
export function validateChannelName(name: string, optionName: string) {
debug('validateChannelName()')
if (name.trim().length === 0) {
throw (eInvalidValue("Channel name can't be empty or consist only of whitespace.", [
'Supply a non-empty value to --name flag',
`Supply a non-empty value to flag '${optionName}'`,
]))
}
}
Expand Down