| 1 | <h1 align="center">
|
|---|
| 2 | <img alt="Standard Schema fire logo" loading="lazy" width="50" height="50" decoding="async" data-nimg="1" style="color:transparent" src="https://standardschema.dev/favicon.svg">
|
|---|
| 3 | </br>
|
|---|
| 4 | Standard Schema</h1>
|
|---|
| 5 | <p align="center">
|
|---|
| 6 | A family of specs for interoperable TypeScript
|
|---|
| 7 | <br/>
|
|---|
| 8 | <a href="https://standardschema.dev">standardschema.dev</a>
|
|---|
| 9 | </p>
|
|---|
| 10 | <br/>
|
|---|
| 11 |
|
|---|
| 12 | <!-- start -->
|
|---|
| 13 |
|
|---|
| 14 | The Standard Schema project is a set of interfaces that standardize the provision and consumption of shared functionality in the TypeScript ecosystem.
|
|---|
| 15 |
|
|---|
| 16 | Its goal is to allow tools to accept a single input that includes all the types and capabilities they need— no library-specific adapters, no extra dependencies. The result is an ecosystem that's fair for implementers, friendly for consumers, and open for end users.
|
|---|
| 17 |
|
|---|
| 18 | ## The specifications
|
|---|
| 19 |
|
|---|
| 20 | The specifications can be found below in their entirety. Libraries wishing to implement a spec can copy/paste the code block below into their codebase. They're also available at `@standard-schema/spec` on [npm](https://www.npmjs.com/package/@standard-schema/spec) and [JSR](https://jsr.io/@standard-schema/spec).
|
|---|
| 21 |
|
|---|
| 22 | ```ts
|
|---|
| 23 | // #########################
|
|---|
| 24 | // ### Standard Typed ###
|
|---|
| 25 | // #########################
|
|---|
| 26 |
|
|---|
| 27 | /** The Standard Typed interface. This is a base type extended by other specs. */
|
|---|
| 28 | export interface StandardTypedV1<Input = unknown, Output = Input> {
|
|---|
| 29 | /** The Standard properties. */
|
|---|
| 30 | readonly "~standard": StandardTypedV1.Props<Input, Output>;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | export declare namespace StandardTypedV1 {
|
|---|
| 34 | /** The Standard Typed properties interface. */
|
|---|
| 35 | export interface Props<Input = unknown, Output = Input> {
|
|---|
| 36 | /** The version number of the standard. */
|
|---|
| 37 | readonly version: 1;
|
|---|
| 38 | /** The vendor name of the schema library. */
|
|---|
| 39 | readonly vendor: string;
|
|---|
| 40 | /** Inferred types associated with the schema. */
|
|---|
| 41 | readonly types?: Types<Input, Output> | undefined;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | /** The Standard Typed types interface. */
|
|---|
| 45 | export interface Types<Input = unknown, Output = Input> {
|
|---|
| 46 | /** The input type of the schema. */
|
|---|
| 47 | readonly input: Input;
|
|---|
| 48 | /** The output type of the schema. */
|
|---|
| 49 | readonly output: Output;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /** Infers the input type of a Standard Typed. */
|
|---|
| 53 | export type InferInput<Schema extends StandardTypedV1> = NonNullable<
|
|---|
| 54 | Schema["~standard"]["types"]
|
|---|
| 55 | >["input"];
|
|---|
| 56 |
|
|---|
| 57 | /** Infers the output type of a Standard Typed. */
|
|---|
| 58 | export type InferOutput<Schema extends StandardTypedV1> = NonNullable<
|
|---|
| 59 | Schema["~standard"]["types"]
|
|---|
| 60 | >["output"];
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | // ##########################
|
|---|
| 64 | // ### Standard Schema ###
|
|---|
| 65 | // ##########################
|
|---|
| 66 |
|
|---|
| 67 | /** The Standard Schema interface. */
|
|---|
| 68 | export interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|---|
| 69 | /** The Standard Schema properties. */
|
|---|
| 70 | readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | export declare namespace StandardSchemaV1 {
|
|---|
| 74 | /** The Standard Schema properties interface. */
|
|---|
| 75 | export interface Props<Input = unknown, Output = Input>
|
|---|
| 76 | extends StandardTypedV1.Props<Input, Output> {
|
|---|
| 77 | /** Validates unknown input values. */
|
|---|
| 78 | readonly validate: (
|
|---|
| 79 | value: unknown,
|
|---|
| 80 | options?: StandardSchemaV1.Options | undefined
|
|---|
| 81 | ) => Result<Output> | Promise<Result<Output>>;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /** The result interface of the validate function. */
|
|---|
| 85 | export type Result<Output> = SuccessResult<Output> | FailureResult;
|
|---|
| 86 |
|
|---|
| 87 | /** The result interface if validation succeeds. */
|
|---|
| 88 | export interface SuccessResult<Output> {
|
|---|
| 89 | /** The typed output value. */
|
|---|
| 90 | readonly value: Output;
|
|---|
| 91 | /** A falsy value for `issues` indicates success. */
|
|---|
| 92 | readonly issues?: undefined;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | export interface Options {
|
|---|
| 96 | /** Explicit support for additional vendor-specific parameters, if needed. */
|
|---|
| 97 | readonly libraryOptions?: Record<string, unknown> | undefined;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** The result interface if validation fails. */
|
|---|
| 101 | export interface FailureResult {
|
|---|
| 102 | /** The issues of failed validation. */
|
|---|
| 103 | readonly issues: ReadonlyArray<Issue>;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** The issue interface of the failure output. */
|
|---|
| 107 | export interface Issue {
|
|---|
| 108 | /** The error message of the issue. */
|
|---|
| 109 | readonly message: string;
|
|---|
| 110 | /** The path of the issue, if any. */
|
|---|
| 111 | readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /** The path segment interface of the issue. */
|
|---|
| 115 | export interface PathSegment {
|
|---|
| 116 | /** The key representing a path segment. */
|
|---|
| 117 | readonly key: PropertyKey;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /** The Standard types interface. */
|
|---|
| 121 | export interface Types<Input = unknown, Output = Input>
|
|---|
| 122 | extends StandardTypedV1.Types<Input, Output> {}
|
|---|
| 123 |
|
|---|
| 124 | /** Infers the input type of a Standard. */
|
|---|
| 125 | export type InferInput<Schema extends StandardTypedV1> =
|
|---|
| 126 | StandardTypedV1.InferInput<Schema>;
|
|---|
| 127 |
|
|---|
| 128 | /** Infers the output type of a Standard. */
|
|---|
| 129 | export type InferOutput<Schema extends StandardTypedV1> =
|
|---|
| 130 | StandardTypedV1.InferOutput<Schema>;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // ###############################
|
|---|
| 134 | // ### Standard JSON Schema ###
|
|---|
| 135 | // ###############################
|
|---|
| 136 |
|
|---|
| 137 | /** The Standard JSON Schema interface. */
|
|---|
| 138 | export interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
|
|---|
| 139 | /** The Standard JSON Schema properties. */
|
|---|
| 140 | readonly "~standard": StandardJSONSchemaV1.Props<Input, Output>;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | export declare namespace StandardJSONSchemaV1 {
|
|---|
| 144 | /** The Standard JSON Schema properties interface. */
|
|---|
| 145 | export interface Props<Input = unknown, Output = Input>
|
|---|
| 146 | extends StandardTypedV1.Props<Input, Output> {
|
|---|
| 147 | /** Methods for generating the input/output JSON Schema. */
|
|---|
| 148 | readonly jsonSchema: StandardJSONSchemaV1.Converter;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /** The Standard JSON Schema converter interface. */
|
|---|
| 152 | export interface Converter {
|
|---|
| 153 | /** Converts the input type to JSON Schema. May throw if conversion is not supported. */
|
|---|
| 154 | readonly input: (
|
|---|
| 155 | options: StandardJSONSchemaV1.Options
|
|---|
| 156 | ) => Record<string, unknown>;
|
|---|
| 157 | /** Converts the output type to JSON Schema. May throw if conversion is not supported. */
|
|---|
| 158 | readonly output: (
|
|---|
| 159 | options: StandardJSONSchemaV1.Options
|
|---|
| 160 | ) => Record<string, unknown>;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * The target version of the generated JSON Schema.
|
|---|
| 165 | *
|
|---|
| 166 | * It is *strongly recommended* that implementers support `"draft-2020-12"` and `"draft-07"`, as they are both in wide use. All other targets can be implemented on a best-effort basis. Libraries should throw if they don't support a specified target.
|
|---|
| 167 | *
|
|---|
| 168 | * The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
|
|---|
| 169 | */
|
|---|
| 170 | export type Target =
|
|---|
| 171 | | "draft-2020-12"
|
|---|
| 172 | | "draft-07"
|
|---|
| 173 | | "openapi-3.0"
|
|---|
| 174 | // Accepts any string for future targets while preserving autocomplete
|
|---|
| 175 | | ({} & string);
|
|---|
| 176 |
|
|---|
| 177 | /** The options for the input/output methods. */
|
|---|
| 178 | export interface Options {
|
|---|
| 179 | /** Specifies the target version of the generated JSON Schema. Support for all versions is on a best-effort basis. If a given version is not supported, the library should throw. */
|
|---|
| 180 | readonly target: Target;
|
|---|
| 181 |
|
|---|
| 182 | /** Explicit support for additional vendor-specific parameters, if needed. */
|
|---|
| 183 | readonly libraryOptions?: Record<string, unknown> | undefined;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | /** The Standard types interface. */
|
|---|
| 187 | export interface Types<Input = unknown, Output = Input>
|
|---|
| 188 | extends StandardTypedV1.Types<Input, Output> {}
|
|---|
| 189 |
|
|---|
| 190 | /** Infers the input type of a Standard. */
|
|---|
| 191 | export type InferInput<Schema extends StandardTypedV1> =
|
|---|
| 192 | StandardTypedV1.InferInput<Schema>;
|
|---|
| 193 |
|
|---|
| 194 | /** Infers the output type of a Standard. */
|
|---|
| 195 | export type InferOutput<Schema extends StandardTypedV1> =
|
|---|
| 196 | StandardTypedV1.InferOutput<Schema>;
|
|---|
| 197 | }
|
|---|
| 198 | ```
|
|---|