Skip to content

Commit 57d0958

Browse files
dreyfus92github-actions[bot]
authored andcommitted
[ci] format
1 parent 34f52fe commit 57d0958

File tree

3 files changed

+41
-42
lines changed

3 files changed

+41
-42
lines changed

examples/basic/text-validation.ts

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
1-
import { text, note, isCancel } from '@clack/prompts';
21
import { setTimeout } from 'node:timers/promises';
2+
import { isCancel, note, text } from '@clack/prompts';
33

44
async function main() {
5-
console.clear();
6-
7-
// Example demonstrating the issue with initial value validation
8-
const name = await text({
9-
message: 'Enter your name (letters and spaces only)',
10-
initialValue: 'John123', // Invalid initial value with numbers
11-
validate: (value) => {
12-
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
13-
return undefined;
14-
},
15-
});
16-
17-
if (!isCancel(name)) {
18-
note(`Valid name: ${name}`, 'Success');
19-
}
20-
21-
await setTimeout(1000);
22-
23-
// Example with a valid initial value for comparison
24-
const validName = await text({
25-
message: 'Enter another name (letters and spaces only)',
26-
initialValue: 'John Doe', // Valid initial value
27-
validate: (value) => {
28-
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
29-
return undefined;
30-
},
31-
});
32-
33-
if (!isCancel(validName)) {
34-
note(`Valid name: ${validName}`, 'Success');
35-
}
36-
37-
await setTimeout(1000);
38-
5+
console.clear();
6+
7+
// Example demonstrating the issue with initial value validation
8+
const name = await text({
9+
message: 'Enter your name (letters and spaces only)',
10+
initialValue: 'John123', // Invalid initial value with numbers
11+
validate: (value) => {
12+
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
13+
return undefined;
14+
},
15+
});
16+
17+
if (!isCancel(name)) {
18+
note(`Valid name: ${name}`, 'Success');
19+
}
20+
21+
await setTimeout(1000);
22+
23+
// Example with a valid initial value for comparison
24+
const validName = await text({
25+
message: 'Enter another name (letters and spaces only)',
26+
initialValue: 'John Doe', // Valid initial value
27+
validate: (value) => {
28+
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
29+
return undefined;
30+
},
31+
});
32+
33+
if (!isCancel(validName)) {
34+
note(`Valid name: ${validName}`, 'Success');
35+
}
36+
37+
await setTimeout(1000);
3938
}
4039

41-
main().catch(console.error);
40+
main().catch(console.error);

packages/core/src/prompts/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export default class Prompt {
147147
this.rl.write(this.opts.initialValue);
148148
}
149149
this._setValue(this.opts.initialValue);
150-
150+
151151
// Validate initial value if validator exists
152152
if (this.opts.validate) {
153153
const problem = this.opts.validate(this.opts.initialValue);

packages/core/test/prompts/prompt.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ describe('Prompt', () => {
270270
output,
271271
render: () => 'foo',
272272
initialValue: 'invalid',
273-
validate: (value) => value === 'valid' ? undefined : 'must be valid',
273+
validate: (value) => (value === 'valid' ? undefined : 'must be valid'),
274274
});
275275
instance.prompt();
276276

@@ -284,7 +284,7 @@ describe('Prompt', () => {
284284
output,
285285
render: () => 'foo',
286286
initialValue: 'valid',
287-
validate: (value) => value === 'valid' ? undefined : 'must be valid',
287+
validate: (value) => (value === 'valid' ? undefined : 'must be valid'),
288288
});
289289
instance.prompt();
290290

@@ -298,7 +298,7 @@ describe('Prompt', () => {
298298
output,
299299
render: () => 'foo',
300300
initialValue: 'invalid',
301-
validate: (value) => value === 'valid' ? undefined : new Error('must be valid'),
301+
validate: (value) => (value === 'valid' ? undefined : new Error('must be valid')),
302302
});
303303
instance.prompt();
304304

@@ -312,7 +312,7 @@ describe('Prompt', () => {
312312
output,
313313
render: () => 'foo',
314314
initialValue: 'Invalid Value $$$',
315-
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
315+
validate: (value) => (/^[A-Z]+$/.test(value) ? undefined : 'Invalid value'),
316316
});
317317
instance.prompt();
318318

@@ -326,7 +326,7 @@ describe('Prompt', () => {
326326
output,
327327
render: () => 'foo',
328328
initialValue: 'VALID',
329-
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
329+
validate: (value) => (/^[A-Z]+$/.test(value) ? undefined : 'Invalid value'),
330330
});
331331
instance.prompt();
332332

0 commit comments

Comments
 (0)