mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-19 01:25:36 -05:00
Add validation for new file names for sql projects (#21601)
* Add validation for new file names for sql projects * Addres comments and add validation for new project dialog * Address comments * Address comments on test * Fix tests * Remove extra error messages and rename file * Address comments * Fix tests * Add test file back
This commit is contained in:
@@ -731,3 +731,35 @@ export async function getTargetPlatformFromServerVersion(serverInfo: azdataType.
|
||||
|
||||
return targetPlatform;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a given character is a valid filename character
|
||||
* @param c Character to validate
|
||||
*/
|
||||
export function isValidFilenameCharacter(c: string): boolean {
|
||||
return getDataWorkspaceExtensionApi().isValidFilenameCharacter(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces invalid filename characters in a string with underscores
|
||||
* @param s The string to be sanitized for a filename
|
||||
*/
|
||||
export function sanitizeStringForFilename(s: string): string {
|
||||
return getDataWorkspaceExtensionApi().sanitizeStringForFilename(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the string is a valid filename
|
||||
* @param name filename to check
|
||||
*/
|
||||
export function isValidBasename(name?: string): boolean {
|
||||
return getDataWorkspaceExtensionApi().isValidBasename(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns specific error message if file name is invalid
|
||||
* @param name filename to check
|
||||
*/
|
||||
export function isValidBasenameErrorMessage(name?: string): string {
|
||||
return getDataWorkspaceExtensionApi().isValidBasenameErrorMessage(name);
|
||||
}
|
||||
|
||||
@@ -651,7 +651,7 @@ export class ProjectsController {
|
||||
}
|
||||
|
||||
private async promptForNewObjectName(itemType: templates.ProjectScriptType, _project: ISqlProject, folderPath: string, fileExtension?: string, defaultName?: string): Promise<string | undefined> {
|
||||
const suggestedName = defaultName ?? itemType.friendlyName.replace(/\s+/g, '');
|
||||
const suggestedName = utils.sanitizeStringForFilename(defaultName ?? itemType.friendlyName.replace(/\s+/g, ''));
|
||||
let counter: number = 0;
|
||||
|
||||
do {
|
||||
@@ -662,6 +662,9 @@ export class ProjectsController {
|
||||
const itemObjectName = await vscode.window.showInputBox({
|
||||
prompt: constants.newObjectNamePrompt(itemType.friendlyName),
|
||||
value: `${suggestedName}${counter}`,
|
||||
validateInput: (value) => {
|
||||
return utils.isValidBasename(value) ? undefined : utils.isValidBasenameErrorMessage(value);
|
||||
},
|
||||
ignoreFocusOut: true,
|
||||
});
|
||||
|
||||
@@ -1279,7 +1282,7 @@ export class ProjectsController {
|
||||
prompt: constants.autorestProjectName,
|
||||
value: defaultName,
|
||||
validateInput: (value) => {
|
||||
return value.trim() ? undefined : constants.nameMustNotBeEmpty;
|
||||
return utils.isValidBasename(value.trim()) ? undefined : utils.isValidBasenameErrorMessage(value.trim());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import { cssStyles } from '../common/uiConstants';
|
||||
import { ImportDataModel } from '../models/api/import';
|
||||
import { Deferred } from '../common/promise';
|
||||
import { getConnectionName, mapExtractTargetEnum } from './utils';
|
||||
import { exists, getAzdataApi, getDataWorkspaceExtensionApi } from '../common/utils';
|
||||
import { exists, getAzdataApi, getDataWorkspaceExtensionApi, isValidBasename, isValidBasenameErrorMessage, sanitizeStringForFilename } from '../common/utils';
|
||||
|
||||
export class CreateProjectFromDatabaseDialog {
|
||||
public dialog: azdataType.window.Dialog;
|
||||
@@ -218,7 +218,7 @@ export class CreateProjectFromDatabaseDialog {
|
||||
}
|
||||
|
||||
public setProjectName() {
|
||||
this.projectNameTextBox!.value = newProjectTool.defaultProjectNameFromDb(<string>this.sourceDatabaseDropDown!.value);
|
||||
this.projectNameTextBox!.value = newProjectTool.defaultProjectNameFromDb(sanitizeStringForFilename(<string>this.sourceDatabaseDropDown!.value));
|
||||
}
|
||||
|
||||
private createSourceConnectionComponent(view: azdataType.ModelView): azdataType.InputBoxComponent {
|
||||
@@ -290,17 +290,26 @@ export class CreateProjectFromDatabaseDialog {
|
||||
}
|
||||
|
||||
private createProjectNameRow(view: azdataType.ModelView): azdataType.FlexContainer {
|
||||
this.projectNameTextBox = view.modelBuilder.inputBox().withProps({
|
||||
ariaLabel: constants.projectNamePlaceholderText,
|
||||
placeHolder: constants.projectNamePlaceholderText,
|
||||
required: true,
|
||||
width: cssStyles.createProjectFromDatabaseTextboxWidth
|
||||
}).component();
|
||||
this.projectNameTextBox = view.modelBuilder.inputBox().withValidation(
|
||||
component => isValidBasename(component.value)
|
||||
)
|
||||
.withProps({
|
||||
ariaLabel: constants.projectNamePlaceholderText,
|
||||
placeHolder: constants.projectNamePlaceholderText,
|
||||
required: true,
|
||||
width: cssStyles.createProjectFromDatabaseTextboxWidth
|
||||
}).component();
|
||||
|
||||
this.projectNameTextBox.onTextChanged(() => {
|
||||
this.projectNameTextBox!.value = this.projectNameTextBox!.value?.trim();
|
||||
void this.projectNameTextBox!.updateProperty('title', this.projectNameTextBox!.value);
|
||||
this.tryEnableCreateButton();
|
||||
this.projectNameTextBox.onTextChanged(text => {
|
||||
const errorMessage = isValidBasenameErrorMessage(text);
|
||||
if (errorMessage) {
|
||||
// Set validation error message if project name is invalid
|
||||
void this.projectNameTextBox!.updateProperty('validationErrorMessage', errorMessage);
|
||||
} else {
|
||||
this.projectNameTextBox!.value = this.projectNameTextBox!.value?.trim();
|
||||
void this.projectNameTextBox!.updateProperty('title', this.projectNameTextBox!.value);
|
||||
this.tryEnableCreateButton();
|
||||
}
|
||||
});
|
||||
|
||||
const projectNameLabel = view.modelBuilder.text().withProps({
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as path from 'path';
|
||||
import * as constants from '../common/constants';
|
||||
import { exists, getVscodeMssqlApi } from '../common/utils';
|
||||
import { exists, getVscodeMssqlApi, isValidBasename, isValidBasenameErrorMessage, sanitizeStringForFilename } from '../common/utils';
|
||||
import { IConnectionInfo } from 'vscode-mssql';
|
||||
import { defaultProjectNameFromDb, defaultProjectSaveLocation } from '../tools/newProjectTool';
|
||||
import { ImportDataModel } from '../models/api/import';
|
||||
@@ -70,9 +70,9 @@ export async function createNewProjectFromDatabaseWithQuickpick(connectionInfo?:
|
||||
const projectName = await vscode.window.showInputBox(
|
||||
{
|
||||
title: constants.projectNamePlaceholderText,
|
||||
value: defaultProjectNameFromDb(selectedDatabase),
|
||||
value: defaultProjectNameFromDb(sanitizeStringForFilename(selectedDatabase)),
|
||||
validateInput: (value) => {
|
||||
return value ? undefined : constants.nameMustNotBeEmpty;
|
||||
return isValidBasename(value) ? undefined : isValidBasenameErrorMessage(value);
|
||||
},
|
||||
ignoreFocusOut: true
|
||||
});
|
||||
|
||||
@@ -29,4 +29,3 @@ describe('Tests to verify dialog utils functions', function (): void {
|
||||
should(getDefaultDockerImageWithTag('AzureV12', 'mcr.microsoft.com/azure-sql-edge', azureLiteImageInfo)).equals(`${azureLiteImageInfo?.name}`, 'Unexpected docker image returned for target platform Azure Azure lite base image');
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user