AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.07k stars 350 forks source link

How to (auto) trim typed text? #399

Closed baxyz closed 2 years ago

baxyz commented 2 years ago

After reading the docs, I didn't find any clue to trim the text typed by the user.

Am I missing something? Is there a workaround to do that?

For now, a space character is considered as a valid input for validators (Required, MinLength(1), ...)

mislav commented 2 years ago

A validator will enforce a certain format of value, causing the prompt to be rendered again each time a validator fails. A "transformer" (see Question.Transform field) will post-process the value entered. You can define a transformer that will clean up leading or trailing whitespace so you don't end up with those characters.

If the current offering of validators doesn't suit you, you can define your own that do the same checks but by trimming any leading/trailing space beforehand.

baxyz commented 2 years ago

Thanks, that's work lovely with a custom Required validator and a simple Transform.