bombshell-dev / clack

Effortlessly build beautiful command-line apps
https://clack.cc
5.53k stars 90 forks source link

[Request] Add `Error` support for `prompt.validate` #160

Open Mist3rBru opened 1 year ago

Mist3rBru commented 1 year ago

Is your feature request related to a problem? Please describe. I would like to create custom errors, and return they on validate, without the need to call error.message every time.

Describe the solution you'd like Make prompt.validate accept Error instances as return type. Then internally prompt.validate could verify if the result is a Error instance, then call error.message.

Example:

interface TextOptions {
  message: string;
  ...
  validate?: (value: string) => string | Error | void;
}

await p.text({
  message: 'Type your alias key:',
  validate: value => {
    if(value.length !== 1) {
      return new InvalidParamError('alias', 'max length is 1')
    }
  }
})

Describe alternatives you've considered N/A

Additional context I'd be happy to contribute this.