Skip to content

Commit 8093f3c

Browse files
orochaanatemoo-re
andauthoredJan 9, 2025··
feat(@clack/core,@clack/prompts): add Error support for prompt.validate (#165)
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
1 parent 98925e3 commit 8093f3c

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed
 

‎.changeset/moody-hairs-learn.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@clack/prompts': patch
3+
'@clack/core': patch
4+
---
5+
6+
Adds `Error` support to the `validate` function

‎packages/core/src/prompts/prompt.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface PromptOptions<Self extends Prompt> {
1414
render(this: Omit<Self, 'prompt'>): string | undefined;
1515
placeholder?: string;
1616
initialValue?: any;
17-
validate?: ((value: any) => string | undefined) | undefined;
17+
validate?: ((value: any) => string | Error | undefined) | undefined;
1818
input?: Readable;
1919
output?: Writable;
2020
debug?: boolean;
@@ -207,7 +207,7 @@ export default class Prompt {
207207
if (this.opts.validate) {
208208
const problem = this.opts.validate(this.value);
209209
if (problem) {
210-
this.error = problem;
210+
this.error = problem instanceof Error ? problem.message : problem;
211211
this.state = 'error';
212212
this.rl?.write(this.value);
213213
}

‎packages/prompts/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export interface TextOptions {
102102
placeholder?: string;
103103
defaultValue?: string;
104104
initialValue?: string;
105-
validate?: (value: string) => string | undefined;
105+
validate?: (value: string) => string | Error | undefined;
106106
}
107107
export const text = (opts: TextOptions) => {
108108
return new TextPrompt({
@@ -138,7 +138,7 @@ export const text = (opts: TextOptions) => {
138138
export interface PasswordOptions {
139139
message: string;
140140
mask?: string;
141-
validate?: (value: string) => string | undefined;
141+
validate?: (value: string) => string | Error | undefined;
142142
}
143143
export const password = (opts: PasswordOptions) => {
144144
return new PasswordPrompt({

0 commit comments

Comments
 (0)
Please sign in to comment.