Skip to content

Commit

Permalink
fix(@angular-devkit/schematics-cli): correctly transform numbers from…
Browse files Browse the repository at this point in the history
… 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)
  • Loading branch information
alan-agius4 authored and clydin committed Mar 7, 2023
1 parent 57d12b1 commit dfd03aa
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/angular_devkit/schematics_cli/bin/schematics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,32 @@ function _createPromptProvider(): schema.PromptProvider {
const validator = definition.validator;
if (validator) {
question.validate = (input) => validator(input);

// Filter allows transformation of the value prior to validation
question.filter = async (input) => {
for (const type of definition.propertyTypes) {
let value;
switch (type) {
case 'string':
value = String(input);
break;
case 'integer':
case 'number':
value = Number(input);
break;
default:
value = input;
break;
}
// Can be a string if validation fails
const isValid = (await validator(value)) === true;
if (isValid) {
return value;
}
}

return input;
};
}

switch (definition.type) {
Expand Down

0 comments on commit dfd03aa

Please sign in to comment.