|
| 1 | +import { SchemaError } from '@standard-schema/utils' |
| 2 | + |
| 3 | +/** The Standard Schema interface. */ |
| 4 | +export interface StandardSchemaV1<Input = unknown, Output = Input> { |
| 5 | + /** The Standard Schema properties. */ |
| 6 | + readonly '~standard': StandardSchemaV1.Props<Input, Output> |
| 7 | +} |
| 8 | + |
| 9 | +export declare namespace StandardSchemaV1 { |
| 10 | + /** The Standard Schema properties interface. */ |
| 11 | + export interface Props<Input = unknown, Output = Input> { |
| 12 | + /** The version number of the standard. */ |
| 13 | + readonly version: 1 |
| 14 | + /** The vendor name of the schema library. */ |
| 15 | + readonly vendor: string |
| 16 | + /** Validates unknown input values. */ |
| 17 | + readonly validate: ( |
| 18 | + value: unknown, |
| 19 | + ) => Result<Output> | Promise<Result<Output>> |
| 20 | + /** Inferred types associated with the schema. */ |
| 21 | + readonly types?: Types<Input, Output> | undefined |
| 22 | + } |
| 23 | + |
| 24 | + /** The result interface of the validate function. */ |
| 25 | + export type Result<Output> = SuccessResult<Output> | FailureResult |
| 26 | + |
| 27 | + /** The result interface if validation succeeds. */ |
| 28 | + export interface SuccessResult<Output> { |
| 29 | + /** The typed output value. */ |
| 30 | + readonly value: Output |
| 31 | + /** The non-existent issues. */ |
| 32 | + readonly issues?: undefined |
| 33 | + } |
| 34 | + |
| 35 | + /** The result interface if validation fails. */ |
| 36 | + export interface FailureResult { |
| 37 | + /** The issues of failed validation. */ |
| 38 | + readonly issues: ReadonlyArray<Issue> |
| 39 | + } |
| 40 | + |
| 41 | + /** The issue interface of the failure output. */ |
| 42 | + export interface Issue { |
| 43 | + /** The error message of the issue. */ |
| 44 | + readonly message: string |
| 45 | + /** The path of the issue, if any. */ |
| 46 | + readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined |
| 47 | + } |
| 48 | + |
| 49 | + /** The path segment interface of the issue. */ |
| 50 | + export interface PathSegment { |
| 51 | + /** The key representing a path segment. */ |
| 52 | + readonly key: PropertyKey |
| 53 | + } |
| 54 | + |
| 55 | + /** The Standard Schema types interface. */ |
| 56 | + export interface Types<Input = unknown, Output = Input> { |
| 57 | + /** The input type of the schema. */ |
| 58 | + readonly input: Input |
| 59 | + /** The output type of the schema. */ |
| 60 | + readonly output: Output |
| 61 | + } |
| 62 | + |
| 63 | + /** Infers the input type of a Standard Schema. */ |
| 64 | + export type InferInput<Schema extends StandardSchemaV1> = NonNullable< |
| 65 | + Schema['~standard']['types'] |
| 66 | + >['input'] |
| 67 | + |
| 68 | + /** Infers the output type of a Standard Schema. */ |
| 69 | + export type InferOutput<Schema extends StandardSchemaV1> = NonNullable< |
| 70 | + Schema['~standard']['types'] |
| 71 | + >['output'] |
| 72 | +} |
| 73 | + |
| 74 | +export async function parseWithSchema<Schema extends StandardSchemaV1>( |
| 75 | + schema: Schema, |
| 76 | + data: unknown, |
| 77 | +): Promise<StandardSchemaV1.InferOutput<Schema>> { |
| 78 | + let result = schema['~standard'].validate(data) |
| 79 | + if (result instanceof Promise) result = await result |
| 80 | + if (result.issues) throw new SchemaError(result.issues) |
| 81 | + return result.value |
| 82 | +} |
0 commit comments