Skip to content

Commit a90ccc7

Browse files
committed
feat: prompt.validate error support
1 parent 4c98bd2 commit a90ccc7

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

.changeset/moody-hairs-learn.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clack/prompts': patch
3+
'@clack/core': patch
4+
---
5+
6+
Add Error support for validate function

packages/core/src/prompts/prompt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface PromptOptions<Self extends Prompt> {
1313
render(this: Omit<Self, 'prompt'>): string | undefined;
1414
placeholder?: string;
1515
initialValue?: any;
16-
validate?: ((value: any) => string | undefined) | undefined;
16+
validate?: ((value: any) => string | Error | undefined) | undefined;
1717
input?: Readable;
1818
output?: Writable;
1919
debug?: boolean;
@@ -183,7 +183,7 @@ export default class Prompt {
183183
if (this.opts.validate) {
184184
const problem = this.opts.validate(this.value);
185185
if (problem) {
186-
this.error = problem;
186+
this.error = problem instanceof Error ? problem.message : problem;
187187
this.state = 'error';
188188
this.rl.write(this.value);
189189
}

packages/prompts/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export interface TextOptions {
100100
placeholder?: string;
101101
defaultValue?: string;
102102
initialValue?: string;
103-
validate?: (value: string) => string | undefined;
103+
validate?: (value: string) => string | Error | undefined;
104104
}
105105
export const text = (opts: TextOptions) => {
106106
return new TextPrompt({
@@ -136,7 +136,7 @@ export const text = (opts: TextOptions) => {
136136
export interface PasswordOptions {
137137
message: string;
138138
mask?: string;
139-
validate?: (value: string) => string | undefined;
139+
validate?: (value: string) => string | Error | undefined;
140140
}
141141
export const password = (opts: PasswordOptions) => {
142142
return new PasswordPrompt({

0 commit comments

Comments
 (0)