@kaito-http/core 4.0.0-beta.2 → 4.0.0-beta.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-TL3E52YN.js +835 -0
- package/dist/cors/cors.cjs +85 -17
- package/dist/cors/cors.d.cts +122 -26
- package/dist/cors/cors.d.ts +122 -26
- package/dist/cors/cors.js +85 -17
- package/dist/index.cjs +955 -85
- package/dist/index.d.cts +97 -104
- package/dist/index.d.ts +97 -104
- package/dist/index.js +140 -84
- package/dist/schema/schema.cjs +875 -0
- package/dist/schema/schema.d.cts +333 -0
- package/dist/schema/schema.d.ts +333 -0
- package/dist/schema/schema.js +38 -0
- package/dist/stream/stream.cjs +6 -9
- package/dist/stream/stream.d.cts +11 -8
- package/dist/stream/stream.d.ts +11 -8
- package/dist/stream/stream.js +6 -9
- package/package.json +6 -9
|
@@ -0,0 +1,835 @@
|
|
|
1
|
+
// src/schema/schema.ts
|
|
2
|
+
function isPrimitiveJSONValue(value) {
|
|
3
|
+
return typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null;
|
|
4
|
+
}
|
|
5
|
+
var SchemaError = class extends Error {
|
|
6
|
+
constructor(issues) {
|
|
7
|
+
const first = issues.values().next().value;
|
|
8
|
+
if (first === void 0) {
|
|
9
|
+
throw new Error("SchemaError expects at least one issue to be provided");
|
|
10
|
+
}
|
|
11
|
+
super(first.message);
|
|
12
|
+
this.issues = issues;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var ParseContext = class _ParseContext {
|
|
16
|
+
static ISSUE = Symbol("ISSUE");
|
|
17
|
+
ISSUE = _ParseContext.ISSUE;
|
|
18
|
+
#issues = /* @__PURE__ */ new Set();
|
|
19
|
+
addIssue(message, path) {
|
|
20
|
+
this.#issues.add({ message, path });
|
|
21
|
+
return _ParseContext.ISSUE;
|
|
22
|
+
}
|
|
23
|
+
addIssues(issues, path) {
|
|
24
|
+
for (const issue of issues) {
|
|
25
|
+
this.#issues.add({ ...issue, path: [...path, ...issue.path] });
|
|
26
|
+
}
|
|
27
|
+
return _ParseContext.ISSUE;
|
|
28
|
+
}
|
|
29
|
+
get issues() {
|
|
30
|
+
return this.#issues;
|
|
31
|
+
}
|
|
32
|
+
static result(fn) {
|
|
33
|
+
const result = _ParseContext.with(fn);
|
|
34
|
+
if (result.type === "FATAL" || result.issues.size > 0) {
|
|
35
|
+
return {
|
|
36
|
+
success: false,
|
|
37
|
+
issues: result.issues
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
success: true,
|
|
42
|
+
result: result.data
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
static with(fn) {
|
|
46
|
+
const ctx = new _ParseContext();
|
|
47
|
+
const data = fn(ctx);
|
|
48
|
+
if (data === _ParseContext.ISSUE) {
|
|
49
|
+
return {
|
|
50
|
+
type: "FATAL",
|
|
51
|
+
issues: ctx.issues
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
type: "PARSED",
|
|
56
|
+
issues: ctx.issues,
|
|
57
|
+
data
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var BaseSchema = class {
|
|
62
|
+
/** @internal */
|
|
63
|
+
_input;
|
|
64
|
+
/** @internal */
|
|
65
|
+
_output;
|
|
66
|
+
def;
|
|
67
|
+
getSchemaObject() {
|
|
68
|
+
const schema = {};
|
|
69
|
+
if (this.def.description !== void 0) {
|
|
70
|
+
schema.description = this.def.description;
|
|
71
|
+
}
|
|
72
|
+
if (this.def.example !== void 0) {
|
|
73
|
+
schema.example = this.def.example;
|
|
74
|
+
}
|
|
75
|
+
return schema;
|
|
76
|
+
}
|
|
77
|
+
clone(def) {
|
|
78
|
+
return new this.constructor({
|
|
79
|
+
...this.def,
|
|
80
|
+
...def
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
constructor(def) {
|
|
84
|
+
this.def = def;
|
|
85
|
+
}
|
|
86
|
+
or(other) {
|
|
87
|
+
return k.union([this, other]);
|
|
88
|
+
}
|
|
89
|
+
example(example) {
|
|
90
|
+
if (example === void 0) {
|
|
91
|
+
return this.def.example;
|
|
92
|
+
}
|
|
93
|
+
return this.clone({ example });
|
|
94
|
+
}
|
|
95
|
+
description(description) {
|
|
96
|
+
if (description === void 0) {
|
|
97
|
+
return this.def.description;
|
|
98
|
+
}
|
|
99
|
+
return this.clone({ description });
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
var STRING_FORMAT_REGEXES = {
|
|
103
|
+
uuid: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i,
|
|
104
|
+
email: /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i,
|
|
105
|
+
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,
|
|
106
|
+
ipv6: /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/,
|
|
107
|
+
date: /^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/,
|
|
108
|
+
uri: /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\/.+|^[a-zA-Z][a-zA-Z0-9+.-]*:[^\/].+/,
|
|
109
|
+
hostname: /^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/
|
|
110
|
+
};
|
|
111
|
+
var KString = class _KString extends BaseSchema {
|
|
112
|
+
static create = () => new _KString({});
|
|
113
|
+
serialize(value) {
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
setCheck(check) {
|
|
117
|
+
return this.clone({ [check.type]: check });
|
|
118
|
+
}
|
|
119
|
+
toOpenAPI() {
|
|
120
|
+
const baseSchema = this.getSchemaObject();
|
|
121
|
+
const schema = {
|
|
122
|
+
...baseSchema,
|
|
123
|
+
type: "string"
|
|
124
|
+
};
|
|
125
|
+
if (this.def.regex) {
|
|
126
|
+
schema.pattern = this.def.regex.regex.source;
|
|
127
|
+
}
|
|
128
|
+
if (this.def.format) {
|
|
129
|
+
schema.format = this.def.format.format;
|
|
130
|
+
}
|
|
131
|
+
if (this.def.min !== void 0) {
|
|
132
|
+
schema.minLength = this.def.min.val;
|
|
133
|
+
}
|
|
134
|
+
if (this.def.max !== void 0) {
|
|
135
|
+
schema.maxLength = this.def.max.val;
|
|
136
|
+
}
|
|
137
|
+
return schema;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Sets the minimum length of the string
|
|
141
|
+
*
|
|
142
|
+
* @param min The minimum length of the string
|
|
143
|
+
* @returns A clone of the schema with the minimum length set
|
|
144
|
+
*/
|
|
145
|
+
min(min, message) {
|
|
146
|
+
return this.setCheck({ type: "min", val: min, message });
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Sets the maximum length of the string
|
|
150
|
+
*
|
|
151
|
+
* @param max The maximum length of the string
|
|
152
|
+
* @returns A clone of the schema with the maximum length set
|
|
153
|
+
*/
|
|
154
|
+
max(max, message) {
|
|
155
|
+
return this.setCheck({ type: "max", val: max, message });
|
|
156
|
+
}
|
|
157
|
+
regex(regex, message) {
|
|
158
|
+
return this.setCheck({ type: "regex", regex, message });
|
|
159
|
+
}
|
|
160
|
+
startsWith(prefix, message) {
|
|
161
|
+
return this.setCheck({ type: "startsWith", prefix, message });
|
|
162
|
+
}
|
|
163
|
+
endsWith(suffix, message) {
|
|
164
|
+
return this.setCheck({ type: "endsWith", suffix, message });
|
|
165
|
+
}
|
|
166
|
+
format(format, message) {
|
|
167
|
+
return this.setCheck({ type: "format", format, message });
|
|
168
|
+
}
|
|
169
|
+
uri(message) {
|
|
170
|
+
return this.format("uri", message);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Deprecated because OpenAPI uses the term "uri"
|
|
174
|
+
* but this method exists for making migration from
|
|
175
|
+
* Zod easier.
|
|
176
|
+
*
|
|
177
|
+
* @deprecated Use {@link uri} instead
|
|
178
|
+
*/
|
|
179
|
+
url(message) {
|
|
180
|
+
return this.uri(message);
|
|
181
|
+
}
|
|
182
|
+
email(message) {
|
|
183
|
+
return this.format("email", message);
|
|
184
|
+
}
|
|
185
|
+
uuid(message) {
|
|
186
|
+
return this.format("uuid", message);
|
|
187
|
+
}
|
|
188
|
+
ipv4(message) {
|
|
189
|
+
return this.format("ipv4", message);
|
|
190
|
+
}
|
|
191
|
+
ipv6(message) {
|
|
192
|
+
return this.format("ipv6", message);
|
|
193
|
+
}
|
|
194
|
+
date(message) {
|
|
195
|
+
return this.format("date", message);
|
|
196
|
+
}
|
|
197
|
+
dateTime(message) {
|
|
198
|
+
return this.format("date-time", message);
|
|
199
|
+
}
|
|
200
|
+
password(message) {
|
|
201
|
+
return this.format("password", message);
|
|
202
|
+
}
|
|
203
|
+
byte(message) {
|
|
204
|
+
return this.format("byte", message);
|
|
205
|
+
}
|
|
206
|
+
binary(message) {
|
|
207
|
+
return this.format("binary", message);
|
|
208
|
+
}
|
|
209
|
+
hostname(message) {
|
|
210
|
+
return this.format("hostname", message);
|
|
211
|
+
}
|
|
212
|
+
parseSafe(json) {
|
|
213
|
+
return ParseContext.result((ctx) => {
|
|
214
|
+
if (typeof json !== "string") {
|
|
215
|
+
return ctx.addIssue("Expected string", []);
|
|
216
|
+
}
|
|
217
|
+
if (this.def.min !== void 0 && json.length < this.def.min.val) {
|
|
218
|
+
ctx.addIssue(this.def.min.message ?? `String must be at least ${this.def.min.val} characters`, []);
|
|
219
|
+
}
|
|
220
|
+
if (this.def.max !== void 0 && json.length > this.def.max.val) {
|
|
221
|
+
ctx.addIssue(this.def.max.message ?? `String must be at most ${this.def.max.val} characters`, []);
|
|
222
|
+
}
|
|
223
|
+
if (this.def.regex !== void 0 && !this.def.regex.regex.test(json)) {
|
|
224
|
+
ctx.addIssue(this.def.regex.message ?? `String must match ${this.def.regex.regex.source}`, []);
|
|
225
|
+
}
|
|
226
|
+
if (this.def.startsWith !== void 0 && !json.startsWith(this.def.startsWith.prefix)) {
|
|
227
|
+
ctx.addIssue(this.def.startsWith.message ?? `String must start with "${this.def.startsWith.prefix}"`, []);
|
|
228
|
+
}
|
|
229
|
+
if (this.def.endsWith !== void 0 && !json.endsWith(this.def.endsWith.suffix)) {
|
|
230
|
+
ctx.addIssue(this.def.endsWith.message ?? `String must end with "${this.def.endsWith.suffix}"`, []);
|
|
231
|
+
}
|
|
232
|
+
if (this.def.format !== void 0) {
|
|
233
|
+
switch (this.def.format.format) {
|
|
234
|
+
case "uuid":
|
|
235
|
+
if (!STRING_FORMAT_REGEXES.uuid.test(json)) {
|
|
236
|
+
ctx.addIssue(this.def.format.message ?? "Invalid UUID format", []);
|
|
237
|
+
}
|
|
238
|
+
break;
|
|
239
|
+
case "email":
|
|
240
|
+
if (!STRING_FORMAT_REGEXES.email.test(json)) {
|
|
241
|
+
ctx.addIssue(this.def.format.message ?? "Invalid email format", []);
|
|
242
|
+
}
|
|
243
|
+
break;
|
|
244
|
+
case "ipv4":
|
|
245
|
+
if (!STRING_FORMAT_REGEXES.ipv4.test(json)) {
|
|
246
|
+
ctx.addIssue(this.def.format.message ?? "Invalid IPv4 address", []);
|
|
247
|
+
}
|
|
248
|
+
break;
|
|
249
|
+
case "ipv6":
|
|
250
|
+
if (!STRING_FORMAT_REGEXES.ipv6.test(json)) {
|
|
251
|
+
ctx.addIssue(this.def.format.message ?? "Invalid IPv6 address", []);
|
|
252
|
+
}
|
|
253
|
+
break;
|
|
254
|
+
case "date":
|
|
255
|
+
if (!STRING_FORMAT_REGEXES.date.test(json)) {
|
|
256
|
+
ctx.addIssue(this.def.format.message ?? "Invalid date format", []);
|
|
257
|
+
}
|
|
258
|
+
break;
|
|
259
|
+
case "date-time":
|
|
260
|
+
if (Number.isNaN(new Date(json).getTime())) {
|
|
261
|
+
ctx.addIssue(this.def.format.message ?? "Invalid date-time format", []);
|
|
262
|
+
}
|
|
263
|
+
break;
|
|
264
|
+
case "byte":
|
|
265
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(json) || json.length % 4 !== 0) {
|
|
266
|
+
ctx.addIssue(this.def.format.message ?? "Invalid base64 format", []);
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
case "uri":
|
|
270
|
+
if (!STRING_FORMAT_REGEXES.uri.test(json)) {
|
|
271
|
+
ctx.addIssue(this.def.format.message ?? "Invalid URI format", []);
|
|
272
|
+
}
|
|
273
|
+
break;
|
|
274
|
+
case "hostname":
|
|
275
|
+
if (!STRING_FORMAT_REGEXES.hostname.test(json)) {
|
|
276
|
+
ctx.addIssue(this.def.format.message ?? "Invalid hostname format", []);
|
|
277
|
+
}
|
|
278
|
+
break;
|
|
279
|
+
case "binary":
|
|
280
|
+
break;
|
|
281
|
+
case "password":
|
|
282
|
+
break;
|
|
283
|
+
default:
|
|
284
|
+
this.def.format.format;
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return json;
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
parse(json) {
|
|
292
|
+
const result = this.parseSafe(json);
|
|
293
|
+
if (!result.success) {
|
|
294
|
+
throw new SchemaError(result.issues);
|
|
295
|
+
}
|
|
296
|
+
return result.result;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
var KNumber = class _KNumber extends BaseSchema {
|
|
300
|
+
static create = () => new _KNumber({});
|
|
301
|
+
serialize(value) {
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
setCheck(check) {
|
|
305
|
+
return this.clone({ [check.type]: check });
|
|
306
|
+
}
|
|
307
|
+
toOpenAPI() {
|
|
308
|
+
const baseSchema = this.getSchemaObject();
|
|
309
|
+
const schema = {
|
|
310
|
+
...baseSchema,
|
|
311
|
+
type: "number"
|
|
312
|
+
};
|
|
313
|
+
if (this.def.min !== void 0) {
|
|
314
|
+
schema.minimum = this.def.min.val;
|
|
315
|
+
}
|
|
316
|
+
if (this.def.max !== void 0) {
|
|
317
|
+
schema.maximum = this.def.max.val;
|
|
318
|
+
}
|
|
319
|
+
if (this.def.multipleOf !== void 0) {
|
|
320
|
+
schema.multipleOf = this.def.multipleOf.val;
|
|
321
|
+
}
|
|
322
|
+
if (this.def.integer) {
|
|
323
|
+
schema.type = "integer";
|
|
324
|
+
}
|
|
325
|
+
if (this.def.format) {
|
|
326
|
+
switch (this.def.format.format) {
|
|
327
|
+
case "float":
|
|
328
|
+
schema.format = "float";
|
|
329
|
+
schema.type = "number";
|
|
330
|
+
break;
|
|
331
|
+
case "double":
|
|
332
|
+
schema.format = "double";
|
|
333
|
+
schema.type = "number";
|
|
334
|
+
break;
|
|
335
|
+
case "int32":
|
|
336
|
+
schema.format = "int32";
|
|
337
|
+
schema.type = "integer";
|
|
338
|
+
break;
|
|
339
|
+
case "int64":
|
|
340
|
+
schema.format = "int64";
|
|
341
|
+
schema.type = "integer";
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (this.def.format) {
|
|
346
|
+
schema.format = this.def.format.format;
|
|
347
|
+
}
|
|
348
|
+
return schema;
|
|
349
|
+
}
|
|
350
|
+
min(min) {
|
|
351
|
+
return this.setCheck({ type: "min", val: min });
|
|
352
|
+
}
|
|
353
|
+
max(max) {
|
|
354
|
+
return this.setCheck({ type: "max", val: max });
|
|
355
|
+
}
|
|
356
|
+
integer() {
|
|
357
|
+
return this.setCheck({ type: "integer" });
|
|
358
|
+
}
|
|
359
|
+
multipleOf(multipleOf) {
|
|
360
|
+
if (multipleOf <= 0) {
|
|
361
|
+
throw new Error("multipleOf must be a positive number");
|
|
362
|
+
}
|
|
363
|
+
return this.setCheck({ type: "multipleOf", val: multipleOf });
|
|
364
|
+
}
|
|
365
|
+
float() {
|
|
366
|
+
return this.setCheck({ type: "format", format: "float" });
|
|
367
|
+
}
|
|
368
|
+
double() {
|
|
369
|
+
return this.setCheck({ type: "format", format: "double" });
|
|
370
|
+
}
|
|
371
|
+
int32() {
|
|
372
|
+
return this.setCheck({ type: "format", format: "int32" }).integer();
|
|
373
|
+
}
|
|
374
|
+
int64() {
|
|
375
|
+
return this.setCheck({ type: "format", format: "int64" }).integer();
|
|
376
|
+
}
|
|
377
|
+
parseSafe(json) {
|
|
378
|
+
return ParseContext.result((ctx) => {
|
|
379
|
+
if (typeof json !== "number") {
|
|
380
|
+
return ctx.addIssue("Expected number", []);
|
|
381
|
+
}
|
|
382
|
+
if (this.def.integer && !Number.isInteger(json)) {
|
|
383
|
+
ctx.addIssue(this.def.integer.message ?? "Expected integer", []);
|
|
384
|
+
}
|
|
385
|
+
if (this.def.min !== void 0 && json < this.def.min.val) {
|
|
386
|
+
ctx.addIssue(this.def.min.message ?? `Number must be greater than or equal to ${this.def.min.val}`, []);
|
|
387
|
+
}
|
|
388
|
+
if (this.def.max !== void 0 && json > this.def.max.val) {
|
|
389
|
+
ctx.addIssue(this.def.max.message ?? `Number must be less than or equal to ${this.def.max.val}`, []);
|
|
390
|
+
}
|
|
391
|
+
if (this.def.multipleOf !== void 0 && json % this.def.multipleOf.val !== 0) {
|
|
392
|
+
ctx.addIssue(this.def.multipleOf.message ?? `Number must be multiple of ${this.def.multipleOf.val}`, []);
|
|
393
|
+
}
|
|
394
|
+
return json;
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
parse(json) {
|
|
398
|
+
const result = this.parseSafe(json);
|
|
399
|
+
if (!result.success) {
|
|
400
|
+
throw new SchemaError(result.issues);
|
|
401
|
+
}
|
|
402
|
+
return result.result;
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
var KBoolean = class _KBoolean extends BaseSchema {
|
|
406
|
+
static create = () => new _KBoolean({});
|
|
407
|
+
serialize(value) {
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
toOpenAPI() {
|
|
411
|
+
const baseSchema = this.getSchemaObject();
|
|
412
|
+
return {
|
|
413
|
+
...baseSchema,
|
|
414
|
+
type: "boolean"
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
parseSafe(json) {
|
|
418
|
+
return ParseContext.result((ctx) => {
|
|
419
|
+
if (typeof json !== "boolean") {
|
|
420
|
+
return ctx.addIssue("Expected boolean", []);
|
|
421
|
+
}
|
|
422
|
+
return json;
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
parse(json) {
|
|
426
|
+
const result = this.parseSafe(json);
|
|
427
|
+
if (!result.success) {
|
|
428
|
+
throw new SchemaError(result.issues);
|
|
429
|
+
}
|
|
430
|
+
return result.result;
|
|
431
|
+
}
|
|
432
|
+
};
|
|
433
|
+
var KArray = class _KArray extends BaseSchema {
|
|
434
|
+
static create = (items) => new _KArray({ items });
|
|
435
|
+
serialize(value) {
|
|
436
|
+
return value.map((item) => this.def.items.serialize(item));
|
|
437
|
+
}
|
|
438
|
+
setCheck(check) {
|
|
439
|
+
return this.clone({ [check.type]: check });
|
|
440
|
+
}
|
|
441
|
+
toOpenAPI() {
|
|
442
|
+
const baseSchema = this.getSchemaObject();
|
|
443
|
+
return {
|
|
444
|
+
...baseSchema,
|
|
445
|
+
type: "array",
|
|
446
|
+
items: this.def.items.toOpenAPI(),
|
|
447
|
+
...this.def.minItems !== void 0 ? { minItems: this.def.minItems.val } : {},
|
|
448
|
+
...this.def.maxItems !== void 0 ? { maxItems: this.def.maxItems.val } : {},
|
|
449
|
+
...this.def.uniqueItems !== void 0 ? { uniqueItems: this.def.uniqueItems.val } : {}
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
min(minItems) {
|
|
453
|
+
return this.setCheck({ type: "minItems", val: minItems });
|
|
454
|
+
}
|
|
455
|
+
max(maxItems) {
|
|
456
|
+
return this.setCheck({ type: "maxItems", val: maxItems });
|
|
457
|
+
}
|
|
458
|
+
unique() {
|
|
459
|
+
return this.setCheck({ type: "uniqueItems", val: true });
|
|
460
|
+
}
|
|
461
|
+
notUnique() {
|
|
462
|
+
return this.setCheck({ type: "uniqueItems", val: false });
|
|
463
|
+
}
|
|
464
|
+
parseSafe(json) {
|
|
465
|
+
return ParseContext.result((ctx) => {
|
|
466
|
+
if (!Array.isArray(json)) {
|
|
467
|
+
return ctx.addIssue("Expected array", []);
|
|
468
|
+
}
|
|
469
|
+
if (this.def.minItems !== void 0 && json.length < this.def.minItems.val) {
|
|
470
|
+
ctx.addIssue(this.def.minItems.message ?? `Array must have at least ${this.def.minItems.val} items`, []);
|
|
471
|
+
}
|
|
472
|
+
if (this.def.maxItems !== void 0 && json.length > this.def.maxItems.val) {
|
|
473
|
+
ctx.addIssue(this.def.maxItems.message ?? `Array must have at most ${this.def.maxItems.val} items`, []);
|
|
474
|
+
}
|
|
475
|
+
if (this.def.uniqueItems?.val === true && new Set(json).size !== json.length) {
|
|
476
|
+
ctx.addIssue(this.def.uniqueItems.message ?? "Array items must be unique", []);
|
|
477
|
+
}
|
|
478
|
+
const items = [];
|
|
479
|
+
for (let i = 0; i < json.length; i++) {
|
|
480
|
+
const item = json[i];
|
|
481
|
+
const result = this.def.items.parseSafe(item);
|
|
482
|
+
if (!result.success) {
|
|
483
|
+
ctx.addIssues(result.issues, [i.toString()]);
|
|
484
|
+
} else {
|
|
485
|
+
items.push(result.result);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return items;
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
parse(json) {
|
|
492
|
+
const result = this.parseSafe(json);
|
|
493
|
+
if (!result.success) {
|
|
494
|
+
throw new SchemaError(result.issues);
|
|
495
|
+
}
|
|
496
|
+
return result.result;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
var KNull = class _KNull extends BaseSchema {
|
|
500
|
+
static create = () => new _KNull({});
|
|
501
|
+
serialize(value) {
|
|
502
|
+
return value;
|
|
503
|
+
}
|
|
504
|
+
toOpenAPI() {
|
|
505
|
+
const baseSchema = this.getSchemaObject();
|
|
506
|
+
return {
|
|
507
|
+
...baseSchema,
|
|
508
|
+
type: "null"
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
parseSafe(json) {
|
|
512
|
+
return ParseContext.result((ctx) => {
|
|
513
|
+
if (json !== null) {
|
|
514
|
+
return ctx.addIssue("Expected null", []);
|
|
515
|
+
}
|
|
516
|
+
return null;
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
parse(json) {
|
|
520
|
+
const result = this.parseSafe(json);
|
|
521
|
+
if (!result.success) {
|
|
522
|
+
throw new SchemaError(result.issues);
|
|
523
|
+
}
|
|
524
|
+
return result.result;
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
var KObject = class _KObject extends BaseSchema {
|
|
528
|
+
static create = (shape) => new _KObject({ shape });
|
|
529
|
+
serialize(value) {
|
|
530
|
+
const result = {};
|
|
531
|
+
for (const key in this.def.shape) {
|
|
532
|
+
if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
|
|
533
|
+
const fieldValue = value[key];
|
|
534
|
+
if (fieldValue === void 0) {
|
|
535
|
+
throw new Error(`Missing required property: ${key}`);
|
|
536
|
+
}
|
|
537
|
+
result[key] = this.def.shape[key].serialize(fieldValue);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return result;
|
|
541
|
+
}
|
|
542
|
+
toOpenAPI() {
|
|
543
|
+
const baseSchema = this.getSchemaObject();
|
|
544
|
+
return {
|
|
545
|
+
...baseSchema,
|
|
546
|
+
type: "object",
|
|
547
|
+
properties: Object.fromEntries(
|
|
548
|
+
Object.entries(this.def.shape).map((entry) => {
|
|
549
|
+
const [key, value] = entry;
|
|
550
|
+
return [key, value.toOpenAPI()];
|
|
551
|
+
})
|
|
552
|
+
),
|
|
553
|
+
required: Object.keys(this.def.shape)
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
parseSafe(json) {
|
|
557
|
+
return ParseContext.result((ctx) => {
|
|
558
|
+
if (typeof json !== "object" || json === null || Array.isArray(json)) {
|
|
559
|
+
return ctx.addIssue(`Expected object, got ${typeof json}`, []);
|
|
560
|
+
}
|
|
561
|
+
const result = {};
|
|
562
|
+
for (const key in this.def.shape) {
|
|
563
|
+
if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
|
|
564
|
+
const value = json[key];
|
|
565
|
+
if (value === void 0) {
|
|
566
|
+
return ctx.addIssue(`Missing required property: ${key}`, [key]);
|
|
567
|
+
}
|
|
568
|
+
const parseResult = this.def.shape[key].parseSafe(value);
|
|
569
|
+
if (!parseResult.success) {
|
|
570
|
+
return ctx.addIssues(parseResult.issues, [key]);
|
|
571
|
+
}
|
|
572
|
+
result[key] = parseResult.result;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
return result;
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
parse(json) {
|
|
579
|
+
const result = this.parseSafe(json);
|
|
580
|
+
if (!result.success) {
|
|
581
|
+
throw new SchemaError(result.issues);
|
|
582
|
+
}
|
|
583
|
+
return result.result;
|
|
584
|
+
}
|
|
585
|
+
get shape() {
|
|
586
|
+
return this.def.shape;
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
var KObjectFromURLSearchParams = class _KObjectFromURLSearchParams extends KObject {
|
|
590
|
+
static create = (shape) => new _KObjectFromURLSearchParams({ shape });
|
|
591
|
+
serialize(value) {
|
|
592
|
+
return super.serialize(value);
|
|
593
|
+
}
|
|
594
|
+
toOpenAPI() {
|
|
595
|
+
return super.toOpenAPI();
|
|
596
|
+
}
|
|
597
|
+
parseSafe(json) {
|
|
598
|
+
return ParseContext.result((ctx) => {
|
|
599
|
+
if (!(json instanceof URLSearchParams)) {
|
|
600
|
+
return ctx.addIssue(`Expected URLSearchParams, got ${typeof json}`, []);
|
|
601
|
+
}
|
|
602
|
+
const result = {};
|
|
603
|
+
for (const key in this.def.shape) {
|
|
604
|
+
if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
|
|
605
|
+
const value = json.get(key);
|
|
606
|
+
if (value === null) {
|
|
607
|
+
return ctx.addIssue(`Missing required property: ${key}`, [key]);
|
|
608
|
+
}
|
|
609
|
+
const parseResult = this.def.shape[key].parseSafe(value);
|
|
610
|
+
if (!parseResult.success) {
|
|
611
|
+
return ctx.addIssues(parseResult.issues, [key]);
|
|
612
|
+
}
|
|
613
|
+
result[key] = parseResult.result;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
return result;
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
var KRef = class _KRef extends BaseSchema {
|
|
621
|
+
static create = (name, shape) => new _KRef({ name, shape });
|
|
622
|
+
serialize(value) {
|
|
623
|
+
const result = {};
|
|
624
|
+
for (const key in this.def.shape) {
|
|
625
|
+
if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
|
|
626
|
+
const fieldValue = value[key];
|
|
627
|
+
if (fieldValue === void 0) {
|
|
628
|
+
throw new Error(`Missing required property: ${key}`);
|
|
629
|
+
}
|
|
630
|
+
result[key] = this.def.shape[key].serialize(fieldValue);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
return result;
|
|
634
|
+
}
|
|
635
|
+
example() {
|
|
636
|
+
throw new Error("Cannot set an example on a KRef");
|
|
637
|
+
}
|
|
638
|
+
toOpenAPI() {
|
|
639
|
+
return {
|
|
640
|
+
$ref: `#/components/schemas/${this.def.name}`,
|
|
641
|
+
...this.def.description ? { description: this.def.description } : {},
|
|
642
|
+
...this.def.summary ? { summary: this.def.summary } : {}
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
parseSafe(json) {
|
|
646
|
+
return ParseContext.result((ctx) => {
|
|
647
|
+
if (typeof json !== "object" || json === null || Array.isArray(json)) {
|
|
648
|
+
return ctx.addIssue(`Expected object, got ${typeof json}`, []);
|
|
649
|
+
}
|
|
650
|
+
const result = {};
|
|
651
|
+
for (const key in this.def.shape) {
|
|
652
|
+
if (Object.prototype.hasOwnProperty.call(this.def.shape, key)) {
|
|
653
|
+
const value = json[key];
|
|
654
|
+
if (value === void 0) {
|
|
655
|
+
return ctx.addIssue(`Missing required property: ${key}`, [key]);
|
|
656
|
+
}
|
|
657
|
+
const parseResult = this.def.shape[key].parseSafe(value);
|
|
658
|
+
if (!parseResult.success) {
|
|
659
|
+
return ctx.addIssues(parseResult.issues, [key]);
|
|
660
|
+
}
|
|
661
|
+
result[key] = parseResult.result;
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return result;
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
parse(json) {
|
|
668
|
+
const result = this.parseSafe(json);
|
|
669
|
+
if (!result.success) {
|
|
670
|
+
throw new SchemaError(result.issues);
|
|
671
|
+
}
|
|
672
|
+
return result.result;
|
|
673
|
+
}
|
|
674
|
+
summary(summary) {
|
|
675
|
+
if (summary === void 0) {
|
|
676
|
+
return this.def.summary;
|
|
677
|
+
}
|
|
678
|
+
return this.clone({ summary });
|
|
679
|
+
}
|
|
680
|
+
get shape() {
|
|
681
|
+
return this.def.shape;
|
|
682
|
+
}
|
|
683
|
+
get name() {
|
|
684
|
+
return this.def.name;
|
|
685
|
+
}
|
|
686
|
+
};
|
|
687
|
+
var KScalar = class _KScalar extends BaseSchema {
|
|
688
|
+
static create = (options) => new _KScalar(options);
|
|
689
|
+
constructor(def) {
|
|
690
|
+
super(def);
|
|
691
|
+
}
|
|
692
|
+
serialize(value) {
|
|
693
|
+
return this.def.toClient(value);
|
|
694
|
+
}
|
|
695
|
+
toOpenAPI() {
|
|
696
|
+
return this.def.schema.toOpenAPI();
|
|
697
|
+
}
|
|
698
|
+
parseSafe(json) {
|
|
699
|
+
return ParseContext.result((ctx) => {
|
|
700
|
+
const jsonValue = this.def.schema.parseSafe(json);
|
|
701
|
+
if (!jsonValue.success) {
|
|
702
|
+
return ctx.addIssues(jsonValue.issues, []);
|
|
703
|
+
}
|
|
704
|
+
try {
|
|
705
|
+
return this.def.toServer(jsonValue.result);
|
|
706
|
+
} catch (error) {
|
|
707
|
+
return ctx.addIssue(error instanceof Error ? error.message : "Conversion failed", []);
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
example(example) {
|
|
712
|
+
if (example === void 0) {
|
|
713
|
+
return this.def.example;
|
|
714
|
+
}
|
|
715
|
+
return this.clone({ example });
|
|
716
|
+
}
|
|
717
|
+
description(description) {
|
|
718
|
+
if (description === void 0) {
|
|
719
|
+
return this.def.schema.description();
|
|
720
|
+
}
|
|
721
|
+
return this.clone({ description });
|
|
722
|
+
}
|
|
723
|
+
parse(json) {
|
|
724
|
+
const result = this.parseSafe(json);
|
|
725
|
+
if (!result.success) {
|
|
726
|
+
throw new SchemaError(result.issues);
|
|
727
|
+
}
|
|
728
|
+
return result.result;
|
|
729
|
+
}
|
|
730
|
+
};
|
|
731
|
+
var KUnion = class _KUnion extends BaseSchema {
|
|
732
|
+
static create = (items) => new _KUnion({ items });
|
|
733
|
+
serialize(value) {
|
|
734
|
+
for (const option of this.def.items) {
|
|
735
|
+
try {
|
|
736
|
+
return option.serialize(value);
|
|
737
|
+
} catch {
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
throw new Error("Value does not match any union option for serialization");
|
|
741
|
+
}
|
|
742
|
+
toOpenAPI() {
|
|
743
|
+
const baseSchema = this.getSchemaObject();
|
|
744
|
+
return {
|
|
745
|
+
...baseSchema,
|
|
746
|
+
oneOf: this.def.items.map((option) => option.toOpenAPI())
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
parseSafe(json) {
|
|
750
|
+
let lastIssues;
|
|
751
|
+
for (const option of this.def.items) {
|
|
752
|
+
const result = option.parseSafe(json);
|
|
753
|
+
if (result.success) {
|
|
754
|
+
return { success: true, result: result.result };
|
|
755
|
+
} else {
|
|
756
|
+
lastIssues = result.issues;
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
return { success: false, issues: lastIssues ?? /* @__PURE__ */ new Set([{ message: "No union option matched", path: [] }]) };
|
|
760
|
+
}
|
|
761
|
+
parse(json) {
|
|
762
|
+
const result = this.parseSafe(json);
|
|
763
|
+
if (!result.success) {
|
|
764
|
+
throw new SchemaError(result.issues);
|
|
765
|
+
}
|
|
766
|
+
return result.result;
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
var KLiteral = class _KLiteral extends BaseSchema {
|
|
770
|
+
parse(json) {
|
|
771
|
+
const result = this.parseSafe(json);
|
|
772
|
+
if (!result.success) {
|
|
773
|
+
throw new SchemaError(result.issues);
|
|
774
|
+
}
|
|
775
|
+
return result.result;
|
|
776
|
+
}
|
|
777
|
+
static create = (value) => new _KLiteral({ value });
|
|
778
|
+
serialize(value) {
|
|
779
|
+
return value;
|
|
780
|
+
}
|
|
781
|
+
toOpenAPI() {
|
|
782
|
+
const baseSchema = this.getSchemaObject();
|
|
783
|
+
const type = typeof this.def.value;
|
|
784
|
+
return {
|
|
785
|
+
...baseSchema,
|
|
786
|
+
type,
|
|
787
|
+
enum: [this.def.value]
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
parseSafe(json) {
|
|
791
|
+
return ParseContext.result((ctx) => {
|
|
792
|
+
if (json !== this.def.value) {
|
|
793
|
+
return ctx.addIssue(`Expected ${this.def.value}`, []);
|
|
794
|
+
}
|
|
795
|
+
return this.def.value;
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
};
|
|
799
|
+
var k = {
|
|
800
|
+
string: KString.create,
|
|
801
|
+
number: KNumber.create,
|
|
802
|
+
boolean: KBoolean.create,
|
|
803
|
+
array: KArray.create,
|
|
804
|
+
null: KNull.create,
|
|
805
|
+
ref: KRef.create,
|
|
806
|
+
object: KObject.create,
|
|
807
|
+
scalar: KScalar.create,
|
|
808
|
+
literal: KLiteral.create,
|
|
809
|
+
union: KUnion.create,
|
|
810
|
+
/**
|
|
811
|
+
* @internal
|
|
812
|
+
* @experimental
|
|
813
|
+
*/
|
|
814
|
+
objectFromURLSearchParams: KObjectFromURLSearchParams.create
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
export {
|
|
818
|
+
isPrimitiveJSONValue,
|
|
819
|
+
SchemaError,
|
|
820
|
+
ParseContext,
|
|
821
|
+
BaseSchema,
|
|
822
|
+
STRING_FORMAT_REGEXES,
|
|
823
|
+
KString,
|
|
824
|
+
KNumber,
|
|
825
|
+
KBoolean,
|
|
826
|
+
KArray,
|
|
827
|
+
KNull,
|
|
828
|
+
KObject,
|
|
829
|
+
KObjectFromURLSearchParams,
|
|
830
|
+
KRef,
|
|
831
|
+
KScalar,
|
|
832
|
+
KUnion,
|
|
833
|
+
KLiteral,
|
|
834
|
+
k
|
|
835
|
+
};
|