Skip to content

Commit 46163fa

Browse files
committed
async validate
1 parent f44c104 commit 46163fa

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export { default as GroupMultiSelectPrompt } from './prompts/group-multiselect';
33
export { default as MultiSelectPrompt } from './prompts/multi-select';
44
export { default as PasswordPrompt } from './prompts/password';
55
export { default as Prompt, isCancel } from './prompts/prompt';
6-
export type { State } from './prompts/prompt';
6+
export type { State, Validator } from './prompts/prompt';
77
export { default as SelectPrompt } from './prompts/select';
88
export { default as SelectKeyPrompt } from './prompts/select-key';
99
export { default as TextPrompt } from './prompts/text';

packages/core/src/prompts/prompt.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ const aliases = new Map([
3838
]);
3939
const keys = new Set(['up', 'down', 'left', 'right', 'space', 'enter']);
4040

41+
export interface Validator<Value = any> {
42+
(value: Value): string | void | Promise<string | void>;
43+
}
44+
4145
export interface PromptOptions<Self extends Prompt> {
4246
render(this: Omit<Self, 'prompt'>): string | void;
4347
placeholder?: string;
4448
initialValue?: any;
45-
validate?: ((value: any) => string | void) | undefined;
49+
validate?: Validator;
4650
input?: Readable;
4751
output?: Writable;
4852
debug?: boolean;
@@ -153,7 +157,7 @@ export default class Prompt {
153157
this.subscribers.clear();
154158
}
155159

156-
private onKeypress(char: string, key?: Key) {
160+
private async onKeypress(char: string, key?: Key) {
157161
if (this.state === 'error') {
158162
this.state = 'active';
159163
}
@@ -172,7 +176,7 @@ export default class Prompt {
172176

173177
if (key?.name === 'return') {
174178
if (this.opts.validate) {
175-
const problem = this.opts.validate(this.value);
179+
const problem = await this.opts.validate(this.value);
176180
if (problem) {
177181
this.error = problem;
178182
this.state = 'error';

packages/prompts/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
SelectPrompt,
1010
State,
1111
TextPrompt,
12+
Validator,
1213
} from '@clack/core';
1314
import isUnicodeSupported from 'is-unicode-supported';
1415
import color from 'picocolors';
@@ -63,7 +64,7 @@ export interface TextOptions {
6364
placeholder?: string;
6465
defaultValue?: string;
6566
initialValue?: string;
66-
validate?: (value: string) => string | void;
67+
validate?: Validator<string>;
6768
}
6869
export const text = (opts: TextOptions) => {
6970
return new TextPrompt({
@@ -99,7 +100,7 @@ export const text = (opts: TextOptions) => {
99100
export interface PasswordOptions {
100101
message: string;
101102
mask?: string;
102-
validate?: (value: string) => string | void;
103+
validate?: Validator<string>;
103104
}
104105
export const password = (opts: PasswordOptions) => {
105106
return new PasswordPrompt({

0 commit comments

Comments
 (0)