1/**
2 * Type definitions from the Standard Schema specification.
3 *
4 * The declarations below are vendored verbatim from `@standard-schema/spec` 1.1.0.
5 *
6 * @since 4.0.0
7 */
8
9/*
10MIT License
11
12Copyright (c) 2024 Colin McDonnell
13
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31*/
32
33// #########################
34// ### Standard Typed ###
35// #########################
36
37/** The Standard Typed interface. This is a base type extended by other specs. */
38export interface StandardTypedV1<Input = unknown, Output = Input> {
39 /** The Standard properties. */
40 readonly "~standard": StandardTypedV1.Props<Input, Output>;
41}
42
43export declare namespace StandardTypedV1 {
44 /** The Standard Typed properties interface. */
45 export interface Props<Input = unknown, Output = Input> {
46 /** The version number of the standard. */
47 readonly version: 1;
48 /** The vendor name of the schema library. */
49 readonly vendor: string;
50 /** Inferred types associated with the schema. */
51 readonly types?: Types<Input, Output> | undefined;
52 }
53
54 /** The Standard Typed types interface. */
55 export interface Types<Input = unknown, Output = Input> {
56 /** The input type of the schema. */
57 readonly input: Input;
58 /** The output type of the schema. */
59 readonly output: Output;
60 }
61
62 /** Infers the input type of a Standard Typed. */
63 export type InferInput<Schema extends StandardTypedV1> = NonNullable<
64 Schema["~standard"]["types"]
65 >["input"];
66
67 /** Infers the output type of a Standard Typed. */
68 export type InferOutput<Schema extends StandardTypedV1> = NonNullable<
69 Schema["~standard"]["types"]
70 >["output"];
71}
72
73// ##########################
74// ### Standard Schema ###
75// ##########################
76
77/** The Standard Schema interface. */
78export interface StandardSchemaV1<Input = unknown, Output = Input> {
79 /** The Standard Schema properties. */
80 readonly "~standard": StandardSchemaV1.Props<Input, Output>;
81}
82
83export declare namespace StandardSchemaV1 {
84 /** The Standard Schema properties interface. */
85 export interface Props<Input = unknown, Output = Input>
86 extends StandardTypedV1.Props<Input, Output> {
87 /** Validates unknown input values. */
88 readonly validate: (
89 value: unknown,
90 options?: StandardSchemaV1.Options | undefined
91 ) => Result<Output> | Promise<Result<Output>>;
92 }
93
94 /** The result interface of the validate function. */
95 export type Result<Output> = SuccessResult<Output> | FailureResult;
96
97 /** The result interface if validation succeeds. */
98 export interface SuccessResult<Output> {
99 /** The typed output value. */
100 readonly value: Output;
101 /** A falsy value for `issues` indicates success. */
102 readonly issues?: undefined;
103 }
104
105 export interface Options {
106 /** Explicit support for additional vendor-specific parameters, if needed. */
107 readonly libraryOptions?: Record<string, unknown> | undefined;
108 }
109
110 /** The result interface if validation fails. */
111 export interface FailureResult {
112 /** The issues of failed validation. */
113 readonly issues: ReadonlyArray<Issue>;
114 }
115
116 /** The issue interface of the failure output. */
117 export interface Issue {
118 /** The error message of the issue. */
119 readonly message: string;
120 /** The path of the issue, if any. */
121 readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
122 }
123
124 /** The path segment interface of the issue. */
125 export interface PathSegment {
126 /** The key representing a path segment. */
127 readonly key: PropertyKey;
128 }
129
130 /** The Standard types interface. */
131 export interface Types<Input = unknown, Output = Input>
132 extends StandardTypedV1.Types<Input, Output> {}
133
134 /** Infers the input type of a Standard. */
135 export type InferInput<Schema extends StandardTypedV1> =
136 StandardTypedV1.InferInput<Schema>;
137
138 /** Infers the output type of a Standard. */
139 export type InferOutput<Schema extends StandardTypedV1> =
140 StandardTypedV1.InferOutput<Schema>;
141}
142
143// ###############################
144// ### Standard JSON Schema ###
145// ###############################
146
147/** The Standard JSON Schema interface. */
148export interface StandardJSONSchemaV1<Input = unknown, Output = Input> {
149 /** The Standard JSON Schema properties. */
150 readonly "~standard": StandardJSONSchemaV1.Props<Input, Output>;
151}
152
153export declare namespace StandardJSONSchemaV1 {
154 /** The Standard JSON Schema properties interface. */
155 export interface Props<Input = unknown, Output = Input>
156 extends StandardTypedV1.Props<Input, Output> {
157 /** Methods for generating the input/output JSON Schema. */
158 readonly jsonSchema: StandardJSONSchemaV1.Converter;
159 }
160
161 /** The Standard JSON Schema converter interface. */
162 export interface Converter {
163 /** Converts the input type to JSON Schema. May throw if conversion is not supported. */
164 readonly input: (
165 options: StandardJSONSchemaV1.Options
166 ) => Record<string, unknown>;
167 /** Converts the output type to JSON Schema. May throw if conversion is not supported. */
168 readonly output: (
169 options: StandardJSONSchemaV1.Options
170 ) => Record<string, unknown>;
171 }
172
173 /**
174 * The target version of the generated JSON Schema.
175 *
176 * 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.
177 *
178 * The `"openapi-3.0"` target is intended as a standardized specifier for OpenAPI 3.0 which is a superset of JSON Schema `"draft-04"`.
179 */
180 export type Target =
181 | "draft-2020-12"
182 | "draft-07"
183 | "openapi-3.0"
184 // Accepts any string for future targets while preserving autocomplete
185 | ({} & string);
186
187 /** The options for the input/output methods. */
188 export interface Options {
189 /** 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. */
190 readonly target: Target;
191
192 /** Explicit support for additional vendor-specific parameters, if needed. */
193 readonly libraryOptions?: Record<string, unknown> | undefined;
194 }
195
196 /** The Standard types interface. */
197 export interface Types<Input = unknown, Output = Input>
198 extends StandardTypedV1.Types<Input, Output> {}
199
200 /** Infers the input type of a Standard. */
201 export type InferInput<Schema extends StandardTypedV1> =
202 StandardTypedV1.InferInput<Schema>;
203
204 /** Infers the output type of a Standard. */
205 export type InferOutput<Schema extends StandardTypedV1> =
206 StandardTypedV1.InferOutput<Schema>;
207}