11import type { StandardSchemaV1 } from "@standard-schema/spec" ;
22import type { HttpStatus } from "./status" ;
33
4+ export type ZetaSchema < Input = unknown , Output = Input > = StandardSchemaV1 <
5+ Input ,
6+ Output
7+ > & {
8+ "~zeta" : {
9+ type : string ;
10+ meta : Record < string , any > ;
11+ } ;
12+ meta ( meta ?: Record < string , any > ) : ZetaSchema < Input , Output > ;
13+ } ;
14+
15+ function createZetaSchema < Input = unknown , Output = Input > (
16+ name : string ,
17+ validate : ( value : unknown ) => StandardSchemaV1 . Result < Output > ,
18+ meta : Record < string , string > = { } ,
19+ ) : ZetaSchema < Input , Output > {
20+ const parentMeta = meta ;
21+ return {
22+ "~zeta" : {
23+ type : name ,
24+ meta,
25+ } ,
26+ "~standard" : {
27+ vendor : "@aklinker/zeta" ,
28+ version : 1 ,
29+ validate,
30+ } ,
31+ meta ( meta ) {
32+ return createZetaSchema ( name , validate , {
33+ ...parentMeta ,
34+ ...meta ,
35+ } ) ;
36+ } ,
37+ } ;
38+ }
39+
440/**
5- * A schema for a error response. Use when defining additional status codes that an operation might return with:
41+ * A schema for an error response. Use when defining additional status codes
42+ * that an operation might return with:
643 *
744 * ```ts
845 * import { ErrorResponse } from '@aklinker/zeta';
@@ -21,11 +58,10 @@ import type { HttpStatus } from "./status";
2158 * );
2259 * ```
2360 */
24- export const ErrorResponse : StandardSchemaV1 = {
25- "~standard" : {
26- vendor : "@aklinker/zeta" ,
27- version : 1 ,
28- validate : ( value : unknown ) : StandardSchemaV1 . Result < ErrorResponse > => {
61+ export const ErrorResponse : ZetaSchema < unknown , ErrorResponse > =
62+ createZetaSchema < unknown , ErrorResponse > (
63+ "ErrorResponse" ,
64+ ( value : unknown ) : StandardSchemaV1 . Result < ErrorResponse > => {
2965 if ( value == null )
3066 return {
3167 issues : [ { message : `Expected an object, received ${ value } ` } ] ,
@@ -59,13 +95,12 @@ export const ErrorResponse: StandardSchemaV1 = {
5995
6096 return { value : value as ErrorResponse } ;
6197 } ,
62- } ,
63- } ;
98+ ) ;
6499
65100/**
66101 * The actual type an error response conforms to.
67102 */
68- export type ErrorResponse = StandardSchemaV1 & {
103+ export type ErrorResponse = {
69104 [ additionalInfo : string ] : any ;
70105 name : string ;
71106 message : string ;
@@ -95,3 +130,37 @@ export const ErrorResponseJsonSchema = {
95130 } ,
96131 required : [ "status" , "name" , "message" ] ,
97132} ;
133+
134+ /**
135+ * A schema for when you want to not return a response. Use when defining
136+ * additional status codes that an operation might return with:
137+ *
138+ * ```ts
139+ * import { NoResponse } from '@aklinker/zeta';
140+ *
141+ * app.get(
142+ * "/api/item/:itemId",
143+ * {
144+ * responses: {
145+ * [HttpStatus.Accepted]: NoResponse,
146+ * }
147+ * },
148+ * () => {
149+ * // ...
150+ * }
151+ * );
152+ * ```
153+ */
154+ export const NoResponse : ZetaSchema < undefined | null | void , void > =
155+ createZetaSchema < undefined | null | void , void > (
156+ "NoResponse" ,
157+ ( value : unknown ) : StandardSchemaV1 . Result < void > => {
158+ return value != null
159+ ? {
160+ issues : [
161+ { message : `Expected undefined or null, got ${ typeof value } ` } ,
162+ ] ,
163+ }
164+ : { value : undefined } ;
165+ } ,
166+ ) ;
0 commit comments