Skip to content

Commit dfd03aa

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/schematics-cli): correctly transform numbers from prompts
This commits ports the same logic from the `@angular/cli` (https://github.com/angular/angular-cli/blob/693d78b80de9e91bd5313353ea5c524742196e0a/packages/angular/cli/src/command-builder/schematics-command-module.ts#L183-L211) to correctly handle numbers in prompts. Closes #24817 (cherry picked from commit d15d44d)
1 parent 57d12b1 commit dfd03aa

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

packages/angular_devkit/schematics_cli/bin/schematics.ts

+26
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,32 @@ function _createPromptProvider(): schema.PromptProvider {
7979
const validator = definition.validator;
8080
if (validator) {
8181
question.validate = (input) => validator(input);
82+
83+
// Filter allows transformation of the value prior to validation
84+
question.filter = async (input) => {
85+
for (const type of definition.propertyTypes) {
86+
let value;
87+
switch (type) {
88+
case 'string':
89+
value = String(input);
90+
break;
91+
case 'integer':
92+
case 'number':
93+
value = Number(input);
94+
break;
95+
default:
96+
value = input;
97+
break;
98+
}
99+
// Can be a string if validation fails
100+
const isValid = (await validator(value)) === true;
101+
if (isValid) {
102+
return value;
103+
}
104+
}
105+
106+
return input;
107+
};
82108
}
83109

84110
switch (definition.type) {

0 commit comments

Comments
 (0)