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: Check the name without a number at the beginning when creating a new dialog #4493

Merged
merged 5 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 7 additions & 4 deletions Composer/packages/client/src/pages/design/createDialogModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { Stack, StackItem } from 'office-ui-fabric-react/lib/Stack';
import { TextField } from 'office-ui-fabric-react/lib/TextField';
import { useRecoilValue } from 'recoil';
import { RecognizerSchema, useRecognizerConfig, useShellApi } from '@bfc/extension-client';
import { DialogFactory, SDKKinds } from '@bfc/shared';
import { DialogFactory, SDKKinds, DialogUtils } from '@bfc/shared';
import { DialogWrapper, DialogTypes } from '@bfc/ui-shared';

import { DialogCreationCopy, nameRegex } from '../../constants';
import { DialogCreationCopy } from '../../constants';
import { StorageFolder } from '../../recoilModel/types';
import { FieldConfig, useForm } from '../../hooks/useForm';
import { actionsSeedState, schemasState, validateDialogSelectorFamily } from '../../recoilModel';
Expand Down Expand Up @@ -46,9 +46,12 @@ export const CreateDialogModal: React.FC<CreateDialogModalProps> = (props) => {
name: {
required: true,
validate: (value) => {
if (!nameRegex.test(value)) {
return formatMessage('Spaces and special characters are not allowed. Use letters, numbers, -, or _.');
try {
DialogUtils.validateDialogName(value);
} catch (error) {
return error.message;
}

if (dialogs.some((dialog) => dialog.id.toLowerCase() === value.toLowerCase())) {
return formatMessage('Duplicate dialog name');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { validateDialogName } from '../../src/dialogUtils/validateDialogName';

const error = new Error(
"Spaces and special characters are not allowed. Use letters, numbers, -, or _ and don't use number at the beginning."
);
const emptyError = new Error('The file name can not be empty');

describe('check dialog name', () => {
it('don not support special characters', () => {
expect(() => validateDialogName('*a')).toThrowError(error);
expect(() => validateDialogName('c*a')).toThrowError(error);
expect(() => validateDialogName('c a')).toThrowError(error);
expect(() => validateDialogName('')).toThrowError(emptyError);
});

it('don not support number at the beginning.', () => {
expect(() => validateDialogName('1a')).toThrowError(error);
expect(() => validateDialogName('a1')).not.toThrowError();
});
});
1 change: 1 addition & 0 deletions Composer/packages/lib/shared/src/dialogUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
// Licensed under the MIT License.

export * from './jsonTracker';
export * from './validateDialogName';
20 changes: 20 additions & 0 deletions Composer/packages/lib/shared/src/dialogUtils/validateDialogName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import formatMessage from 'format-message';

export const validateDialogName = (name: string) => {
const nameRegex = /^[a-zA-Z][a-zA-Z0-9-_]*$/;

if (!name) {
throw new Error(formatMessage('The file name can not be empty'));
}

if (!nameRegex.test(name)) {
throw new Error(
formatMessage(
"Spaces and special characters are not allowed. Use letters, numbers, -, or _ and don't use number at the beginning."
)
);
}
};
20 changes: 11 additions & 9 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ import fs from 'fs';

import axios from 'axios';
import { autofixReferInDialog } from '@bfc/indexers';
import { getNewDesigner, FileInfo, Diagnostic, IBotProject, DialogSetting, FileExtensions, Skill } from '@bfc/shared';
import {
getNewDesigner,
FileInfo,
Diagnostic,
IBotProject,
DialogSetting,
FileExtensions,
Skill,
DialogUtils,
} from '@bfc/shared';
import merge from 'lodash/merge';
import { UserIdentity, ExtensionContext } from '@bfc/extension';
import { FeedbackType, generate } from '@microsoft/bf-generate-library';
Expand Down Expand Up @@ -409,21 +418,14 @@ export class BotProject implements IBotProject {
};

public validateFileName = (name: string) => {
const nameRegex = /^[a-zA-Z0-9-_]+$/;
const { fileId, fileType } = parseFileName(name, '');

let fileName = fileId;
if (fileType === '.dialog') {
fileName = Path.basename(name, fileType);
}

if (!fileName) {
throw new Error('The file name can not be empty');
}

if (!nameRegex.test(fileName)) {
throw new Error('Spaces and special characters are not allowed. Use letters, numbers, -, or _.');
}
DialogUtils.validateDialogName(fileName);
};

public createFile = async (name: string, content = '') => {
Expand Down