@power-plant/schema 0.0.36 → 0.0.38
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/codegen.cjs +1 -1
- package/dist/codegen.d.cts +1 -1
- package/dist/codegen.d.mts +1 -1
- package/dist/codegen.mjs +7 -7
- package/dist/deepkit.cjs +9 -0
- package/dist/deepkit.d.cts +1 -0
- package/dist/deepkit.d.mts +1 -0
- package/dist/deepkit.mjs +3 -0
- package/dist/{helpers-CQS3qnsv.cjs → helpers-DbFdMcOB.cjs} +1 -1
- package/dist/{helpers-BxWdDZn4.mjs → helpers-_8ADIMSN.mjs} +2 -2
- package/dist/helpers-_8ADIMSN.mjs.map +1 -0
- package/dist/index.cjs +576 -98
- package/dist/index.d.cts +20 -16
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +20 -16
- package/dist/index.mjs +578 -102
- package/dist/{types-OH3Npqz2.d.mts → types-BH-Q7QrB.d.cts} +103 -76
- package/dist/types-BH-Q7QrB.d.cts.map +1 -0
- package/dist/{types-CEGqfUA4.d.cts → types-BrSNvUQg.d.mts} +103 -76
- package/dist/types-BrSNvUQg.d.mts.map +1 -0
- package/dist/valibot.d.cts +1 -1
- package/dist/valibot.d.mts +1 -1
- package/dist/zod.cjs +1 -1
- package/dist/zod.d.cts +1 -1
- package/dist/zod.d.mts +1 -1
- package/dist/zod.mjs +1 -1
- package/package.json +36 -19
- package/dist/helpers-BxWdDZn4.mjs.map +0 -1
- package/dist/types-CEGqfUA4.d.cts.map +0 -1
- package/dist/types-OH3Npqz2.d.mts.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
import { a as JsonSchemaTypeNames, c as SCHEMA_SINGLE_KEYWORDS, i as JSON_SCHEMA_TYPES, n as JSON_SCHEMA_METADATA_KEYS, o as SCHEMA_ARRAY_CONCAT_KEYWORDS, r as JSON_SCHEMA_PRIMITIVE_TYPES, s as SCHEMA_RECORD_KEYWORDS, t as JSON_SCHEMA_CONSTRUCTOR_KEY } from "./constants-BlppSDwG.mjs";
|
|
2
|
-
import { $ as isUntypedConfig, A as isJsonSchemaNativeEnum, B as isJsonSchemaSet, C as isJsonSchemaDate, D as isJsonSchemaKeywords, E as isJsonSchemaInteger, F as isJsonSchemaObject, G as isJsonSchemaUnion, H as isJsonSchemaTuple, I as isJsonSchemaPrimitiveType, J as isSchema, K as isJsonSchemaUnknown, L as isJsonSchemaPrimitiveUnion, M as isJsonSchemaNull, N as isJsonSchemaNullable, O as isJsonSchemaLiteral, P as isJsonSchemaNumber, Q as isStandardSchema, R as isJsonSchemaRecord, S as isJsonSchemaBoolean, T as isJsonSchemaEnum, U as isJsonSchemaType, V as isJsonSchemaString, W as isJsonSchemaUndefined, X as isSchemaOf, Y as isSchemaObject, Z as isSchemaWithSource, _ as isJsonSchemaAllOf, a as getPropertiesList, at as isValibotSchema, b as isJsonSchemaArray, c as isSchemaNullable, ct as readSchemaTypes, d as merge, et as isUntypedConfigStrict, f as mergeMetadata, g as isJsonSchema, h as isFileReference, i as getProperties, it as isUntypedSchemaStrict, j as isJsonSchemaNever, k as isJsonSchemaMap, l as isValidSchemaConfigFile, m as hasJsonSchemaConstructorFlag, n as getJsonSchema, nt as isUntypedInputStrict, o as getProperty, ot as applyJsonSchemaMetadata, p as traverseSchema, q as isNullOnlyJsonSchema, r as getJsonSchemaObject, rt as isUntypedSchema, s as isPropertyOptional, st as getPrimarySchemaType, t as addProperty, tt as isUntypedInput, u as isValidSchemaInputFile, v as isJsonSchemaAny, w as isJsonSchemaDecimal, x as isJsonSchemaBigint, y as isJsonSchemaAnyOf, z as isJsonSchemaRef } from "./helpers-
|
|
2
|
+
import { $ as isUntypedConfig, A as isJsonSchemaNativeEnum, B as isJsonSchemaSet, C as isJsonSchemaDate, D as isJsonSchemaKeywords, E as isJsonSchemaInteger, F as isJsonSchemaObject, G as isJsonSchemaUnion, H as isJsonSchemaTuple, I as isJsonSchemaPrimitiveType, J as isSchema, K as isJsonSchemaUnknown, L as isJsonSchemaPrimitiveUnion, M as isJsonSchemaNull, N as isJsonSchemaNullable, O as isJsonSchemaLiteral, P as isJsonSchemaNumber, Q as isStandardSchema, R as isJsonSchemaRecord, S as isJsonSchemaBoolean, T as isJsonSchemaEnum, U as isJsonSchemaType, V as isJsonSchemaString, W as isJsonSchemaUndefined, X as isSchemaOf, Y as isSchemaObject, Z as isSchemaWithSource, _ as isJsonSchemaAllOf, a as getPropertiesList, at as isValibotSchema, b as isJsonSchemaArray, c as isSchemaNullable, ct as readSchemaTypes, d as merge, et as isUntypedConfigStrict, f as mergeMetadata, g as isJsonSchema, h as isFileReference, i as getProperties, it as isUntypedSchemaStrict, j as isJsonSchemaNever, k as isJsonSchemaMap, l as isValidSchemaConfigFile, m as hasJsonSchemaConstructorFlag, n as getJsonSchema, nt as isUntypedInputStrict, o as getProperty, ot as applyJsonSchemaMetadata, p as traverseSchema, q as isNullOnlyJsonSchema, r as getJsonSchemaObject, rt as isUntypedSchema, s as isPropertyOptional, st as getPrimarySchemaType, t as addProperty, tt as isUntypedInput, u as isValidSchemaInputFile, v as isJsonSchemaAny, w as isJsonSchemaDecimal, x as isJsonSchemaBigint, y as isJsonSchemaAnyOf, z as isJsonSchemaRef } from "./helpers-_8ADIMSN.mjs";
|
|
3
|
+
import { getJsonSchemaType } from "./codegen.mjs";
|
|
3
4
|
import { n as createStoragePromises, r as normalizeKey, t as mapStorageToFileSystem } from "./storage-C3BT2Xh-.mjs";
|
|
4
5
|
import { isSetObject } from "@stryke/type-checks/is-set-object";
|
|
5
6
|
import { isSetString } from "@stryke/type-checks/is-set-string";
|
|
6
|
-
import { isSetString as isSetString$1 } from "@stryke/type-checks";
|
|
7
|
+
import { isBigInt, isBoolean, isInteger, isNull, isNumber, isRegExp, isSetArray, isSetObject as isSetObject$1, isSetString as isSetString$1, isString, isUndefined } from "@stryke/type-checks";
|
|
8
|
+
import { ReflectionClass, ReflectionKind, TypeNumberBrand, isType, reflect, stringifyType } from "@deepkit/type";
|
|
9
|
+
import { Cache, DeclarationTransformer, ReflectionTransformer } from "@deepkit/type-compiler";
|
|
7
10
|
import { extractFileReference } from "@stryke/convert/extract-file-reference";
|
|
8
11
|
import { resolveSafe } from "@stryke/fs/resolve";
|
|
9
12
|
import { murmurhash } from "@stryke/hash";
|
|
10
13
|
import { deepClone } from "@stryke/helpers/deep-clone";
|
|
14
|
+
import { omit } from "@stryke/helpers/omit";
|
|
11
15
|
import { isStandardJsonSchema } from "@stryke/json";
|
|
12
16
|
import { appendPath } from "@stryke/path/append";
|
|
13
|
-
import { findFileExtensionSafe, findFilePath } from "@stryke/path/find";
|
|
17
|
+
import { findFileDotExtensionSafe, findFileExtensionSafe, findFilePath } from "@stryke/path/find";
|
|
14
18
|
import { joinPaths } from "@stryke/path/join";
|
|
15
19
|
import { VALID_OBJECT_SOURCE_EXTENSIONS } from "@stryke/resolve/constants";
|
|
16
20
|
import { loadSafe } from "@stryke/resolve/load";
|
|
17
|
-
import { resolve } from "@stryke/resolve/resolve";
|
|
18
21
|
import { list } from "@stryke/string-format/list";
|
|
19
22
|
import { extractJsonSchema as extractJsonSchema$1, isZod3Type } from "@stryke/zod";
|
|
20
23
|
import { toJsonSchema } from "@valibot/to-json-schema";
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import
|
|
24
|
+
import { build } from "esbuild";
|
|
25
|
+
import { createJiti } from "jiti";
|
|
26
|
+
import { readFile } from "node:fs/promises";
|
|
27
|
+
import ts, { DiagnosticCategory } from "typescript";
|
|
28
|
+
import defu from "defu";
|
|
24
29
|
|
|
25
30
|
//#region src/compatibility.ts
|
|
26
31
|
const METADATA_KEYS = /* @__PURE__ */ new Set([
|
|
@@ -124,6 +129,412 @@ function assertSchemasDoNotContradict(base, override, label = "schema") {
|
|
|
124
129
|
if (issues.length > 0) throw new Error(`The ${label} schema contradicts the generator schema:\n${issues.map((issue) => `- ${issue}`).join("\n")}`);
|
|
125
130
|
}
|
|
126
131
|
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/reflection.ts
|
|
134
|
+
/**
|
|
135
|
+
* Maps a Deepkit numeric `brand` to JSON Schema `type` and `format`.
|
|
136
|
+
*
|
|
137
|
+
* @remarks
|
|
138
|
+
* This function takes a `TypeNumberBrand` (which represents specific numeric types in Deepkit, such as `integer`, `float`, `int8`, etc.) and returns a corresponding JSON Schema fragment that includes the appropriate `type`, `format`, and any relevant keywords (like `multipleOf` for integers). If the brand is not recognized, it defaults to a generic JSON Schema for numbers.
|
|
139
|
+
*
|
|
140
|
+
* @param brand - The Deepkit numeric brand to convert.
|
|
141
|
+
* @return A JSON Schema fragment representing the numeric type corresponding to the provided brand.
|
|
142
|
+
*/
|
|
143
|
+
function numberBrandToJsonSchema(brand) {
|
|
144
|
+
switch (brand) {
|
|
145
|
+
case TypeNumberBrand.integer: return {
|
|
146
|
+
type: "integer",
|
|
147
|
+
format: "int32",
|
|
148
|
+
multipleOf: 1
|
|
149
|
+
};
|
|
150
|
+
case TypeNumberBrand.int8: return {
|
|
151
|
+
type: "integer",
|
|
152
|
+
format: "int8",
|
|
153
|
+
multipleOf: 1
|
|
154
|
+
};
|
|
155
|
+
case TypeNumberBrand.uint8: return {
|
|
156
|
+
type: "integer",
|
|
157
|
+
format: "uint8",
|
|
158
|
+
multipleOf: 1
|
|
159
|
+
};
|
|
160
|
+
case TypeNumberBrand.int16: return {
|
|
161
|
+
type: "integer",
|
|
162
|
+
format: "int16",
|
|
163
|
+
multipleOf: 1
|
|
164
|
+
};
|
|
165
|
+
case TypeNumberBrand.uint16: return {
|
|
166
|
+
type: "integer",
|
|
167
|
+
format: "uint16",
|
|
168
|
+
multipleOf: 1
|
|
169
|
+
};
|
|
170
|
+
case TypeNumberBrand.int32: return {
|
|
171
|
+
type: "integer",
|
|
172
|
+
format: "int32",
|
|
173
|
+
multipleOf: 1
|
|
174
|
+
};
|
|
175
|
+
case TypeNumberBrand.uint32: return {
|
|
176
|
+
type: "integer",
|
|
177
|
+
format: "uint32",
|
|
178
|
+
multipleOf: 1
|
|
179
|
+
};
|
|
180
|
+
case TypeNumberBrand.float:
|
|
181
|
+
case TypeNumberBrand.float32: return {
|
|
182
|
+
type: "number",
|
|
183
|
+
format: "float"
|
|
184
|
+
};
|
|
185
|
+
case TypeNumberBrand.float64: return {
|
|
186
|
+
type: "number",
|
|
187
|
+
format: "double"
|
|
188
|
+
};
|
|
189
|
+
case void 0:
|
|
190
|
+
default: return { type: "number" };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function withReflectionTags(reflection, schema) {
|
|
194
|
+
if (!isSetObject$1(schema) || !isSetObject$1(reflection?.tags)) return schema;
|
|
195
|
+
const updatedSchema = { ...schema };
|
|
196
|
+
const tags = reflection.tags;
|
|
197
|
+
if (isSetString$1(tags.title)) updatedSchema.title = tags.title;
|
|
198
|
+
if (isSetArray(tags.alias)) updatedSchema.alias = tags.alias;
|
|
199
|
+
if (!isUndefined(tags.hidden)) updatedSchema.hidden = tags.hidden;
|
|
200
|
+
if (!isUndefined(tags.ignore)) updatedSchema.ignore = tags.ignore;
|
|
201
|
+
if (!isUndefined(tags.internal)) updatedSchema.internal = tags.internal;
|
|
202
|
+
if (!isUndefined(tags.runtime)) updatedSchema.runtime = tags.runtime;
|
|
203
|
+
if (!isUndefined(tags.readonly)) updatedSchema.readOnly = tags.readonly;
|
|
204
|
+
return updatedSchema;
|
|
205
|
+
}
|
|
206
|
+
function withNullable(schema) {
|
|
207
|
+
if (!isSetObject$1(schema)) return { anyOf: [schema, {
|
|
208
|
+
type: "null",
|
|
209
|
+
default: null
|
|
210
|
+
}] };
|
|
211
|
+
const rawType = schema.type;
|
|
212
|
+
const types = Array.isArray(rawType) ? [...rawType] : rawType ? [rawType] : [];
|
|
213
|
+
if (!types.includes("null")) types.push("null");
|
|
214
|
+
return {
|
|
215
|
+
...schema,
|
|
216
|
+
type: types.length === 1 ? types[0] : types
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Converts a Deepkit type reflection into a JSON Schema (draft-07) fragment.
|
|
221
|
+
*/
|
|
222
|
+
function reflectionToJsonSchema(reflection) {
|
|
223
|
+
return reflectionToJsonSchemaInner(reflection);
|
|
224
|
+
}
|
|
225
|
+
function reflectionToJsonSchemaInner(reflection) {
|
|
226
|
+
switch (reflection.kind) {
|
|
227
|
+
case ReflectionKind.any:
|
|
228
|
+
case ReflectionKind.unknown:
|
|
229
|
+
case ReflectionKind.void:
|
|
230
|
+
case ReflectionKind.object: return withReflectionTags(reflection, { name: reflection.typeName });
|
|
231
|
+
case ReflectionKind.never: return;
|
|
232
|
+
case ReflectionKind.undefined:
|
|
233
|
+
case ReflectionKind.null: return withReflectionTags(reflection, {
|
|
234
|
+
type: "null",
|
|
235
|
+
name: reflection.typeName,
|
|
236
|
+
default: null
|
|
237
|
+
});
|
|
238
|
+
case ReflectionKind.string: return withReflectionTags(reflection, {
|
|
239
|
+
type: "string",
|
|
240
|
+
name: reflection.typeName
|
|
241
|
+
});
|
|
242
|
+
case ReflectionKind.boolean: return withReflectionTags(reflection, {
|
|
243
|
+
type: "boolean",
|
|
244
|
+
name: reflection.typeName
|
|
245
|
+
});
|
|
246
|
+
case ReflectionKind.number: return withReflectionTags(reflection, numberBrandToJsonSchema(reflection.brand));
|
|
247
|
+
case ReflectionKind.bigint: return withReflectionTags(reflection, {
|
|
248
|
+
type: "integer",
|
|
249
|
+
name: reflection.typeName,
|
|
250
|
+
format: "int64"
|
|
251
|
+
});
|
|
252
|
+
case ReflectionKind.regexp: return withReflectionTags(reflection, {
|
|
253
|
+
type: "string",
|
|
254
|
+
name: reflection.typeName,
|
|
255
|
+
format: "regex",
|
|
256
|
+
contentMediaType: "text/regex"
|
|
257
|
+
});
|
|
258
|
+
case ReflectionKind.literal: {
|
|
259
|
+
const { literal } = reflection;
|
|
260
|
+
if (isBigInt(literal)) return withReflectionTags(reflection, {
|
|
261
|
+
type: "integer",
|
|
262
|
+
name: reflection.typeName,
|
|
263
|
+
format: "int64",
|
|
264
|
+
const: literal
|
|
265
|
+
});
|
|
266
|
+
if (isRegExp(literal)) return withReflectionTags(reflection, {
|
|
267
|
+
type: "string",
|
|
268
|
+
name: reflection.typeName,
|
|
269
|
+
format: "regex",
|
|
270
|
+
const: literal.source
|
|
271
|
+
});
|
|
272
|
+
return withReflectionTags(reflection, {
|
|
273
|
+
type: getJsonSchemaType(literal),
|
|
274
|
+
name: reflection.typeName,
|
|
275
|
+
const: literal
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
case ReflectionKind.templateLiteral: return withReflectionTags(reflection, { type: "string" });
|
|
279
|
+
case ReflectionKind.enum: {
|
|
280
|
+
const values = reflection.values.filter((value) => isString(value) || isInteger(value) || isBigInt(value) || isNumber(value) || isBoolean(value) || isNull(value));
|
|
281
|
+
if (values.length === 0) return withReflectionTags(reflection, {
|
|
282
|
+
name: reflection.typeName,
|
|
283
|
+
description: reflection.description,
|
|
284
|
+
enum: []
|
|
285
|
+
});
|
|
286
|
+
return withReflectionTags(reflection, {
|
|
287
|
+
type: values.every((value) => isString(value)) ? "string" : values.every((value) => isInteger(value) || isBigInt(value)) ? "integer" : values.every((value) => isNumber(value)) ? "number" : values.every((value) => isBoolean(value)) ? "boolean" : values.every((value) => isNull(value)) ? "null" : values.reduce((ret, value) => {
|
|
288
|
+
const type = getJsonSchemaType(value);
|
|
289
|
+
if (isJsonSchemaPrimitiveType(type) && !ret.includes(type)) ret.push(type);
|
|
290
|
+
return ret;
|
|
291
|
+
}, []),
|
|
292
|
+
name: reflection.typeName,
|
|
293
|
+
description: reflection.description,
|
|
294
|
+
enum: values,
|
|
295
|
+
default: values.length === 1 ? values[0] : void 0
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
case ReflectionKind.array: {
|
|
299
|
+
const items = reflectionToJsonSchemaInner(reflection.type);
|
|
300
|
+
return withReflectionTags(reflection, {
|
|
301
|
+
type: "array",
|
|
302
|
+
name: reflection.typeName,
|
|
303
|
+
items: items ?? {}
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
case ReflectionKind.tuple: {
|
|
307
|
+
const items = reflection.types.map((member) => reflectionToJsonSchemaInner(member.type)).filter((item) => item !== void 0);
|
|
308
|
+
if (items.length <= 1) return withReflectionTags(reflection, {
|
|
309
|
+
type: "array",
|
|
310
|
+
name: reflection.typeName,
|
|
311
|
+
items: items.length === 1 ? items[0] : {}
|
|
312
|
+
});
|
|
313
|
+
return withReflectionTags(reflection, {
|
|
314
|
+
type: "array",
|
|
315
|
+
name: reflection.typeName,
|
|
316
|
+
prefixItems: items,
|
|
317
|
+
minItems: items.length,
|
|
318
|
+
maxItems: items.length
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
case ReflectionKind.union: {
|
|
322
|
+
const branches = reflection.types.map((inner) => reflectionToJsonSchemaInner(inner)).filter((branch) => branch !== void 0);
|
|
323
|
+
if (!reflection.types.some((inner) => inner.kind === ReflectionKind.null || inner.kind === ReflectionKind.undefined)) return withReflectionTags(reflection, {
|
|
324
|
+
name: reflection.typeName,
|
|
325
|
+
anyOf: branches
|
|
326
|
+
});
|
|
327
|
+
const nonNull = branches.filter((branch) => !isNullOnlyJsonSchema(branch));
|
|
328
|
+
if (nonNull.length === 0) return withReflectionTags(reflection, {
|
|
329
|
+
type: "null",
|
|
330
|
+
default: null
|
|
331
|
+
});
|
|
332
|
+
if (nonNull.length === 1) {
|
|
333
|
+
const first = nonNull[0];
|
|
334
|
+
if (!isSetObject$1(first)) return withNullable(withReflectionTags(reflection, {
|
|
335
|
+
name: reflection.typeName,
|
|
336
|
+
anyOf: [first]
|
|
337
|
+
}));
|
|
338
|
+
return withNullable(withReflectionTags(reflection, {
|
|
339
|
+
name: reflection.typeName,
|
|
340
|
+
...first
|
|
341
|
+
}));
|
|
342
|
+
}
|
|
343
|
+
const enumValues = nonNull.map((branch) => isSetObject$1(branch) ? branch.const : void 0).filter((value) => value === null || typeof value === "string" || typeof value === "number" || typeof value === "bigint" || typeof value === "boolean");
|
|
344
|
+
if (enumValues.length === nonNull.length) return withNullable(withReflectionTags(reflection, {
|
|
345
|
+
name: reflection.typeName,
|
|
346
|
+
enum: enumValues
|
|
347
|
+
}));
|
|
348
|
+
const discriminator = tryReflectionDiscriminator(reflection.types);
|
|
349
|
+
if (discriminator && isSetObject$1(discriminator)) return withNullable(withReflectionTags(reflection, {
|
|
350
|
+
name: reflection.typeName,
|
|
351
|
+
...discriminator
|
|
352
|
+
}));
|
|
353
|
+
return withNullable(withReflectionTags(reflection, {
|
|
354
|
+
name: reflection.typeName,
|
|
355
|
+
anyOf: nonNull
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
case ReflectionKind.intersection: {
|
|
359
|
+
const members = reflection.types.map((inner) => reflectionToJsonSchemaInner(inner)).filter((item) => item !== void 0);
|
|
360
|
+
if (members.length === 0) return;
|
|
361
|
+
if (members.length === 1) {
|
|
362
|
+
if (!isSetObject$1(members[0])) return members[0];
|
|
363
|
+
return withReflectionTags(reflection, {
|
|
364
|
+
name: reflection.typeName,
|
|
365
|
+
...members[0]
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
if (members.every(isJsonSchemaObject)) return withReflectionTags(reflection, {
|
|
369
|
+
name: reflection.typeName,
|
|
370
|
+
...mergeObjectSchemas(members)
|
|
371
|
+
});
|
|
372
|
+
return withReflectionTags(reflection, {
|
|
373
|
+
name: reflection.typeName,
|
|
374
|
+
allOf: members
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
case ReflectionKind.promise: return reflectionToJsonSchemaInner(reflection.type);
|
|
378
|
+
case ReflectionKind.objectLiteral: return objectReflectionToJsonSchema(reflection);
|
|
379
|
+
case ReflectionKind.class: switch (reflection.classType?.name) {
|
|
380
|
+
case "Date": return withReflectionTags(reflection, {
|
|
381
|
+
type: "string",
|
|
382
|
+
format: "date-time"
|
|
383
|
+
});
|
|
384
|
+
case "RegExp": return withReflectionTags(reflection, {
|
|
385
|
+
type: "string",
|
|
386
|
+
format: "regex"
|
|
387
|
+
});
|
|
388
|
+
case "URL": return withReflectionTags(reflection, {
|
|
389
|
+
type: "string",
|
|
390
|
+
format: "uri"
|
|
391
|
+
});
|
|
392
|
+
case "Set": {
|
|
393
|
+
const itemType = reflection.arguments?.[0];
|
|
394
|
+
return withReflectionTags(reflection, {
|
|
395
|
+
type: "array",
|
|
396
|
+
items: (itemType ? reflectionToJsonSchemaInner(itemType) : void 0) ?? {},
|
|
397
|
+
uniqueItems: true
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
case "Map": {
|
|
401
|
+
const valueType = reflection.arguments?.[1];
|
|
402
|
+
return withReflectionTags(reflection, {
|
|
403
|
+
type: "object",
|
|
404
|
+
additionalProperties: (valueType ? reflectionToJsonSchemaInner(valueType) : void 0) ?? true
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
case "Uint8Array":
|
|
408
|
+
case "Uint8ClampedArray":
|
|
409
|
+
case "Uint16Array":
|
|
410
|
+
case "Uint32Array":
|
|
411
|
+
case "Int8Array":
|
|
412
|
+
case "Int16Array":
|
|
413
|
+
case "Int32Array":
|
|
414
|
+
case "Float32Array":
|
|
415
|
+
case "Float64Array":
|
|
416
|
+
case "BigInt64Array":
|
|
417
|
+
case "BigUint64Array": return withReflectionTags(reflection, {
|
|
418
|
+
type: "string",
|
|
419
|
+
format: "byte",
|
|
420
|
+
contentEncoding: "base64"
|
|
421
|
+
});
|
|
422
|
+
case void 0:
|
|
423
|
+
default: return withReflectionTags(reflection, {
|
|
424
|
+
name: reflection.typeName,
|
|
425
|
+
description: reflection.description,
|
|
426
|
+
...objectReflectionToJsonSchema(reflection)
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
case ReflectionKind.symbol:
|
|
430
|
+
case ReflectionKind.property:
|
|
431
|
+
case ReflectionKind.method:
|
|
432
|
+
case ReflectionKind.function:
|
|
433
|
+
case ReflectionKind.parameter:
|
|
434
|
+
case ReflectionKind.typeParameter:
|
|
435
|
+
case ReflectionKind.tupleMember:
|
|
436
|
+
case ReflectionKind.enumMember:
|
|
437
|
+
case ReflectionKind.rest:
|
|
438
|
+
case ReflectionKind.indexSignature:
|
|
439
|
+
case ReflectionKind.propertySignature:
|
|
440
|
+
case ReflectionKind.methodSignature:
|
|
441
|
+
case ReflectionKind.infer:
|
|
442
|
+
case ReflectionKind.callSignature:
|
|
443
|
+
default: return;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
function mergeObjectSchemas(schemas) {
|
|
447
|
+
const merged = {
|
|
448
|
+
type: "object",
|
|
449
|
+
properties: {},
|
|
450
|
+
required: []
|
|
451
|
+
};
|
|
452
|
+
for (const schema of schemas) {
|
|
453
|
+
if (schema.properties) merged.properties = defu(merged.properties, schema.properties);
|
|
454
|
+
if (schema.required) merged.required = Array.from(/* @__PURE__ */ new Set([...merged.required ?? [], ...schema.required]));
|
|
455
|
+
if (schema.additionalProperties !== void 0) merged.additionalProperties = schema.additionalProperties;
|
|
456
|
+
}
|
|
457
|
+
if ((merged.required?.length ?? 0) === 0) delete merged.required;
|
|
458
|
+
return merged;
|
|
459
|
+
}
|
|
460
|
+
function tryReflectionDiscriminator(types) {
|
|
461
|
+
const nonNullTypes = types.filter((t) => t.kind !== ReflectionKind.null && t.kind !== ReflectionKind.undefined);
|
|
462
|
+
const objectBranches = nonNullTypes.filter((t) => t.kind === ReflectionKind.objectLiteral || t.kind === ReflectionKind.class);
|
|
463
|
+
if (objectBranches.length < 2 || objectBranches.length !== nonNullTypes.length) return;
|
|
464
|
+
let tagKey;
|
|
465
|
+
const branches = [];
|
|
466
|
+
for (const branch of objectBranches) {
|
|
467
|
+
const literalProps = [];
|
|
468
|
+
for (const member of branch.types) if ((member.kind === ReflectionKind.property || member.kind === ReflectionKind.propertySignature) && typeof member.name === "string" && member.type.kind === ReflectionKind.literal && typeof member.type.literal === "string") literalProps.push({
|
|
469
|
+
name: member.name,
|
|
470
|
+
literal: member.type.literal
|
|
471
|
+
});
|
|
472
|
+
if (literalProps.length === 0) return;
|
|
473
|
+
const first = literalProps[0];
|
|
474
|
+
if (!tagKey) tagKey = first.name;
|
|
475
|
+
else if (tagKey !== first.name) return;
|
|
476
|
+
const body = objectReflectionToJsonSchema({
|
|
477
|
+
...branch,
|
|
478
|
+
types: branch.types.filter((member) => !((member.kind === ReflectionKind.property || member.kind === ReflectionKind.propertySignature) && member.name === tagKey))
|
|
479
|
+
});
|
|
480
|
+
if (!body || !isJsonSchemaObject(body)) return;
|
|
481
|
+
branches.push({
|
|
482
|
+
type: "object",
|
|
483
|
+
properties: {
|
|
484
|
+
[tagKey]: { const: first.literal },
|
|
485
|
+
...body.properties ?? {}
|
|
486
|
+
},
|
|
487
|
+
required: [tagKey, ...body.required ?? []],
|
|
488
|
+
additionalProperties: body.additionalProperties ?? false
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
if (!tagKey) return;
|
|
492
|
+
return {
|
|
493
|
+
oneOf: branches,
|
|
494
|
+
discriminator: { propertyName: tagKey }
|
|
495
|
+
};
|
|
496
|
+
}
|
|
497
|
+
function objectReflectionToJsonSchema(type) {
|
|
498
|
+
const reflection = ReflectionClass.from(type);
|
|
499
|
+
const schema = {
|
|
500
|
+
type: "object",
|
|
501
|
+
name: reflection.getName(),
|
|
502
|
+
description: reflection.getDescription(),
|
|
503
|
+
properties: {},
|
|
504
|
+
required: [],
|
|
505
|
+
primaryKey: reflection.getPrimaries().map((primary) => primary.getNameAsString()),
|
|
506
|
+
...isSetString$1(reflection.databaseSchemaName) ? { databaseSchemaName: reflection.databaseSchemaName } : {},
|
|
507
|
+
...isSetString$1(reflection.getName()) ? { name: reflection.getName() } : {},
|
|
508
|
+
...isSetString$1(reflection.getDescription()) ? { description: reflection.getDescription() } : {}
|
|
509
|
+
};
|
|
510
|
+
for (const propertyReflection of reflection.getProperties()) {
|
|
511
|
+
if (propertyReflection.getKind() === ReflectionKind.indexSignature) {
|
|
512
|
+
schema.additionalProperties = reflectionToJsonSchemaInner(propertyReflection.type) ?? true;
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
let property = reflectionToJsonSchemaInner(propertyReflection.type);
|
|
516
|
+
if (!property) continue;
|
|
517
|
+
const propertySchema = isSetObject$1(property) ? property : {};
|
|
518
|
+
const groups = propertyReflection.getGroups();
|
|
519
|
+
property = {
|
|
520
|
+
...propertySchema,
|
|
521
|
+
name: propertyReflection.getNameAsString(),
|
|
522
|
+
description: propertyReflection.getDescription(),
|
|
523
|
+
readOnly: propertyReflection.isReadonly(),
|
|
524
|
+
...propertyReflection.hasDefault() ? { default: propertyReflection.getDefaultValue() } : {},
|
|
525
|
+
...isSetArray(groups) ? { tags: groups } : {}
|
|
526
|
+
};
|
|
527
|
+
if (propertyReflection.isNullable()) property = withNullable(property);
|
|
528
|
+
schema.properties ??= {};
|
|
529
|
+
schema.properties[propertyReflection.name] = property;
|
|
530
|
+
if (!propertyReflection.isOptional()) {
|
|
531
|
+
schema.required ??= [];
|
|
532
|
+
schema.required.push(propertyReflection.name);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
return schema;
|
|
536
|
+
}
|
|
537
|
+
|
|
127
538
|
//#endregion
|
|
128
539
|
//#region src/extract.ts
|
|
129
540
|
const SCHEMA_BUNDLE_BASE_URI = "https://power-plant.invalid/";
|
|
@@ -329,6 +740,10 @@ function extractHash(variant, input) {
|
|
|
329
740
|
variant,
|
|
330
741
|
input: unwrappedConfig._def
|
|
331
742
|
});
|
|
743
|
+
else if (isType(unwrappedConfig)) return murmurhash({
|
|
744
|
+
variant,
|
|
745
|
+
input: stringifyType(unwrappedConfig)
|
|
746
|
+
});
|
|
332
747
|
else if (isStandardJsonSchema(unwrappedConfig)) return murmurhash({
|
|
333
748
|
variant,
|
|
334
749
|
input: unwrappedConfig["~standard"]
|
|
@@ -353,6 +768,13 @@ function extractHash(variant, input) {
|
|
|
353
768
|
throw new Error(`Failed to create an input hash for the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a Valibot BaseSchema, or a reflected Deepkit Type object.`);
|
|
354
769
|
}
|
|
355
770
|
/**
|
|
771
|
+
* Converts a reflected Deepkit {@link Type} into a JSON Schema (draft-2020-12) representation.
|
|
772
|
+
*/
|
|
773
|
+
function extractReflection(reflection) {
|
|
774
|
+
if (!isType(reflection)) return;
|
|
775
|
+
return reflectionToJsonSchema(reflection);
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
356
778
|
* Extracts a JSON Schema from Zod, Standard Schema, Valibot, untyped, or JSON Schema inputs.
|
|
357
779
|
*
|
|
358
780
|
* @param schema - The schema input to extract a JSON Schema from.
|
|
@@ -378,6 +800,7 @@ function extractJsonSchema(schema) {
|
|
|
378
800
|
function extractResolvedVariant(input) {
|
|
379
801
|
if (isSetObject(input)) {
|
|
380
802
|
if (isZod3Type(input)) return "zod3";
|
|
803
|
+
else if (isType(input)) return "reflection";
|
|
381
804
|
else if (isUntypedConfigStrict(input) || isUntypedSchemaStrict(input)) return "untyped";
|
|
382
805
|
else if (isStandardJsonSchema(input)) return "standard-schema";
|
|
383
806
|
else if (isJsonSchema(input)) return "json-schema";
|
|
@@ -410,6 +833,7 @@ async function extractSchema(input, variant) {
|
|
|
410
833
|
const resolvedVariant = variant ?? extractResolvedVariant(input);
|
|
411
834
|
let schema;
|
|
412
835
|
if (resolvedVariant === "zod3" || resolvedVariant === "json-schema" || resolvedVariant === "standard-schema" || resolvedVariant === "untyped" || resolvedVariant === "valibot") schema = extractJsonSchema(input);
|
|
836
|
+
else if (resolvedVariant === "reflection") schema = extractReflection(input);
|
|
413
837
|
if (schema) return bundleReferences(schema);
|
|
414
838
|
throw new Error(`Failed to extract a valid schema from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a Valibot BaseSchema, an untyped schema, or a reflected Deepkit Type object.`);
|
|
415
839
|
}
|
|
@@ -447,123 +871,166 @@ function extractSource(variant, input) {
|
|
|
447
871
|
variant: "valibot",
|
|
448
872
|
schema: input
|
|
449
873
|
};
|
|
874
|
+
else if (variant === "reflection") return {
|
|
875
|
+
hash: extractHash(variant, input),
|
|
876
|
+
variant: "reflection",
|
|
877
|
+
schema: input
|
|
878
|
+
};
|
|
450
879
|
throw new Error(`Failed to extract source information from the provided input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, an untyped schema, or a reflected Deepkit Type object.`);
|
|
451
880
|
}
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
881
|
+
const deepkitCache = new Cache();
|
|
882
|
+
function rewriteTypeOnlyImports(source) {
|
|
883
|
+
return source.replaceAll(/\bimport\s+type\s+/g, "import ").replaceAll(/\bexport\s+type\s+\*\s+from/g, "export * from").replaceAll(/\bexport\s+type\s+\{/g, "export {");
|
|
884
|
+
}
|
|
885
|
+
function resolveReflectionConfig(options) {
|
|
886
|
+
return {
|
|
887
|
+
reflection: options.reflection ?? "default",
|
|
888
|
+
exclude: options.exclude
|
|
889
|
+
};
|
|
890
|
+
}
|
|
891
|
+
function getCompilerOptions(options) {
|
|
892
|
+
const cwd = options.cwd || process.cwd();
|
|
893
|
+
const tsconfigPath = options.tsconfig ? appendPath(options.tsconfig, cwd) : joinPaths(cwd, "tsconfig.json");
|
|
894
|
+
try {
|
|
895
|
+
const raw = ts.sys.readFile(tsconfigPath);
|
|
896
|
+
if (raw) {
|
|
897
|
+
const parsed = ts.parseConfigFileTextToJson(tsconfigPath, raw);
|
|
898
|
+
if (parsed.config) return {
|
|
899
|
+
...ts.parseJsonConfigFileContent(parsed.config, ts.sys, findFilePath(tsconfigPath) || cwd, {}, tsconfigPath).options,
|
|
900
|
+
noEmit: true,
|
|
901
|
+
experimentalDecorators: true,
|
|
902
|
+
emitDecoratorMetadata: true
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
} catch {}
|
|
469
906
|
return {
|
|
470
|
-
noEmit: true,
|
|
471
|
-
emitDecoratorMetadata: true,
|
|
472
|
-
experimentalDecorators: true,
|
|
473
907
|
target: ts.ScriptTarget.ES2022,
|
|
474
908
|
module: ts.ModuleKind.ESNext,
|
|
475
909
|
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
476
|
-
strictNullChecks:
|
|
910
|
+
strictNullChecks: true,
|
|
911
|
+
experimentalDecorators: true,
|
|
912
|
+
emitDecoratorMetadata: true,
|
|
477
913
|
skipLibCheck: true,
|
|
478
|
-
skipDefaultLibCheck: true,
|
|
479
914
|
esModuleInterop: true,
|
|
480
|
-
|
|
915
|
+
noEmit: true
|
|
481
916
|
};
|
|
482
917
|
}
|
|
483
|
-
function
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
918
|
+
function transpileWithDeepkit(code, fileName, options) {
|
|
919
|
+
const reflectionConfig = resolveReflectionConfig(options);
|
|
920
|
+
deepkitCache.tick();
|
|
921
|
+
return ts.transpileModule(code, {
|
|
922
|
+
compilerOptions: getCompilerOptions(options),
|
|
923
|
+
fileName,
|
|
924
|
+
transformers: {
|
|
925
|
+
before: [(context) => new ReflectionTransformer(context, deepkitCache).withReflection(reflectionConfig)],
|
|
926
|
+
after: [(context) => new DeclarationTransformer(context, deepkitCache).withReflection(reflectionConfig)]
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
async function readSourceFile(path, fs) {
|
|
931
|
+
try {
|
|
932
|
+
if (fs?.promises?.readFile) return await fs.promises.readFile(path, "utf8");
|
|
933
|
+
return await readFile(path, "utf8");
|
|
934
|
+
} catch {
|
|
935
|
+
return;
|
|
491
936
|
}
|
|
492
937
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
938
|
+
function createDeepkitPlugin(options) {
|
|
939
|
+
return {
|
|
940
|
+
name: "power-plant:deepkit",
|
|
941
|
+
setup(pluginBuild) {
|
|
942
|
+
pluginBuild.onLoad({ filter: /\.(m|c)?tsx?$/ }, async (args) => {
|
|
943
|
+
if (args.pluginData?.isReflected) {
|
|
944
|
+
const contents = await readSourceFile(args.path, options.fs);
|
|
945
|
+
if (!contents) return null;
|
|
946
|
+
return {
|
|
947
|
+
contents,
|
|
948
|
+
loader: "ts",
|
|
949
|
+
pluginData: { isReflected: true }
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
const raw = await readSourceFile(args.path, options.fs);
|
|
953
|
+
if (!raw) return null;
|
|
954
|
+
const result = transpileWithDeepkit(rewriteTypeOnlyImports(raw), args.path, options);
|
|
955
|
+
if (result.diagnostics?.length) {
|
|
956
|
+
const errors = result.diagnostics.filter((diagnostic) => diagnostic.category === DiagnosticCategory.Error);
|
|
957
|
+
if (errors.length > 0) {
|
|
958
|
+
const errorMessage = `Deepkit Type reflection transpilation errors: ${args.path} \n ${errors.map((diagnostic) => `-${diagnostic.file ? `${diagnostic.file.fileName}:` : ""} ${typeof diagnostic.messageText === "string" ? diagnostic.messageText : diagnostic.messageText.messageText} (at ${diagnostic.start}:${diagnostic.length})`).join("\n")}`;
|
|
959
|
+
options.logger?.error?.(errorMessage);
|
|
960
|
+
throw new Error(errorMessage);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
return {
|
|
964
|
+
contents: result.outputText,
|
|
965
|
+
loader: "ts",
|
|
966
|
+
pluginData: { isReflected: true }
|
|
967
|
+
};
|
|
968
|
+
});
|
|
509
969
|
}
|
|
510
|
-
return baseGetSourceFile(name, languageVersionOrOptions, onError, shouldCreateNewSourceFile);
|
|
511
970
|
};
|
|
512
|
-
const program = ts.createProgram([filePath], compilerOptions, host);
|
|
513
|
-
if (!config.skipTypeCheck) {
|
|
514
|
-
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
515
|
-
if (diagnostics.length) throw new Error(`Type check error: ${diagnostics.map((diagnostic) => diagnostic.messageText.toString()).join("\n")}`);
|
|
516
|
-
}
|
|
517
|
-
return program;
|
|
518
971
|
}
|
|
519
972
|
/**
|
|
520
|
-
* Resolves a type definition to a JSON Schema.
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
* [ts-json-schema-generator](https://github.com/vega/ts-json-schema-generator)
|
|
524
|
-
* using the referenced export name as the target type when one is provided.
|
|
973
|
+
* Resolves a type definition to a JSON Schema. Bundles the TypeScript module
|
|
974
|
+
* graph with esbuild using `@deepkit/type-compiler` reflection transformers,
|
|
975
|
+
* then converts the reflected Deepkit {@link Type} via {@link reflectionToJsonSchema}.
|
|
525
976
|
*
|
|
526
977
|
* @param input - The type definition to compile. This can be either a string or a {@link FileReference} object.
|
|
527
|
-
* @param options - Optional overrides
|
|
978
|
+
* @param options - Optional overrides for reflection and file resolution.
|
|
528
979
|
* @returns A promise that resolves to the generated JSON Schema.
|
|
980
|
+
* @see https://deepkit.io/en/documentation/runtime-types/reflection
|
|
529
981
|
*/
|
|
530
982
|
async function extractTSType(input, options = {}) {
|
|
531
983
|
const fileReference = extractFileReference(input);
|
|
532
984
|
if (!fileReference) throw new Error(`Failed to extract a file reference from the provided input. The input must be a string or an object with a "file" property that specifies the file path and optional export name.`);
|
|
533
|
-
const exportName = fileReference.export ?? "
|
|
985
|
+
const exportName = fileReference.export ?? "default";
|
|
534
986
|
const resolvedPath = await resolveSafe(fileReference.file, { fs: options.fs });
|
|
535
987
|
const filePath = resolvedPath || fileReference.file;
|
|
988
|
+
const cwd = options.cwd || process.cwd();
|
|
536
989
|
try {
|
|
537
|
-
const tsconfig = options.tsconfig ? appendPath(options.tsconfig, options.cwd || process.cwd()) : joinPaths(options.cwd || process.cwd(), "tsconfig.json");
|
|
538
|
-
const bundled = await resolve(filePath, {
|
|
539
|
-
cwd: options.cwd || process.cwd(),
|
|
540
|
-
fs: options.fs
|
|
541
|
-
});
|
|
542
990
|
options.logger?.debug?.(`Generating JSON schema for bundled "${filePath}" using the type "${exportName}"`);
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
991
|
+
const result = await build({
|
|
992
|
+
platform: "node",
|
|
993
|
+
format: "esm",
|
|
994
|
+
logLevel: "silent",
|
|
995
|
+
entryPoints: [filePath],
|
|
996
|
+
write: false,
|
|
997
|
+
sourcemap: false,
|
|
998
|
+
splitting: false,
|
|
999
|
+
treeShaking: true,
|
|
1000
|
+
bundle: true,
|
|
1001
|
+
packages: "bundle",
|
|
1002
|
+
keepNames: true,
|
|
1003
|
+
metafile: false,
|
|
1004
|
+
absWorkingDir: cwd,
|
|
1005
|
+
plugins: [createDeepkitPlugin(options)]
|
|
1006
|
+
});
|
|
1007
|
+
if (result.errors.length > 0) throw new Error(result.errors.map((error) => error.text).join(", "));
|
|
1008
|
+
const bundled = result.outputFiles?.filter(Boolean)[0]?.text;
|
|
1009
|
+
if (!isSetString$1(bundled)) throw new Error(`No output files generated for "${filePath}". Please check the configuration and try again.`);
|
|
1010
|
+
const evaluated = await createJiti(cwd).evalModule(bundled, {
|
|
1011
|
+
filename: filePath,
|
|
1012
|
+
ext: findFileDotExtensionSafe(filePath) || ".ts"
|
|
1013
|
+
});
|
|
1014
|
+
let resolved = evaluated[exportName] ?? evaluated[`__Ω${exportName}`];
|
|
1015
|
+
if (resolved === void 0) throw new Error(`The export "${exportName}" could not be resolved in the "${filePath}" module. ${Object.keys(evaluated).length === 0 ? `After bundling, no exports were found in the module.` : `After bundling, the available exports were: ${Object.keys(evaluated).join(", ")}.`}`);
|
|
1016
|
+
try {
|
|
1017
|
+
const type = reflect(resolved);
|
|
1018
|
+
if (isType(type)) resolved = type;
|
|
1019
|
+
} catch {}
|
|
1020
|
+
if (isType(resolved)) {
|
|
1021
|
+
const schema = extractReflection(resolved);
|
|
1022
|
+
if (!schema) throw new Error(`Failed to convert the reflected Deepkit type for "${exportName}" to JSON Schema.`);
|
|
1023
|
+
return schema;
|
|
1024
|
+
}
|
|
1025
|
+
const schema = extractJsonSchema(resolved);
|
|
1026
|
+
if (!schema) throw new Error(`The export "${exportName}" could not be converted to a JSON Schema.`);
|
|
1027
|
+
return schema;
|
|
561
1028
|
} catch (error) {
|
|
562
1029
|
throw new Error(`Failed to generate a JSON schema for "${fileReference.file}"${resolvedPath && resolvedPath !== fileReference.file ? ` (resolved: ${resolvedPath})` : ""} using the type "${exportName}". Error: ${error.message}`);
|
|
563
1030
|
}
|
|
564
1031
|
}
|
|
565
1032
|
/**
|
|
566
|
-
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [
|
|
1033
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, a Deepkit Type object, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](https://esbuild.github.io) using [@deepkit/type-compiler](https://deepkit.io/en/documentation/runtime-types/getting-started) reflection to obtain the actual schema definition before extraction.
|
|
567
1034
|
*
|
|
568
1035
|
* @example
|
|
569
1036
|
* ```ts
|
|
@@ -577,8 +1044,8 @@ async function extractTSType(input, options = {}) {
|
|
|
577
1044
|
* const schema4 = await extract(context, zodSchema);
|
|
578
1045
|
* // Resolve a schema definition from a Valibot schema
|
|
579
1046
|
* const schema5 = await extract(context, valibotSchema);
|
|
580
|
-
* // Resolve a schema definition from
|
|
581
|
-
* const schema6 = await extract(context,
|
|
1047
|
+
* // Resolve a schema definition from a reflected Deepkit Type object
|
|
1048
|
+
* const schema6 = await extract(context, reflectionType);
|
|
582
1049
|
* ```
|
|
583
1050
|
*
|
|
584
1051
|
* @see https://zod.dev/
|
|
@@ -586,7 +1053,7 @@ async function extractTSType(input, options = {}) {
|
|
|
586
1053
|
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
587
1054
|
* @see https://json-schema.org/
|
|
588
1055
|
* @see https://ajv.js.org/json-type-definition.html
|
|
589
|
-
* @see https://
|
|
1056
|
+
* @see https://deepkit.io/en/documentation/runtime-types/reflection
|
|
590
1057
|
* @see https://github.com/unjs/untyped
|
|
591
1058
|
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
592
1059
|
*
|
|
@@ -617,13 +1084,22 @@ async function extractSchemaWithSource(input, options = {}) {
|
|
|
617
1084
|
let fs;
|
|
618
1085
|
if (options.storage) fs = mapStorageToFileSystem(options.storage);
|
|
619
1086
|
let resolved = await loadSafe(unwrappedConfig, {
|
|
620
|
-
...options,
|
|
621
|
-
|
|
1087
|
+
...omit(options, [
|
|
1088
|
+
"storage",
|
|
1089
|
+
"logger",
|
|
1090
|
+
"tsconfig"
|
|
1091
|
+
]),
|
|
1092
|
+
fs,
|
|
1093
|
+
cwd: options.cwd ?? void 0
|
|
622
1094
|
});
|
|
623
1095
|
resolved ??= await extractTSType(unwrappedConfig, {
|
|
624
1096
|
...options,
|
|
625
1097
|
fs
|
|
626
1098
|
});
|
|
1099
|
+
try {
|
|
1100
|
+
const type = reflect(resolved);
|
|
1101
|
+
if (isType(type)) resolved = type;
|
|
1102
|
+
} catch {}
|
|
627
1103
|
const resolvedConfig = unwrapSchemaConfig(resolved);
|
|
628
1104
|
if (isSchemaWithSource(resolvedConfig)) source = resolvedConfig.source;
|
|
629
1105
|
else if (isSchema(resolvedConfig)) source = {
|
|
@@ -649,7 +1125,7 @@ async function extractSchemaWithSource(input, options = {}) {
|
|
|
649
1125
|
};
|
|
650
1126
|
}
|
|
651
1127
|
/**
|
|
652
|
-
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](esbuild.github.io) using [
|
|
1128
|
+
* Extracts a JSON Schema from a given schema definition input, which can be a Zod schema, a Valibot schema, any Standard JSON Schema type, a plain JSON Schema object, an untyped schema, a Deepkit Type object, or a {@link FileReferenceInput} to an exported TypeScript type definition or any of the previous options. If the input is a {@link FileReferenceInput} (e.g. a file path with an export), the source code will be bundled with [esbuild](https://esbuild.github.io) using [@deepkit/type-compiler](https://deepkit.io/en/documentation/runtime-types/getting-started) reflection to obtain the actual schema definition before extraction.
|
|
653
1129
|
*
|
|
654
1130
|
* @example
|
|
655
1131
|
* ```ts
|
|
@@ -663,8 +1139,8 @@ async function extractSchemaWithSource(input, options = {}) {
|
|
|
663
1139
|
* const schema4 = await extract(context, zodSchema);
|
|
664
1140
|
* // Resolve a schema definition from a Valibot schema
|
|
665
1141
|
* const schema5 = await extract(context, valibotSchema);
|
|
666
|
-
* // Resolve a schema definition from
|
|
667
|
-
* const schema6 = await extract(context,
|
|
1142
|
+
* // Resolve a schema definition from a reflected Deepkit Type object
|
|
1143
|
+
* const schema6 = await extract(context, reflectionType);
|
|
668
1144
|
* ```
|
|
669
1145
|
*
|
|
670
1146
|
* @see https://zod.dev/
|
|
@@ -672,7 +1148,7 @@ async function extractSchemaWithSource(input, options = {}) {
|
|
|
672
1148
|
* @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
|
|
673
1149
|
* @see https://json-schema.org/
|
|
674
1150
|
* @see https://ajv.js.org/json-type-definition.html
|
|
675
|
-
* @see https://
|
|
1151
|
+
* @see https://deepkit.io/en/documentation/runtime-types/reflection
|
|
676
1152
|
* @see https://github.com/unjs/untyped
|
|
677
1153
|
* @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
|
|
678
1154
|
*
|
|
@@ -690,5 +1166,5 @@ async function extract(input, options = {}) {
|
|
|
690
1166
|
}
|
|
691
1167
|
|
|
692
1168
|
//#endregion
|
|
693
|
-
export { JSON_SCHEMA_CONSTRUCTOR_KEY, JSON_SCHEMA_METADATA_KEYS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_TYPES, JsonSchemaTypeNames, SCHEMA_ARRAY_CONCAT_KEYWORDS, SCHEMA_RECORD_KEYWORDS, SCHEMA_SINGLE_KEYWORDS, addProperty, applyJsonSchemaMetadata, assertSchemasDoNotContradict, bundleReferences, createStoragePromises, extract, extractHash, extractJsonSchema, extractResolvedVariant, extractSchema, extractSchemaWithSource, extractSource, extractTSType, extractVariant, findSchemaContradictions, getJsonSchema, getJsonSchemaObject, getPrimarySchemaType, getProperties, getPropertiesList, getProperty, hasJsonSchemaConstructorFlag, isFileReference, isJsonSchema, isJsonSchemaAllOf, isJsonSchemaAny, isJsonSchemaAnyOf, isJsonSchemaArray, isJsonSchemaBigint, isJsonSchemaBoolean, isJsonSchemaDate, isJsonSchemaDecimal, isJsonSchemaEnum, isJsonSchemaInteger, isJsonSchemaKeywords, isJsonSchemaLiteral, isJsonSchemaMap, isJsonSchemaNativeEnum, isJsonSchemaNever, isJsonSchemaNull, isJsonSchemaNullable, isJsonSchemaNumber, isJsonSchemaObject, isJsonSchemaPrimitiveType, isJsonSchemaPrimitiveUnion, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaSet, isJsonSchemaString, isJsonSchemaTuple, isJsonSchemaType, isJsonSchemaUndefined, isJsonSchemaUnion, isJsonSchemaUnknown, isNullOnlyJsonSchema, isPropertyOptional, isSchema, isSchemaNullable, isSchemaObject, isSchemaOf, isSchemaWithSource, isStandardSchema, isUntypedConfig, isUntypedConfigStrict, isUntypedInput, isUntypedInputStrict, isUntypedSchema, isUntypedSchemaStrict, isValibotSchema, isValidSchemaConfigFile, isValidSchemaInputFile, mapStorageToFileSystem, merge, mergeMetadata, normalizeKey, readSchemaTypes, traverseSchema };
|
|
1169
|
+
export { JSON_SCHEMA_CONSTRUCTOR_KEY, JSON_SCHEMA_METADATA_KEYS, JSON_SCHEMA_PRIMITIVE_TYPES, JSON_SCHEMA_TYPES, JsonSchemaTypeNames, SCHEMA_ARRAY_CONCAT_KEYWORDS, SCHEMA_RECORD_KEYWORDS, SCHEMA_SINGLE_KEYWORDS, addProperty, applyJsonSchemaMetadata, assertSchemasDoNotContradict, bundleReferences, createStoragePromises, extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaWithSource, extractSource, extractTSType, extractVariant, findSchemaContradictions, getJsonSchema, getJsonSchemaObject, getPrimarySchemaType, getProperties, getPropertiesList, getProperty, hasJsonSchemaConstructorFlag, isFileReference, isJsonSchema, isJsonSchemaAllOf, isJsonSchemaAny, isJsonSchemaAnyOf, isJsonSchemaArray, isJsonSchemaBigint, isJsonSchemaBoolean, isJsonSchemaDate, isJsonSchemaDecimal, isJsonSchemaEnum, isJsonSchemaInteger, isJsonSchemaKeywords, isJsonSchemaLiteral, isJsonSchemaMap, isJsonSchemaNativeEnum, isJsonSchemaNever, isJsonSchemaNull, isJsonSchemaNullable, isJsonSchemaNumber, isJsonSchemaObject, isJsonSchemaPrimitiveType, isJsonSchemaPrimitiveUnion, isJsonSchemaRecord, isJsonSchemaRef, isJsonSchemaSet, isJsonSchemaString, isJsonSchemaTuple, isJsonSchemaType, isJsonSchemaUndefined, isJsonSchemaUnion, isJsonSchemaUnknown, isNullOnlyJsonSchema, isPropertyOptional, isSchema, isSchemaNullable, isSchemaObject, isSchemaOf, isSchemaWithSource, isStandardSchema, isUntypedConfig, isUntypedConfigStrict, isUntypedInput, isUntypedInputStrict, isUntypedSchema, isUntypedSchemaStrict, isValibotSchema, isValidSchemaConfigFile, isValidSchemaInputFile, mapStorageToFileSystem, merge, mergeMetadata, normalizeKey, readSchemaTypes, traverseSchema };
|
|
694
1170
|
//# sourceMappingURL=index.mjs.map
|