Skip to content

Commit 261333e

Browse files
committed
feat: prompt.validate error support
1 parent 90f8e3d commit 261333e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

packages/core/src/prompts/prompt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export interface PromptOptions<Self extends Prompt> {
4242
render(this: Omit<Self, 'prompt'>): string | void;
4343
placeholder?: string;
4444
initialValue?: any;
45-
validate?: ((value: any) => string | void) | undefined;
45+
validate?: ((value: any) => string | Error | void) | undefined;
4646
input?: Readable;
4747
output?: Writable;
4848
debug?: boolean;
@@ -180,7 +180,7 @@ export default class Prompt {
180180
if (this.opts.validate) {
181181
const problem = this.opts.validate(this.value);
182182
if (problem) {
183-
this.error = problem;
183+
this.error = problem instanceof Error ? problem.message : problem;
184184
this.state = 'error';
185185
this.rl.write(this.value);
186186
}

packages/prompts/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export interface TextOptions {
9898
placeholder?: string;
9999
defaultValue?: string;
100100
initialValue?: string;
101-
validate?: (value: string) => string | void;
101+
validate?: (value: string) => string | Error | void;
102102
}
103103
export const text = (opts: TextOptions) => {
104104
return new TextPrompt({
@@ -134,7 +134,7 @@ export const text = (opts: TextOptions) => {
134134
export interface PasswordOptions {
135135
message: string;
136136
mask?: string;
137-
validate?: (value: string) => string | void;
137+
validate?: (value: string) => string | Error | void;
138138
}
139139
export const password = (opts: PasswordOptions) => {
140140
return new PasswordPrompt({

0 commit comments

Comments
 (0)