|
12 | 12 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
13 | 13 | /* eslint-disable @typescript-eslint/no-namespace */ |
14 | 14 |
|
15 | | -import type {StandardJSONSchemaV1, StandardSchemaV1} from "@standard-schema/spec"; |
| 15 | +import type { |
| 16 | + StandardJSONSchemaV1, |
| 17 | + StandardSchemaV1, |
| 18 | +} from "@standard-schema/spec"; |
16 | 19 |
|
17 | 20 | export interface Serde<T> { |
18 | 21 | contentType?: string; |
@@ -52,20 +55,25 @@ class VoidSerde implements Serde<void> { |
52 | 55 |
|
53 | 56 | class StandardSchemaSerde< |
54 | 57 | T extends { "~standard": StandardSchemaV1.Props }, |
55 | | -> implements Serde<StandardSchemaV1.InferOutput<T>> |
56 | | -{ |
| 58 | +> implements Serde<StandardSchemaV1.InferOutput<T>> { |
57 | 59 | contentType? = "application/json"; |
58 | 60 | jsonSchema?: object | undefined; |
59 | 61 |
|
60 | | - constructor(private readonly schema: T, private readonly validateOptions?: Record<string, unknown>, jsonSchemaOptions?: Record<string, unknown>) { |
| 62 | + constructor( |
| 63 | + private readonly schema: T, |
| 64 | + private readonly validateOptions?: Record<string, unknown>, |
| 65 | + jsonSchemaOptions?: Record<string, unknown> |
| 66 | + ) { |
61 | 67 | // Extract JSON schema if available |
62 | 68 | const standard = schema["~standard"]; |
63 | 69 | if (isStandardJSONSchemaV1(standard)) { |
64 | 70 | try { |
65 | | - this.jsonSchema = (standard as unknown as StandardJSONSchemaV1.Props).jsonSchema.output({ |
| 71 | + this.jsonSchema = ( |
| 72 | + standard as unknown as StandardJSONSchemaV1.Props |
| 73 | + ).jsonSchema.output({ |
66 | 74 | target: "draft-2020-12", |
67 | | - libraryOptions: jsonSchemaOptions |
68 | | - }) |
| 75 | + libraryOptions: jsonSchemaOptions, |
| 76 | + }); |
69 | 77 | } catch { |
70 | 78 | // If JSON schema generation fails, leave it undefined |
71 | 79 | this.jsonSchema = undefined; |
@@ -112,23 +120,23 @@ class StandardSchemaSerde< |
112 | 120 | const errorMessages = result.issues |
113 | 121 | .map((issue: StandardSchemaV1.Issue) => issue.message) |
114 | 122 | .join(", "); |
115 | | - throw new TypeError(`Standard schema validation failed: [${errorMessages}]`); |
| 123 | + throw new TypeError( |
| 124 | + `Standard schema validation failed: [${errorMessages}]` |
| 125 | + ); |
116 | 126 | } |
117 | 127 |
|
118 | 128 | return result.value as StandardSchemaV1.InferOutput<T>; |
119 | 129 | } |
120 | 130 | } |
121 | 131 |
|
122 | | -function isStandardJSONSchemaV1( |
123 | | - standard: StandardSchemaV1.Props |
124 | | -): boolean { |
| 132 | +function isStandardJSONSchemaV1(standard: StandardSchemaV1.Props): boolean { |
125 | 133 | return ( |
126 | | - standard != undefined && |
127 | | - "jsonSchema" in standard && |
128 | | - typeof standard.jsonSchema === "object" && |
129 | | - standard.jsonSchema !== null && |
130 | | - "output" in standard.jsonSchema && |
131 | | - typeof standard.jsonSchema.output === "function" |
| 134 | + standard != undefined && |
| 135 | + "jsonSchema" in standard && |
| 136 | + typeof standard.jsonSchema === "object" && |
| 137 | + standard.jsonSchema !== null && |
| 138 | + "output" in standard.jsonSchema && |
| 139 | + typeof standard.jsonSchema.output === "function" |
132 | 140 | ); |
133 | 141 | } |
134 | 142 |
|
@@ -169,12 +177,10 @@ export namespace serde { |
169 | 177 | * @param jsonSchemaOptions options passed to `StandardJsonSchemaV1.Options.libraryOptions` for code generation |
170 | 178 | * @returns a serde that will validate the data with the standard schema |
171 | 179 | */ |
172 | | - export const schema = < |
173 | | - T extends { "~standard": StandardSchemaV1.Props }, |
174 | | - >( |
175 | | - schema: T, |
176 | | - validateOptions?: Record<string, unknown>, |
177 | | - jsonSchemaOptions?: Record<string, unknown> |
| 180 | + export const schema = <T extends { "~standard": StandardSchemaV1.Props }>( |
| 181 | + schema: T, |
| 182 | + validateOptions?: Record<string, unknown>, |
| 183 | + jsonSchemaOptions?: Record<string, unknown> |
178 | 184 | ): Serde<StandardSchemaV1.InferOutput<T>> => { |
179 | 185 | return new StandardSchemaSerde(schema, jsonSchemaOptions); |
180 | 186 | }; |
|
0 commit comments