How can you validate user input in a Yeoman prompt?

Prepare for the Yeoman (YN) Test. Use flashcards and multiple-choice questions, all with hints and explanations, to get ready for your upcoming exam. Enhance your learning experience!

Multiple Choice

How can you validate user input in a Yeoman prompt?

Explanation:
In Yeoman prompts, you validate input by providing a validate function on each prompt object. This function runs as the user enters their answer and must return true when the input is valid or return a string with an error message to show feedback and block advancing. This per-prompt validation lets you tailor rules for every piece of input—for example, ensuring a non-empty project name, matching a specific pattern, or checking numeric ranges—before the generator proceeds. If you wanted to reuse the same checks, you could extract them into a shared utility, but the standard and most reliable approach is to attach a validate function to each prompt. Validating after generation is not ideal because it won’t prevent the creation of invalid data or files in the first place.

In Yeoman prompts, you validate input by providing a validate function on each prompt object. This function runs as the user enters their answer and must return true when the input is valid or return a string with an error message to show feedback and block advancing. This per-prompt validation lets you tailor rules for every piece of input—for example, ensuring a non-empty project name, matching a specific pattern, or checking numeric ranges—before the generator proceeds. If you wanted to reuse the same checks, you could extract them into a shared utility, but the standard and most reliable approach is to attach a validate function to each prompt. Validating after generation is not ideal because it won’t prevent the creation of invalid data or files in the first place.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy