Skip to content

Commit a6f704e

Browse files
feat: standard schema for validation (#42)
* feat: standard schema for validation * Apply suggestions from code review Signed-off-by: Florian Lefebvre <contact@florian-lefebvre.dev> --------- Signed-off-by: Florian Lefebvre <contact@florian-lefebvre.dev> Co-authored-by: paul valladares <85648028+dreyfus92@users.noreply.github.com>
1 parent 5c95099 commit a6f704e

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/content/docs/clack/guides/best-practices.mdx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ const age = await text({
5353
});
5454
```
5555

56+
A [Standard Schema](https://github.com/standard-schema/standard-schema) can be used, for example using [Arktype](https://arktype.io/):
57+
58+
```ts twoslash
59+
import { text } from '@clack/prompts';
60+
import { type } from 'arktype';
61+
62+
const name = await text({
63+
message: 'Enter your name (letters only)',
64+
validate: type('string.alpha').describe('Name can only contain letters'),
65+
});
66+
```
67+
5668
### 3. Consistent Messaging
5769

5870
Use clear and consistent messaging across your prompts:

src/content/docs/clack/packages/prompts.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ Options:
130130
- `placeholder`: A visual hint shown when the field has no content.
131131
- `defaultValue`: A fallback value returned when the user provides nothing (empty input).
132132
- `initialValue`: The starting value shown when the prompt first renders. Users can edit this value before submitting.
133-
- `validate`: A function that validates user input. Return a `string` or `Error` to show as a validation error, or `undefined` to accept the result.
133+
- `validate`: A function or a [Standard Schema](https://github.com/standard-schema/standard-schema) that validates user input. If a custom function is given, you should return a `string` or `Error` to show as a validation error, or `undefined` to accept the result..
134134
- All [Common Options](#common-options)
135135

136136
### Password Input
@@ -163,7 +163,7 @@ Options:
163163

164164
- `message`: The prompt message or question shown to the user above the input.
165165
- `mask`: Character to use for masking input. Default: `'▪/•'`.
166-
- `validate`: A function that validates user input. Return a `string` or `Error` to show as a validation error, or `undefined` to accept the result.
166+
- `validate`: A function or a [Standard Schema](https://github.com/standard-schema/standard-schema) that validates user input. If a custom function is given, you should return a `string` or `Error` to show as a validation error, or `undefined` to accept the result.
167167
- `clearOnError`: When enabled it causes the input to be cleared if/when validation fails. Default: `false`.
168168
- All [Common Options](#common-options)
169169

0 commit comments

Comments
 (0)