Skip to content

Commit

Permalink
feat(@clack/core,@clack/prompts): add Error support for `prompt.val…
Browse files Browse the repository at this point in the history
…idate` (#165)

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
orochaa and natemoo-re authored Jan 9, 2025
1 parent 98925e3 commit 8093f3c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/moody-hairs-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@clack/prompts': patch
'@clack/core': patch
---

Adds `Error` support to the `validate` function
4 changes: 2 additions & 2 deletions packages/core/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface PromptOptions<Self extends Prompt> {
render(this: Omit<Self, 'prompt'>): string | undefined;
placeholder?: string;
initialValue?: any;
validate?: ((value: any) => string | undefined) | undefined;
validate?: ((value: any) => string | Error | undefined) | undefined;
input?: Readable;
output?: Writable;
debug?: boolean;
Expand Down Expand Up @@ -207,7 +207,7 @@ export default class Prompt {
if (this.opts.validate) {
const problem = this.opts.validate(this.value);
if (problem) {
this.error = problem;
this.error = problem instanceof Error ? problem.message : problem;
this.state = 'error';
this.rl?.write(this.value);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface TextOptions {
placeholder?: string;
defaultValue?: string;
initialValue?: string;
validate?: (value: string) => string | undefined;
validate?: (value: string) => string | Error | undefined;
}
export const text = (opts: TextOptions) => {
return new TextPrompt({
Expand Down Expand Up @@ -138,7 +138,7 @@ export const text = (opts: TextOptions) => {
export interface PasswordOptions {
message: string;
mask?: string;
validate?: (value: string) => string | undefined;
validate?: (value: string) => string | Error | undefined;
}
export const password = (opts: PasswordOptions) => {
return new PasswordPrompt({
Expand Down

0 comments on commit 8093f3c

Please sign in to comment.