-
-
Notifications
You must be signed in to change notification settings - Fork 196
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
Open
florian-lefebvre
wants to merge
9
commits into
bombshell-dev:main
Choose a base branch
from
florian-lefebvre:feat/standard-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 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 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
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,20 @@ | ||
| --- | ||
| "@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 email', | ||
| + validate: type('string.email').describe('Invalid email'), | ||
| }); | ||
| ``` |
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 email', | ||
| initialValue: 'aaa', // Invalid initial value without @ | ||
| validate: type('string.email').describe('Invalid email'), | ||
| }); | ||
|
|
||
| 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 email', | ||
| initialValue: 'john.doe@example.com', // Valid initial value | ||
| validate: type('string.email').describe('Invalid email'), | ||
| }); | ||
|
|
||
| if (!isCancel(validName)) { | ||
| note(`Valid name: ${validName}`, 'Success'); | ||
| } | ||
|
|
||
| await setTimeout(1000); | ||
| } | ||
|
|
||
| await main().catch(console.error); |
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 |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ | |
| "sisteransi": "^1.0.5" | ||
| }, | ||
| "devDependencies": { | ||
| "arktype": "^2.2.0", | ||
| "vitest": "^3.2.4" | ||
| } | ||
| } | ||
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,78 @@ | ||
| // https://standardschema.dev/schema | ||
|
|
||
| /** The Standard Schema interface. */ | ||
| export interface StandardSchemaV1<Input = unknown, Output = Input> { | ||
| /** The Standard Schema properties. */ | ||
| readonly "~standard": StandardSchemaV1.Props<Input, Output>; | ||
| } | ||
|
|
||
| export declare namespace StandardSchemaV1 { | ||
| /** The Standard Schema properties interface. */ | ||
| export interface Props<Input = unknown, Output = Input> { | ||
| /** The version number of the standard. */ | ||
| readonly version: 1; | ||
| /** The vendor name of the schema library. */ | ||
| readonly vendor: string; | ||
| /** Validates unknown input values. */ | ||
| readonly validate: ( | ||
| value: unknown, | ||
| options?: StandardSchemaV1.Options | undefined, | ||
| ) => Result<Output> | Promise<Result<Output>>; | ||
| /** Inferred types associated with the schema. */ | ||
| readonly types?: Types<Input, Output> | undefined; | ||
| } | ||
|
|
||
| /** The result interface of the validate function. */ | ||
| export type Result<Output> = SuccessResult<Output> | FailureResult; | ||
|
|
||
| /** The result interface if validation succeeds. */ | ||
| export interface SuccessResult<Output> { | ||
| /** The typed output value. */ | ||
| readonly value: Output; | ||
| /** A falsy value for `issues` indicates success. */ | ||
| readonly issues?: undefined; | ||
| } | ||
|
|
||
| export interface Options { | ||
| /** Explicit support for additional vendor-specific parameters, if needed. */ | ||
| readonly libraryOptions?: Record<string, unknown> | undefined; | ||
| } | ||
|
|
||
| /** The result interface if validation fails. */ | ||
| export interface FailureResult { | ||
| /** The issues of failed validation. */ | ||
| readonly issues: ReadonlyArray<Issue>; | ||
| } | ||
|
|
||
| /** The issue interface of the failure output. */ | ||
| export interface Issue { | ||
| /** The error message of the issue. */ | ||
| readonly message: string; | ||
| /** The path of the issue, if any. */ | ||
| readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; | ||
| } | ||
|
|
||
| /** The path segment interface of the issue. */ | ||
| export interface PathSegment { | ||
| /** The key representing a path segment. */ | ||
| readonly key: PropertyKey; | ||
| } | ||
|
|
||
| /** The Standard Schema types interface. */ | ||
| export interface Types<Input = unknown, Output = Input> { | ||
| /** The input type of the schema. */ | ||
| readonly input: Input; | ||
| /** The output type of the schema. */ | ||
| readonly output: Output; | ||
| } | ||
|
|
||
| /** Infers the input type of a Standard Schema. */ | ||
| export type InferInput<Schema extends StandardSchemaV1> = NonNullable< | ||
| Schema["~standard"]["types"] | ||
| >["input"]; | ||
|
|
||
| /** Infers the output type of a Standard Schema. */ | ||
| export type InferOutput<Schema extends StandardSchemaV1> = NonNullable< | ||
| Schema["~standard"]["types"] | ||
| >["output"]; | ||
| } |
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 type { StandardSchemaV1 } from './standard-schema.js'; | ||
|
|
||
| /** | ||
| * Represents the `validate()` option. A function or a | ||
| * [Standard Schema](https://github.com/standard-schema/standard-schema) | ||
| * that validates user input. Return a `string` or `Error` to show as a | ||
| * validation error, or `undefined` to accept the result. | ||
| */ | ||
| export type Validate<TValue> = | ||
| | ((value: TValue | undefined) => string | Error | undefined) | ||
| | StandardSchemaV1<TValue | undefined, unknown>; | ||
|
|
||
| /** | ||
| * Runs the `validate()` option and normalizes the result | ||
| * @param validate - The validate option | ||
| * @param value - The user input | ||
| * @returns string | Error | undefined | ||
| */ | ||
| 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 | ||
| // TODO: https://github.com/bombshell-dev/clack/issues/92 | ||
| if (result instanceof Promise) { | ||
| throw new TypeError( | ||
| 'Schema validation must be synchronous. Update `validate()` and remove any asynchronous logic.' | ||
| ); | ||
| } | ||
| return result.issues?.at(0)?.message; | ||
| } | ||
| return validate(value); | ||
| } | ||
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.