Skip to content

Commit fd63e29

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/cli): coerce prompt answers to requested property types
This change converts answers from prompts into the property type requested from the schema. This allows properties that expect a number to correctly validate when an input prompt is used.
1 parent 83c9120 commit fd63e29

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

packages/angular/cli/models/schematic-command.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,32 @@ export abstract class SchematicCommand<
322322
const validator = definition.validator;
323323
if (validator) {
324324
question.validate = input => validator(input);
325+
326+
// Filter allows transformation of the value prior to validation
327+
question.filter = async (input) => {
328+
for (const type of definition.propertyTypes) {
329+
let value;
330+
switch (type) {
331+
case 'string':
332+
value = String(input);
333+
break;
334+
case 'integer':
335+
case 'number':
336+
value = Number(input);
337+
break;
338+
default:
339+
value = input;
340+
break;
341+
}
342+
// Can be a string if validation fails
343+
const isValid = (await validator(value)) === true;
344+
if (isValid) {
345+
return value;
346+
}
347+
}
348+
349+
return input;
350+
};
325351
}
326352

327353
switch (definition.type) {
@@ -339,6 +365,17 @@ export abstract class SchematicCommand<
339365
};
340366
});
341367
break;
368+
case 'input':
369+
if (
370+
definition.propertyTypes.size === 1 &&
371+
(definition.propertyTypes.has('number') ||
372+
definition.propertyTypes.has('integer'))
373+
) {
374+
question.type = 'number';
375+
} else {
376+
question.type = 'input';
377+
}
378+
break;
342379
default:
343380
question.type = definition.type;
344381
break;

0 commit comments

Comments
 (0)