-
Notifications
You must be signed in to change notification settings - Fork 355
fix(cli): validate channel name and clarify invalid-name error #3095
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
Merged
jorgemoya
merged 2 commits into
canary
from
jorgemoya/ltrac-1088-unclear-error-when-creating-a-channel-with-an-invalid-name
Jul 13, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@bigcommerce/catalyst": patch | ||
| --- | ||
|
|
||
| Validate channel names before creating a channel. `catalyst create` and `catalyst channel create` now reject names containing unsupported characters (such as an apostrophe in "Bob's Store") with a clear message that names the offending input and lists the allowed characters — letters, numbers, spaces, hyphens, and underscores — instead of surfacing an opaque API error. The interactive prompt validates as you type, and an invalid `--name` flag fails fast. |
137 changes: 137 additions & 0 deletions
137
packages/catalyst/src/cli/lib/create-channel-flow.spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { checkbox, input, select } from '@inquirer/prompts'; | ||
| import { afterEach, describe, expect, test, vi } from 'vitest'; | ||
|
|
||
| import { createChannel } from './channels'; | ||
| import { getChannelNameError, runCreateChannelFlow } from './create-channel-flow'; | ||
| import { UserActionableError } from './errors'; | ||
| import { getAvailableLocales } from './localization'; | ||
|
|
||
| vi.mock('@inquirer/prompts', () => ({ | ||
| input: vi.fn(), | ||
| select: vi.fn(), | ||
| checkbox: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('./channels', () => ({ | ||
| createChannel: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('./localization', () => ({ | ||
| getAvailableLocales: vi.fn(), | ||
| })); | ||
|
|
||
| const mockInput = vi.mocked(input); | ||
| const mockSelect = vi.mocked(select); | ||
| const mockCheckbox = vi.mocked(checkbox); | ||
| const mockCreateChannel = vi.mocked(createChannel); | ||
| const mockGetAvailableLocales = vi.mocked(getAvailableLocales); | ||
|
|
||
| const baseOptions = { | ||
| storeHash: 'test-store', | ||
| accessToken: 'test-token', | ||
| apiHost: 'api.bigcommerce.com', | ||
| cliApiOrigin: 'https://cxm-prd.bigcommerceapp.com', | ||
| }; | ||
|
|
||
| afterEach(() => { | ||
| vi.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('getChannelNameError', () => { | ||
| test.each([ | ||
| ['My Store', 'a plain name'], | ||
| ['My-Store_2', 'hyphens and underscores'], | ||
| ['Café Münchën', 'accented / non-ASCII letters'], | ||
| [' Padded ', 'surrounding whitespace'], | ||
| ])('accepts %j (%s)', (name) => { | ||
| expect(getChannelNameError(name)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ["Bob's Store", 'an apostrophe'], | ||
| ['Store & Co', 'an ampersand'], | ||
| ['Store #1', 'a hash'], | ||
| ['Store (US)', 'parentheses'], | ||
| ])('rejects %j (%s) with an actionable message', (name) => { | ||
| const error = getChannelNameError(name); | ||
|
|
||
| expect(error).toContain(name); | ||
| expect(error).toContain('not a valid channel name'); | ||
| expect(error).toContain('letters, numbers, spaces, hyphens (-), and underscores (_)'); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ['', 'an empty string'], | ||
| [' ', 'only whitespace'], | ||
| ])('rejects %j (%s) as empty', (name) => { | ||
| expect(getChannelNameError(name)).toBe('Channel name cannot be empty.'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('runCreateChannelFlow', () => { | ||
| test('throws UserActionableError for an invalid --name before calling the API', async () => { | ||
| await expect(runCreateChannelFlow({ ...baseOptions, name: "Bob's Store" })).rejects.toThrow( | ||
| UserActionableError, | ||
| ); | ||
|
|
||
| await expect(runCreateChannelFlow({ ...baseOptions, name: "Bob's Store" })).rejects.toThrow( | ||
| /not a valid channel name/, | ||
| ); | ||
|
|
||
| expect(mockCreateChannel).not.toHaveBeenCalled(); | ||
| expect(mockInput).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('validates the interactive name prompt with getChannelNameError', async () => { | ||
| mockInput.mockResolvedValue('My Store'); | ||
| mockSelect | ||
| .mockResolvedValueOnce('en') // default locale | ||
| .mockResolvedValueOnce(false) // add additional languages? | ||
| .mockResolvedValueOnce(false); // install sample data? | ||
| mockGetAvailableLocales.mockResolvedValue([{ name: 'English', value: 'en' }]); | ||
| mockCreateChannel.mockResolvedValue({ | ||
| channelId: 42, | ||
| storefrontToken: 'token', | ||
| envVars: {}, | ||
| }); | ||
|
|
||
| await runCreateChannelFlow(baseOptions); | ||
|
|
||
| const validate = mockInput.mock.calls[0]?.[0]?.validate; | ||
|
|
||
| expect(validate).toBeTypeOf('function'); | ||
| expect(validate?.("Bob's Store")).toBe( | ||
| '"Bob\'s Store" is not a valid channel name. Channel names may contain only letters, numbers, spaces, hyphens (-), and underscores (_).', | ||
| ); | ||
| expect(validate?.('My Store')).toBe(true); | ||
| }); | ||
|
|
||
| test('passes a valid --name straight through to createChannel', async () => { | ||
| mockCreateChannel.mockResolvedValue({ | ||
| channelId: 42, | ||
| storefrontToken: 'token', | ||
| envVars: {}, | ||
| }); | ||
|
|
||
| await runCreateChannelFlow({ | ||
| ...baseOptions, | ||
| name: 'My Store', | ||
| locale: 'en', | ||
| additionalLocales: [], | ||
| sampleData: false, | ||
| }); | ||
|
|
||
| expect(mockInput).not.toHaveBeenCalled(); | ||
| expect(mockSelect).not.toHaveBeenCalled(); | ||
| expect(mockCheckbox).not.toHaveBeenCalled(); | ||
| expect(mockCreateChannel).toHaveBeenCalledWith( | ||
| 'My Store', | ||
| 'en', | ||
| [], | ||
| false, | ||
| baseOptions.storeHash, | ||
| baseOptions.accessToken, | ||
| baseOptions.cliApiOrigin, | ||
| ); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.