Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@clack/core,@clack/prompts): add Error support for prompt.validate #165

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -13,7 +13,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 @@ -183,7 +183,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 @@ -100,7 +100,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 @@ -136,7 +136,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
Loading