-
-
Notifications
You must be signed in to change notification settings - Fork 203
feat: standard schema for validation #543
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
dreyfus92
merged 14 commits into
bombshell-dev:main
from
florian-lefebvre:feat/standard-schema
May 28, 2026
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a4a4f60
feat: standard schema for validation
florian-lefebvre d071d6d
fix: types
florian-lefebvre b374153
feedback
florian-lefebvre 491371d
feedback
florian-lefebvre 5b6579d
fix: test
florian-lefebvre fd18aae
update todo
florian-lefebvre f318c5b
Update packages/core/src/utils/validation.ts
florian-lefebvre df2f01a
Update packages/core/src/utils/validation.ts
florian-lefebvre 3f77e8a
inline standard schema
florian-lefebvre 56f0713
feedback
florian-lefebvre 0f1fceb
Update packages/core/src/prompts/prompt.ts
florian-lefebvre 1a23de5
Update packages/core/src/utils/validation.ts
florian-lefebvre 2ca6fa9
jsdocs
florian-lefebvre f1419b5
Update packages/core/src/utils/validation.ts
florian-lefebvre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| "@clack/prompts": minor | ||
| "@clack/core": minor | ||
| --- | ||
|
|
||
| Adds support for Standard Schema validation | ||
|
|
||
| Prompts accept an optional `validate()` function to validate user input. While a function provides more flexibility and customization over your validation, it can be a bit verbose. To help solve this, there are libraries that provide schema-based validation to make shorthand and type-strict validation substantially easier. | ||
|
|
||
| Libraries following the [Standard Schema specification](https://github.com/standard-schema/standard-schema) are now natively supported. For example, using [Arktype](https://arktype.io/): | ||
|
|
||
| ```diff | ||
| import { text } from '@clack/prompts'; | ||
| import { type } from 'arktype'; | ||
|
|
||
| const name = await text({ | ||
| message: 'Enter your name (letters only)', | ||
| initialValue: 'John123', // Invalid initial value with numbers | ||
| + validate: type('string.alpha').describe('Name can only contain letters'), | ||
| }); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { setTimeout } from 'node:timers/promises'; | ||
| import { isCancel, note, text } from '@clack/prompts'; | ||
| import { type } from 'arktype'; | ||
|
|
||
| async function main() { | ||
| console.clear(); | ||
|
|
||
| // Example demonstrating the issue with initial value validation | ||
| const name = await text({ | ||
| message: 'Enter your name (letters only)', | ||
| initialValue: 'John123', // Invalid initial value with numbers | ||
| validate: type('string.alpha').describe('Name can only contain letters'), | ||
|
florian-lefebvre marked this conversation as resolved.
Outdated
|
||
| }); | ||
|
|
||
| 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 only)', | ||
| initialValue: 'JohnDoe', // Valid initial value | ||
| validate: type('string.alpha').describe('Name can only contain letters'), | ||
| }); | ||
|
|
||
| if (!isCancel(validName)) { | ||
| note(`Valid name: ${validName}`, 'Success'); | ||
| } | ||
|
|
||
| await setTimeout(1000); | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
|
florian-lefebvre marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import type { StandardSchemaV1 } from '@standard-schema/spec'; | ||
|
|
||
| export type Validate<TValue> = | ||
|
florian-lefebvre marked this conversation as resolved.
|
||
| | ((value: TValue | undefined) => string | Error | undefined) | ||
| | StandardSchemaV1<TValue | undefined, any>; | ||
|
florian-lefebvre marked this conversation as resolved.
Outdated
|
||
|
|
||
| export function runValidation<TValue>( | ||
| validate: Validate<TValue>, | ||
| value: TValue | undefined | ||
| ): string | Error | undefined { | ||
| if ('~standard' in validate) { | ||
| const result = validate['~standard'].validate(value); | ||
| // https://standardschema.dev/schema#how-to-only-allow-synchronous-validation | ||
| if (result instanceof Promise) { | ||
| throw new TypeError( | ||
| 'Schema validation must be synchronous. Update `validate()` and get rid of any asynchronous logic.' | ||
|
florian-lefebvre marked this conversation as resolved.
Outdated
florian-lefebvre marked this conversation as resolved.
Outdated
|
||
| ); | ||
| } | ||
| return result.issues?.at(0)?.message; | ||
| } | ||
| return validate(value); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.