Skip to content

Commit c1fd555

Browse files
committed
Fix error validators and parsing UserPrompts
1 parent f996515 commit c1fd555

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

src/commands/addBinding/settingSteps/LocalAppSettingCreateStep.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { localize } from '../../../localize';
1111
import { type IBindingSetting } from '../../../templates/IBindingTemplate';
1212
import { type ParsedInput } from '../../../templates/script/parseScriptTemplatesV2';
1313
import { nonNullProp, nonNullValue } from '../../../utils/nonNull';
14-
import { getBindingSetting } from '../../createFunction/IFunctionWizardContext';
14+
import { getBindingSetting, setBindingSetting } from '../../createFunction/IFunctionWizardContext';
1515
import { type IBindingWizardContext } from '../IBindingWizardContext';
1616

1717
export class LocalAppSettingCreateStep extends AzureWizardExecuteStep<IBindingWizardContext> {
@@ -30,6 +30,10 @@ export class LocalAppSettingCreateStep extends AzureWizardExecuteStep<IBindingWi
3030
progress.report({ message: localize('updatingLocalSettings', 'Updating {0}...', localSettingsFileName) });
3131
const appSettingName = String(nonNullValue(getBindingSetting(context, this._setting), this._setting.name));
3232
await setLocalAppSetting(context, context.projectPath, appSettingName, nonNullProp(context, this._valueKey as keyof IBindingWizardContext) as string);
33+
// if the binding isn't already set then a new one was created
34+
if (!getBindingSetting(context, this._setting)) {
35+
setBindingSetting(context, this._setting, nonNullProp(context, this._valueKey as keyof IBindingWizardContext) as string);
36+
}
3337
}
3438

3539
public shouldExecute(context: IBindingWizardContext): boolean {

src/commands/createFunction/JobsListStep.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class JobsListStep extends AzureWizardPromptStep<IFunctionWizardContext>
3636
const executeSteps: AzureWizardExecuteStep<FunctionV2WizardContext>[] = [];
3737
context.job.parsedActions.map((pa, index) => {
3838
// add index to increment the priority number
39-
executeSteps.push(actionStepFactory(pa, index));
39+
executeSteps.push(actionStepFactory(pa, index + 500));
4040
});
4141

4242
return { promptSteps, executeSteps };

src/commands/createFunction/promptStepsV2/PromptSchemaStepBase.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ export abstract class PromptSchemaStepBase<T extends FunctionV2WizardContext> ex
2020

2121
protected abstract promptAction(context: T): Promise<unknown>;
2222

23-
protected validateInput(input: string | undefined): string | undefined {
24-
if (!input && this.input.required) {
23+
protected validateInput(value: string | undefined, parsedInput: ParsedInput): string | undefined {
24+
if (!value && parsedInput.required) {
2525
return localize('promptV2StepEmpty', 'The input cannot be empty.');
2626
}
2727

28-
const validators = this.input.validators || [];
28+
const validators = parsedInput.validators || [];
2929
for (const validator of validators) {
30-
if (input) {
31-
if (new RegExp(validator.expression).test(input)) {
30+
if (value) {
31+
if (!new RegExp(validator.expression).test(value)) {
32+
// TODO: get the errorText properly
3233
return validator.errorText;
3334
}
3435
}

src/commands/createFunction/promptStepsV2/StringInputStep.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { type AzExtInputBoxOptions } from "@microsoft/vscode-azext-utils";
7-
import { localize } from "../../../localize";
87
import { type ParsedInput } from "../../../templates/script/parseScriptTemplatesV2";
98
import { type FunctionV2WizardContext } from "../IFunctionWizardContext";
109
import { PromptSchemaStepBase } from "./PromptSchemaStepBase";
@@ -20,26 +19,9 @@ export class StringInputStep<T extends FunctionV2WizardContext> extends PromptSc
2019
title: this.input.label,
2120
prompt: this.input.help,
2221
value: this.input.defaultValue,
23-
validateInput: this.validateInput
22+
validateInput: value => { return this.validateInput(value, this.input); }
2423
};
2524

2625
return await context.ui.showInputBox(options);
2726
}
28-
29-
protected validateInput(input: string | undefined): string | undefined {
30-
if (!input && this.input.required) {
31-
return localize('promptV2StepEmpty', 'The input cannot be empty.');
32-
}
33-
34-
const validators = this.input.validators || [];
35-
for (const validator of validators) {
36-
if (input) {
37-
if (!new RegExp(validator.expression).test(input)) {
38-
return validator.errorText;
39-
}
40-
}
41-
}
42-
43-
return undefined;
44-
}
4527
}

src/templates/script/parseScriptTemplatesV2.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
import { nonNullValue } from '@microsoft/vscode-azext-utils';
77
import { type ActionType, type ProjectLanguage } from '../../constants';
8+
import { localize } from '../../localize';
89
import { type ResourceType } from '../IBindingTemplate';
910
import { type FunctionV2Template } from '../IFunctionTemplate';
1011
import { TemplateSchemaVersion } from '../TemplateProviderBase';
12+
import { getResourceValue } from './parseScriptTemplates';
1113

1214
/**
1315
* Describes script template resources to be used for parsing
@@ -72,11 +74,7 @@ interface RawUserPrompt {
7274
name: string;
7375
label: string;
7476
help?: string;
75-
validators?: {
76-
// string representation of a regex
77-
expression: string;
78-
errorText: string;
79-
}[];
77+
validators?: UserPromptValidator[];
8078
value: 'string' | 'enum' | 'boolean';
8179
enum?: {
8280
value: string;
@@ -86,6 +84,11 @@ interface RawUserPrompt {
8684
placeHolder?: string;
8785
}
8886

87+
type UserPromptValidator = {
88+
expression: string;
89+
errorText: string;
90+
};
91+
8992
export interface ParsedInput extends RawUserPrompt, RawInput {
9093

9194
}
@@ -139,14 +142,22 @@ export function parseUserPrompts(rawUserPrompts: object[], resources: Resources)
139142
// all of the properties in the rawResources are in the format of "param_name" but the keys in the rawUserPrompt are in the format of "param-name"
140143
const paramName = userPrompt[key] as unknown;
141144
if (typeof paramName === 'string' && paramName.startsWith('$')) {
142-
const resourceKey = paramName.substring(1);
143-
// TODO: handle localization
144-
userPrompt[key] = resources['en'][resourceKey];
145+
userPrompt[key] = getResourceValue(resources, paramName, true) || paramName;
146+
} else if (key === 'validators' && Array.isArray(rawUserPrompt[key])) {
147+
const validators: UserPromptValidator[] = rawUserPrompt[key] as UserPromptValidator[];
148+
for (const validator of validators) {
149+
// there are a few edge cases with tokens where the format is [variables('param_name')] instead of $param_name
150+
const matches: RegExpMatchArray | null = validator.errorText.match(/\[variables\(\'(.*)\'\)\]/);
151+
// but in the resources key is $variables_paramName
152+
validator.errorText = getResourceValue(resources, matches ? '$variables_' + matches[1] : validator.errorText, true)
153+
|| localize('validatorError', 'Invalid input');
154+
}
155+
156+
userPrompt[key] = validators;
145157
}
146158
}
147159

148160
userPrompts.push(userPrompt);
149161
}
150162
return userPrompts;
151163
}
152-

0 commit comments

Comments
 (0)