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

Add basic validation to database names #23842

Merged
merged 11 commits into from
Jul 14, 2023
11 changes: 9 additions & 2 deletions extensions/mssql/src/objectManagement/ui/databaseDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,17 @@ export class DatabaseDialog extends ObjectManagementDialogBase<Database, Databas
//#region Create Database
private initializeGeneralSection(): azdata.GroupContainer {
let containers: azdata.Component[] = [];
this.nameInput = this.createInputBox(localizedConstants.NameText, async () => {
const props: azdata.InputBoxProperties = {
ariaLabel: localizedConstants.NameText,
width: DefaultInputWidth,
required: true,
maxLength: 20
};

this.nameInput = this.createTextInputBox(localizedConstants.NameText, async () => {
this.objectInfo.name = this.nameInput.value;
await this.runValidation(false);
barbaravaldez marked this conversation as resolved.
Show resolved Hide resolved
});
}, '', props);
containers.push(this.createLabelInputContainer(localizedConstants.NameText, this.nameInput));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be marked as required so it displays in the UI (* next to the label)


if (this.viewInfo.loginNames?.length > 0) {
Expand Down
15 changes: 15 additions & 0 deletions extensions/mssql/src/ui/dialogBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,21 @@ export abstract class DialogBase<DialogResult> {
return this.createInputBox(ariaLabel, textChangeHandler, value, enabled, 'password', width);
}

protected createTextInputBox(ariaLabel: string, textChangeHandler: (newValue: string) => Promise<void>, value: string = '', properties: azdata.InputBoxProperties, customValidation?: () => Promise<boolean>): azdata.InputBoxComponent {
Charles-Gagnon marked this conversation as resolved.
Show resolved Hide resolved
const textValidation = new RegExp('^[a-zA-Z0-9_]*$');
properties.ariaLabel = ariaLabel;
barbaravaldez marked this conversation as resolved.
Show resolved Hide resolved
properties.validationErrorMessage = customValidation ? properties.validationErrorMessage : uiLoc.OnlyAlphanumericValuesAllowed;
const inputbox: azdata.InputBoxComponent = this.modelView.modelBuilder.inputBox().withProps(properties).withValidation(customValidation ? customValidation : () => {
return textValidation.test(inputbox.value)
}).component();
this.disposables.push(inputbox.onTextChanged(async () => {
await textChangeHandler(inputbox.value!);
this.onFormFieldChange();
await this.runValidation(false);
}));
return inputbox;
}

protected createInputBox(ariaLabel: string, textChangeHandler: (newValue: string) => Promise<void>, value: string = '', enabled: boolean = true, type: azdata.InputBoxInputType = 'text', width: number = DefaultInputWidth, required?: boolean, min?: number, max?: number): azdata.InputBoxComponent {
const inputbox = this.modelView.modelBuilder.inputBox().withProps({ inputType: type, enabled: enabled, ariaLabel: ariaLabel, value: value, width: width, required: required, min: min, max: max }).component();
this.disposables.push(inputbox.onTextChanged(async () => {
Expand Down
1 change: 1 addition & 0 deletions extensions/mssql/src/ui/localizedConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const NoActionScriptedMessage: string = localize('mssql.ui.noActionScript
export const ScriptGeneratedText: string = localize('mssql.ui.scriptGenerated', "Script has been generated successfully. You can close the dialog to view it in the newly opened editor.")
export const GeneratingScriptText: string = localize('mssql.ui.generatingScript', "Generating script...");
export const GeneratingScriptCompletedText: string = localize('mssql.ui.generatingScriptCompleted', "Script generated");
export const OnlyAlphanumericValuesAllowed = localize('objectManagement.OnlyAlphanumericValuesAllowed', "Only alphanumeric characters allowed");

export function scriptError(error: string): string {
return localize('mssql.ui.scriptError', "An error occurred while generating the script. {0}", error);
Expand Down
Loading