Skip to content

fix: validate initial values #316

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

Merged
merged 2 commits into from
May 13, 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
5 changes: 5 additions & 0 deletions .changeset/mean-mice-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clack/core": patch
---

Validates initial values immediately when using text prompts with initialValue and validate props.
41 changes: 41 additions & 0 deletions examples/basic/text-validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { text, note, isCancel } from '@clack/prompts';
import { setTimeout } from 'node:timers/promises';

async function main() {
console.clear();

// Example demonstrating the issue with initial value validation
const name = await text({
message: 'Enter your name (letters and spaces only)',
initialValue: 'John123', // Invalid initial value with numbers
validate: (value) => {
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
return undefined;
},
});

if (!isCancel(name)) {
note(`Valid name: ${name}`, 'Success');
}

await setTimeout(1000);

// Example with a valid initial value for comparison
const validName = await text({
message: 'Enter another name (letters and spaces only)',
initialValue: 'John Doe', // Valid initial value
validate: (value) => {
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
return undefined;
},
});

if (!isCancel(validName)) {
note(`Valid name: ${validName}`, 'Success');
}

await setTimeout(1000);

}

main().catch(console.error);
9 changes: 9 additions & 0 deletions packages/core/src/prompts/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ export default class Prompt {
this.rl.write(this.opts.initialValue);
}
this._setValue(this.opts.initialValue);

// Validate initial value if validator exists
if (this.opts.validate) {
const problem = this.opts.validate(this.opts.initialValue);
if (problem) {
this.error = problem instanceof Error ? problem.message : problem;
this.state = 'error';
}
}
}

this.input.on('keypress', this.onKeypress);
Expand Down
70 changes: 70 additions & 0 deletions packages/core/test/prompts/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,74 @@ describe('Prompt', () => {

expect(instance.state).to.equal('cancel');
});

test('validates initial value on prompt start', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'invalid',
validate: (value) => value === 'valid' ? undefined : 'must be valid',
});
instance.prompt();

expect(instance.state).to.equal('error');
expect(instance.error).to.equal('must be valid');
});

test('accepts valid initial value', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'valid',
validate: (value) => value === 'valid' ? undefined : 'must be valid',
});
instance.prompt();

expect(instance.state).to.equal('active');
expect(instance.error).to.equal('');
});

test('validates initial value with Error object', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'invalid',
validate: (value) => value === 'valid' ? undefined : new Error('must be valid'),
});
instance.prompt();

expect(instance.state).to.equal('error');
expect(instance.error).to.equal('must be valid');
});

test('validates initial value with regex validation', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'Invalid Value $$$',
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
});
instance.prompt();

expect(instance.state).to.equal('error');
expect(instance.error).to.equal('Invalid value');
});

test('accepts valid initial value with regex validation', () => {
const instance = new Prompt({
input,
output,
render: () => 'foo',
initialValue: 'VALID',
validate: (value) => /^[A-Z]+$/.test(value) ? undefined : 'Invalid value',
});
instance.prompt();

expect(instance.state).to.equal('active');
expect(instance.error).to.equal('');
});
});