@powerlines/schema 0.11.1 → 0.11.2

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.
Files changed (62) hide show
  1. package/README.md +36 -22
  2. package/dist/bundle.d.cts +2 -2
  3. package/dist/bundle.d.cts.map +1 -1
  4. package/dist/bundle.d.mts +2 -2
  5. package/dist/bundle.d.mts.map +1 -1
  6. package/dist/bundle.mjs.map +1 -1
  7. package/dist/codegen.cjs +27 -0
  8. package/dist/codegen.d.cts +15 -0
  9. package/dist/codegen.d.cts.map +1 -0
  10. package/dist/codegen.d.mts +15 -0
  11. package/dist/codegen.d.mts.map +1 -0
  12. package/dist/codegen.mjs +24 -0
  13. package/dist/codegen.mjs.map +1 -0
  14. package/dist/extract.cjs +201 -52
  15. package/dist/extract.d.cts +81 -16
  16. package/dist/extract.d.cts.map +1 -1
  17. package/dist/extract.d.mts +81 -16
  18. package/dist/extract.d.mts.map +1 -1
  19. package/dist/extract.mjs +199 -55
  20. package/dist/extract.mjs.map +1 -1
  21. package/dist/index.cjs +15 -3
  22. package/dist/index.d.cts +7 -5
  23. package/dist/index.d.mts +7 -5
  24. package/dist/index.mjs +6 -4
  25. package/dist/jtd.cjs +385 -0
  26. package/dist/jtd.d.cts +15 -0
  27. package/dist/jtd.d.cts.map +1 -0
  28. package/dist/jtd.d.mts +15 -0
  29. package/dist/jtd.d.mts.map +1 -0
  30. package/dist/jtd.mjs +384 -0
  31. package/dist/jtd.mjs.map +1 -0
  32. package/dist/reflection.cjs +321 -100
  33. package/dist/reflection.d.cts +16 -13
  34. package/dist/reflection.d.cts.map +1 -1
  35. package/dist/reflection.d.mts +16 -13
  36. package/dist/reflection.d.mts.map +1 -1
  37. package/dist/reflection.mjs +323 -101
  38. package/dist/reflection.mjs.map +1 -1
  39. package/dist/resolve.d.cts +5 -5
  40. package/dist/resolve.d.cts.map +1 -1
  41. package/dist/resolve.d.mts +5 -5
  42. package/dist/resolve.d.mts.map +1 -1
  43. package/dist/resolve.mjs.map +1 -1
  44. package/dist/type-checks.cjs +76 -0
  45. package/dist/type-checks.d.cts +43 -0
  46. package/dist/type-checks.d.cts.map +1 -0
  47. package/dist/type-checks.d.mts +43 -0
  48. package/dist/type-checks.d.mts.map +1 -0
  49. package/dist/type-checks.mjs +71 -0
  50. package/dist/type-checks.mjs.map +1 -0
  51. package/dist/types.d.cts +166 -24
  52. package/dist/types.d.cts.map +1 -1
  53. package/dist/types.d.mts +166 -24
  54. package/dist/types.d.mts.map +1 -1
  55. package/package.json +25 -19
  56. package/dist/is-schema-definition.cjs +0 -18
  57. package/dist/is-schema-definition.d.cts +0 -13
  58. package/dist/is-schema-definition.d.cts.map +0 -1
  59. package/dist/is-schema-definition.d.mts +0 -13
  60. package/dist/is-schema-definition.d.mts.map +0 -1
  61. package/dist/is-schema-definition.mjs +0 -17
  62. package/dist/is-schema-definition.mjs.map +0 -1
@@ -1,10 +1,13 @@
1
- import { SchemaDefinition, SchemaDefinitionInput, SchemaDefinitionParameter } from "./types.cjs";
2
- import { PluginContext } from "@powerlines/core";
1
+ import { ExtractedSchema, JTDSchemaType, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.cjs";
2
+ import { Context } from "@powerlines/core";
3
3
  import { BuildOptions } from "esbuild";
4
4
  import { Type } from "@powerlines/deepkit/vendor/type";
5
- import { JsonSchema7Type } from "@stryke/json";
6
5
 
7
6
  //#region src/extract.d.ts
7
+ /**
8
+ * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
9
+ */
10
+ declare function extractHash(variant: SchemaInputVariant, input: SchemaInput): string;
8
11
  /**
9
12
  * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
10
13
  *
@@ -14,39 +17,101 @@ import { JsonSchema7Type } from "@stryke/json";
14
17
  * @param reflection - The reflected Deepkit Type to convert.
15
18
  * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.
16
19
  */
17
- declare function extractReflection(reflection: Type): JsonSchema7Type | undefined;
20
+ declare function extractReflection<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(reflection: Type): JTDSchemaType<TMetadata> | undefined;
18
21
  /**
19
- * Extracts a JSON Schema object from a given schema definition, if possible.
22
+ * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
20
23
  *
21
24
  * @remarks
22
- * This function checks if the provided schema is a Zod schema, a Standard JSON Schema, or already a JSON Schema object. If it is a Zod schema, it extracts the corresponding JSON Schema. If it is a Standard JSON Schema, it retrieves the input JSON Schema targeting draft-07. Finally, it checks if the resulting JSON Schema is an object type and returns it if so.
25
+ * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
26
+ *
27
+ * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
28
+ * @returns The extracted JTD schema if successful, otherwise undefined.
29
+ */
30
+ declare function extractJsonSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(schema: unknown): JTDSchemaType<TMetadata> | undefined;
31
+ /**
32
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
33
+ *
34
+ * @param input - The input object to extract the schema definition from.
35
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
36
+ * @throws An error if the input does not contain a valid schema definition.
37
+ */
38
+ declare function extractResolvedVariant(input: SchemaSourceInput): SchemaSourceVariant;
39
+ /**
40
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
41
+ *
42
+ * @param input - The input object to extract the schema definition from.
43
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
44
+ * @throws An error if the input does not contain a valid schema definition.
45
+ */
46
+ declare function extractVariant(input: SchemaInput): SchemaInputVariant;
47
+ /**
48
+ * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
23
49
  *
24
- * @param schema - The schema definition to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
25
- * @returns The extracted JSON Schema (draft-07) object if successful, otherwise undefined.
50
+ * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
51
+ * @param variant - The variant of the schema definition to extract.
52
+ * @returns The extracted JSON Schema if successful.
53
+ * @throws An error if the input does not contain a valid schema definition.
26
54
  */
27
- declare function extractJsonSchema(schema: unknown): JsonSchema7Type | undefined;
55
+ declare function extractSchemaSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JTDSchemaType<TMetadata>>;
28
56
  /**
29
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `SchemaDefinition` containing the extracted schema and its variant if successful; otherwise, it throws an error.
57
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
30
58
  *
59
+ * @param variant - The variant of the schema definition to extract.
31
60
  * @param input - The input object to extract the schema definition from.
32
- * @returns A `SchemaDefinition` containing the extracted schema and its variant if successful.
61
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
33
62
  * @throws An error if the input does not contain a valid schema definition.
34
63
  */
35
- declare function extractSchema(input: SchemaDefinitionInput): SchemaDefinition;
64
+ declare function extractSource<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
36
65
  /**
37
66
  * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
38
67
  *
39
68
  * @example
40
69
  * ```ts
41
- * const schema = await extract(context, "./schemas/*.ts");
70
+ * // Resolve a schema definition from a file path
71
+ * const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
72
+ * // Resolve a schema definition from a JSON Schema object
73
+ * const schema2 = await extractSchema(context, schemaObject);
74
+ * // Resolve a schema definition from a Zod schema
75
+ * const schema3 = await extractSchema(context, zodSchema);
76
+ * // Resolve a schema definition from a reflected Deepkit Type object
77
+ * const schema4 = await extractSchema(context, reflectionType);
42
78
  * ```
43
79
  *
44
80
  * @param context - The plugin context used for resolving the schema definition input.
45
81
  * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
46
82
  * @param options - Optional overrides for the ESBuild configuration used during resolution.
47
- * @returns A promise that resolves to a SchemaDefinition containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
83
+ * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
84
+ */
85
+ declare function extractSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<ExtractedSchema<TMetadata>>;
86
+ /**
87
+ * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * // Resolve a schema definition from a file path
92
+ * const schema1 = await extract(context, "./schemas.ts#MySchema");
93
+ * // Resolve a schema definition from a JSON Schema object
94
+ * const schema2 = await extract(context, schemaObject);
95
+ * // Resolve a schema definition from a Zod schema
96
+ * const schema3 = await extract(context, zodSchema);
97
+ * // Resolve a schema definition from a reflected Deepkit Type object
98
+ * const schema4 = await extract(context, reflectionType);
99
+ * ```
100
+ *
101
+ * @see https://github.com/colinhacks/zod
102
+ * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
103
+ * @see https://json-schema.org/
104
+ * @see https://ajv.js.org/json-type-definition.html
105
+ * @see https://deepkit.io/en/documentation/runtime-types/reflection
106
+ * @see https://github.com/unjs/untyped
107
+ * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
108
+ *
109
+ * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
110
+ * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
111
+ * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
112
+ * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
48
113
  */
49
- declare function extract<TContext extends PluginContext = PluginContext>(context: TContext, input: SchemaDefinitionParameter, options?: Partial<BuildOptions>): Promise<SchemaDefinition>;
114
+ declare function extract<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<Schema<TMetadata>>;
50
115
  //#endregion
51
- export { extract, extractJsonSchema, extractReflection, extractSchema };
116
+ export { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant };
52
117
  //# sourceMappingURL=extract.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"extract.d.cts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;;AA+CA;;;;;;;iBAAgB,iBAAA,CACd,UAAA,EAAY,IAAA,GACX,eAAA;;AAiBH;;;;;AAmCA;;;iBAnCgB,iBAAA,CACd,MAAA,YACC,eAAA;;;;;;AA4FH;;iBA3DgB,aAAA,CAAc,KAAA,EAAO,qBAAA,GAAwB,gBAAA;;;;;;;;;;;;;;iBA2DvC,OAAA,kBAAyB,aAAA,GAAgB,aAAA,CAAA,CAC7D,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,yBAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,gBAAA"}
1
+ {"version":3,"file":"extract.d.cts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AAmEA;iBAAgB,WAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA;;;;;;;;;AA8BT;iBAAgB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,UAAA,EAAY,IAAA,GAAO,aAAA,CAAc,SAAA;;;;;;;;;;iBAiBnB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,MAAA,YAAkB,aAAA,CAAc,SAAA;;;;;;;;iBAsClB,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAA;;;;AA1CH;;;;iBAuEgB,cAAA,CAAe,KAAA,EAAO,WAAA,GAAc,kBAAA;;;;;;;;;iBAgB9B,mBAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CAEpD,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,aAAA,CAAc,SAAA;;;;;;;;AApDzB;iBA6FgB,aAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,OAAA,EAAS,mBAAA,EAAqB,KAAA,EAAO,iBAAA,GAAoB,YAAA;;;;;;;;AAhE3D;;;;;;;;;AAgBA;;;;iBAgHsB,aAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,SAAA;;;;;;;;;;;;;;;;;;;;;;;;AAzE3B;;;;;iBAkKsB,OAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,SAAA"}
@@ -1,10 +1,13 @@
1
- import { SchemaDefinition, SchemaDefinitionInput, SchemaDefinitionParameter } from "./types.mjs";
1
+ import { ExtractedSchema, JTDSchemaType, Schema, SchemaInput, SchemaInputVariant, SchemaMetadata, SchemaSource, SchemaSourceInput, SchemaSourceVariant } from "./types.mjs";
2
2
  import { BuildOptions } from "esbuild";
3
- import { PluginContext } from "@powerlines/core";
3
+ import { Context } from "@powerlines/core";
4
4
  import { Type } from "@powerlines/deepkit/vendor/type";
5
- import { JsonSchema7Type } from "@stryke/json";
6
5
 
7
6
  //#region src/extract.d.ts
7
+ /**
8
+ * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
9
+ */
10
+ declare function extractHash(variant: SchemaInputVariant, input: SchemaInput): string;
8
11
  /**
9
12
  * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
10
13
  *
@@ -14,39 +17,101 @@ import { JsonSchema7Type } from "@stryke/json";
14
17
  * @param reflection - The reflected Deepkit Type to convert.
15
18
  * @returns A JSON Schema (draft-07) fragment representing the type, or `undefined` when no schema could be produced.
16
19
  */
17
- declare function extractReflection(reflection: Type): JsonSchema7Type | undefined;
20
+ declare function extractReflection<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(reflection: Type): JTDSchemaType<TMetadata> | undefined;
18
21
  /**
19
- * Extracts a JSON Schema object from a given schema definition, if possible.
22
+ * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
20
23
  *
21
24
  * @remarks
22
- * This function checks if the provided schema is a Zod schema, a Standard JSON Schema, or already a JSON Schema object. If it is a Zod schema, it extracts the corresponding JSON Schema. If it is a Standard JSON Schema, it retrieves the input JSON Schema targeting draft-07. Finally, it checks if the resulting JSON Schema is an object type and returns it if so.
25
+ * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
26
+ *
27
+ * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
28
+ * @returns The extracted JTD schema if successful, otherwise undefined.
29
+ */
30
+ declare function extractJsonSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(schema: unknown): JTDSchemaType<TMetadata> | undefined;
31
+ /**
32
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
33
+ *
34
+ * @param input - The input object to extract the schema definition from.
35
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
36
+ * @throws An error if the input does not contain a valid schema definition.
37
+ */
38
+ declare function extractResolvedVariant(input: SchemaSourceInput): SchemaSourceVariant;
39
+ /**
40
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
41
+ *
42
+ * @param input - The input object to extract the schema definition from.
43
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
44
+ * @throws An error if the input does not contain a valid schema definition.
45
+ */
46
+ declare function extractVariant(input: SchemaInput): SchemaInputVariant;
47
+ /**
48
+ * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
23
49
  *
24
- * @param schema - The schema definition to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
25
- * @returns The extracted JSON Schema (draft-07) object if successful, otherwise undefined.
50
+ * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
51
+ * @param variant - The variant of the schema definition to extract.
52
+ * @returns The extracted JSON Schema if successful.
53
+ * @throws An error if the input does not contain a valid schema definition.
26
54
  */
27
- declare function extractJsonSchema(schema: unknown): JsonSchema7Type | undefined;
55
+ declare function extractSchemaSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(input: SchemaSourceInput, variant?: SchemaInputVariant): Promise<JTDSchemaType<TMetadata>>;
28
56
  /**
29
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `SchemaDefinition` containing the extracted schema and its variant if successful; otherwise, it throws an error.
57
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
30
58
  *
59
+ * @param variant - The variant of the schema definition to extract.
31
60
  * @param input - The input object to extract the schema definition from.
32
- * @returns A `SchemaDefinition` containing the extracted schema and its variant if successful.
61
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
33
62
  * @throws An error if the input does not contain a valid schema definition.
34
63
  */
35
- declare function extractSchema(input: SchemaDefinitionInput): SchemaDefinition;
64
+ declare function extractSource<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>>(variant: SchemaSourceVariant, input: SchemaSourceInput): SchemaSource;
36
65
  /**
37
66
  * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
38
67
  *
39
68
  * @example
40
69
  * ```ts
41
- * const schema = await extract(context, "./schemas/*.ts");
70
+ * // Resolve a schema definition from a file path
71
+ * const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
72
+ * // Resolve a schema definition from a JSON Schema object
73
+ * const schema2 = await extractSchema(context, schemaObject);
74
+ * // Resolve a schema definition from a Zod schema
75
+ * const schema3 = await extractSchema(context, zodSchema);
76
+ * // Resolve a schema definition from a reflected Deepkit Type object
77
+ * const schema4 = await extractSchema(context, reflectionType);
42
78
  * ```
43
79
  *
44
80
  * @param context - The plugin context used for resolving the schema definition input.
45
81
  * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
46
82
  * @param options - Optional overrides for the ESBuild configuration used during resolution.
47
- * @returns A promise that resolves to a SchemaDefinition containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
83
+ * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
84
+ */
85
+ declare function extractSchema<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<ExtractedSchema<TMetadata>>;
86
+ /**
87
+ * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * // Resolve a schema definition from a file path
92
+ * const schema1 = await extract(context, "./schemas.ts#MySchema");
93
+ * // Resolve a schema definition from a JSON Schema object
94
+ * const schema2 = await extract(context, schemaObject);
95
+ * // Resolve a schema definition from a Zod schema
96
+ * const schema3 = await extract(context, zodSchema);
97
+ * // Resolve a schema definition from a reflected Deepkit Type object
98
+ * const schema4 = await extract(context, reflectionType);
99
+ * ```
100
+ *
101
+ * @see https://github.com/colinhacks/zod
102
+ * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
103
+ * @see https://json-schema.org/
104
+ * @see https://ajv.js.org/json-type-definition.html
105
+ * @see https://deepkit.io/en/documentation/runtime-types/reflection
106
+ * @see https://github.com/unjs/untyped
107
+ * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
108
+ *
109
+ * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
110
+ * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
111
+ * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
112
+ * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
48
113
  */
49
- declare function extract<TContext extends PluginContext = PluginContext>(context: TContext, input: SchemaDefinitionParameter, options?: Partial<BuildOptions>): Promise<SchemaDefinition>;
114
+ declare function extract<TMetadata extends Partial<SchemaMetadata> = Partial<SchemaMetadata>, TContext extends Context = Context>(context: TContext, input: SchemaInput, options?: Partial<BuildOptions>): Promise<Schema<TMetadata>>;
50
115
  //#endregion
51
- export { extract, extractJsonSchema, extractReflection, extractSchema };
116
+ export { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant };
52
117
  //# sourceMappingURL=extract.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;;AA+CA;;;;;;;iBAAgB,iBAAA,CACd,UAAA,EAAY,IAAA,GACX,eAAA;;AAiBH;;;;;AAmCA;;;iBAnCgB,iBAAA,CACd,MAAA,YACC,eAAA;;;;;;AA4FH;;iBA3DgB,aAAA,CAAc,KAAA,EAAO,qBAAA,GAAwB,gBAAA;;;;;;;;;;;;;;iBA2DvC,OAAA,kBAAyB,aAAA,GAAgB,aAAA,CAAA,CAC7D,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,yBAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,gBAAA"}
1
+ {"version":3,"file":"extract.d.mts","names":[],"sources":["../src/extract.ts"],"mappings":";;;;;;;;AAmEA;iBAAgB,WAAA,CACd,OAAA,EAAS,kBAAA,EACT,KAAA,EAAO,WAAA;;;;;;;;;AA8BT;iBAAgB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,UAAA,EAAY,IAAA,GAAO,aAAA,CAAc,SAAA;;;;;;;;;;iBAiBnB,iBAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,MAAA,YAAkB,aAAA,CAAc,SAAA;;;;;;;;iBAsClB,sBAAA,CACd,KAAA,EAAO,iBAAA,GACN,mBAAA;;;;AA1CH;;;;iBAuEgB,cAAA,CAAe,KAAA,EAAO,WAAA,GAAc,kBAAA;;;;;;;;;iBAgB9B,mBAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CAEpD,KAAA,EAAO,iBAAA,EACP,OAAA,GAAU,kBAAA,GACT,OAAA,CAAQ,aAAA,CAAc,SAAA;;;;;;;;AApDzB;iBA6FgB,aAAA,mBACI,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,EAAA,CACpD,OAAA,EAAS,mBAAA,EAAqB,KAAA,EAAO,iBAAA,GAAoB,YAAA;;;;;;;;AAhE3D;;;;;;;;;AAgBA;;;;iBAgHsB,aAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,eAAA,CAAgB,SAAA;;;;;;;;;;;;;;;;;;;;;;;;AAzE3B;;;;;iBAkKsB,OAAA,mBACF,OAAA,CAAQ,cAAA,IAAkB,OAAA,CAAQ,cAAA,oBACnC,OAAA,GAAU,OAAA,CAAA,CAE3B,OAAA,EAAS,QAAA,EACT,KAAA,EAAO,WAAA,EACP,OAAA,GAAS,OAAA,CAAQ,YAAA,IAChB,OAAA,CAAQ,MAAA,CAAO,SAAA"}
package/dist/extract.mjs CHANGED
@@ -1,17 +1,47 @@
1
- import { isSchemaDefinition } from "./is-schema-definition.mjs";
1
+ import { jsonSchemaToJtd } from "./jtd.mjs";
2
2
  import { reflectionToJsonSchema } from "./reflection.mjs";
3
3
  import { resolve } from "./resolve.mjs";
4
+ import { isExtractedSchema, isJTDSchema, isSchema, isUntypedInput, isUntypedSchema } from "./type-checks.mjs";
4
5
  import defu from "defu";
5
6
  import { isTypeDefinition } from "@powerlines/core";
6
7
  import { esbuildPlugin } from "@powerlines/deepkit/esbuild-plugin";
7
- import { isType } from "@powerlines/deepkit/vendor/type";
8
- import { isJsonSchema7ObjectType, isStandardJsonSchema } from "@stryke/json";
8
+ import { isType, stringifyType } from "@powerlines/deepkit/vendor/type";
9
+ import { murmurhash } from "@stryke/hash";
10
+ import { isJsonSchemaObjectType, isStandardJsonSchema } from "@stryke/json";
9
11
  import { isSetString } from "@stryke/type-checks";
10
12
  import { isSetObject as isSetObject$1 } from "@stryke/type-checks/is-set-object";
11
- import { extractJsonSchema7, isZod3Type } from "@stryke/zod";
13
+ import { extractJsonSchema as extractJsonSchema$1, isZod3Type } from "@stryke/zod";
12
14
 
13
15
  //#region src/extract.ts
14
16
  /**
17
+ * Creates a hash string for a given schema definition input. The function checks the type of the input and generates a hash based on its content. If the input is a Zod schema, it hashes the JSON representation of its internal definition. If the input is a Standard JSON Schema, it hashes the JSON representation of its internal standard schema. If the input is already a JSON Schema object, it hashes its JSON representation directly. If the input is a reflected Deepkit Type object, it hashes its JSON representation. The resulting hash string can be used for caching or comparison purposes.
18
+ */
19
+ function extractHash(variant, input) {
20
+ if (isSetString(input)) return murmurhash({
21
+ variant,
22
+ input
23
+ });
24
+ else if (isSetObject$1(input)) {
25
+ if (isZod3Type(input)) return murmurhash({
26
+ variant,
27
+ input: input._def
28
+ });
29
+ else if (isStandardJsonSchema(input)) return murmurhash({
30
+ variant,
31
+ input: input["~standard"]
32
+ });
33
+ else if (isJsonSchemaObjectType(input)) return murmurhash({
34
+ variant,
35
+ input
36
+ });
37
+ else if (isType(input)) return murmurhash({
38
+ variant,
39
+ input: stringifyType(input)
40
+ });
41
+ }
42
+ 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, or a reflected Deepkit Type object.`);
43
+ }
44
+ /**
15
45
  * Converts a reflected Deepkit {@link @powerlines/deepkit/vendor/type#Type} into a JSON Schema (draft-07) representation.
16
46
  *
17
47
  * @remarks
@@ -25,87 +55,201 @@ function extractReflection(reflection) {
25
55
  return reflectionToJsonSchema(reflection);
26
56
  }
27
57
  /**
28
- * Extracts a JSON Schema object from a given schema definition, if possible.
58
+ * Extracts a JSON Type Definition (RFC 8927) schema from a given schema definition, if possible.
29
59
  *
30
60
  * @remarks
31
- * This function checks if the provided schema is a Zod schema, a Standard JSON Schema, or already a JSON Schema object. If it is a Zod schema, it extracts the corresponding JSON Schema. If it is a Standard JSON Schema, it retrieves the input JSON Schema targeting draft-07. Finally, it checks if the resulting JSON Schema is an object type and returns it if so.
61
+ * This function checks if the provided input is a Zod schema, a Standard JSON Schema, or already a JSON Schema object, extracts a JSON Schema fragment via the appropriate adapter (Zod and Standard Schema produce draft-07 fragments), and then converts that fragment into a valid JTD form suitable for AJV's JTD validator.
32
62
  *
33
- * @param schema - The schema definition to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
34
- * @returns The extracted JSON Schema (draft-07) object if successful, otherwise undefined.
63
+ * @param schema - The schema definition to extract from. This can be a Zod schema, a Standard JSON Schema, or a JSON Schema object.
64
+ * @returns The extracted JTD schema if successful, otherwise undefined.
35
65
  */
36
66
  function extractJsonSchema(schema) {
37
- if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) || isJsonSchema7ObjectType(schema))) {
67
+ if (isSetObject$1(schema) && (isZod3Type(schema) || isStandardJsonSchema(schema) || isJsonSchemaObjectType(schema) || isUntypedInput(schema) || isUntypedSchema(schema))) {
38
68
  let jsonSchema;
39
- if (isZod3Type(schema)) jsonSchema = extractJsonSchema7(schema);
40
- else if (isStandardJsonSchema(schema)) jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-07" });
69
+ if (isZod3Type(schema)) jsonSchema = extractJsonSchema$1(schema);
70
+ else if (isStandardJsonSchema(schema)) jsonSchema = schema["~standard"].jsonSchema.input({ target: "draft-2020-12" });
71
+ else if (isUntypedInput(schema)) jsonSchema = schema.$schema;
41
72
  else jsonSchema = schema;
42
- if (isJsonSchema7ObjectType(jsonSchema)) return jsonSchema;
73
+ const jtd = jsonSchemaToJtd(jsonSchema);
74
+ if (jtd) return jtd;
43
75
  }
44
76
  }
45
77
  /**
46
- * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `SchemaDefinition` containing the extracted schema and its variant if successful; otherwise, it throws an error.
78
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
47
79
  *
48
80
  * @param input - The input object to extract the schema definition from.
49
- * @returns A `SchemaDefinition` containing the extracted schema and its variant if successful.
81
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
50
82
  * @throws An error if the input does not contain a valid schema definition.
51
83
  */
52
- function extractSchema(input) {
84
+ function extractResolvedVariant(input) {
53
85
  if (isSetObject$1(input)) {
54
- if (isZod3Type(input)) {
55
- const schema = extractJsonSchema(input);
56
- if (schema) return {
57
- schema,
58
- variant: "zod3",
59
- input
60
- };
61
- } else if (isStandardJsonSchema(input)) {
62
- const schema = extractJsonSchema(input);
63
- if (schema) return {
64
- schema,
65
- variant: "standard-schema",
66
- input
67
- };
68
- } else if (isJsonSchema7ObjectType(input)) {
69
- const schema = extractJsonSchema(input);
70
- if (schema) return {
71
- schema,
72
- variant: "json-schema",
73
- input
74
- };
75
- } else if (isType(input)) {
76
- const schema = extractReflection(input);
77
- if (schema) return {
78
- schema,
79
- variant: "reflection",
80
- input
81
- };
82
- }
86
+ if (isZod3Type(input)) return "zod3";
87
+ else if (isStandardJsonSchema(input)) return "standard-schema";
88
+ else if (isJTDSchema(input)) return "jtd-schema";
89
+ else if (isJsonSchemaObjectType(input)) return "json-schema";
90
+ else if (isType(input)) return "reflection";
91
+ else if (isUntypedInput(input) || isUntypedSchema(input)) return "untyped";
83
92
  }
84
- 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, or a reflected Deepkit Type object.`);
93
+ throw new Error(`Failed to determine the variant of the provided schema definition input. The input must be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema.`);
94
+ }
95
+ /**
96
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a reflected Deepkit Type object, or an Untyped schema. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
97
+ *
98
+ * @param input - The input object to extract the schema definition from.
99
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
100
+ * @throws An error if the input does not contain a valid schema definition.
101
+ */
102
+ function extractVariant(input) {
103
+ if (isSetString(input) || isTypeDefinition(input)) return "type-definition";
104
+ return extractResolvedVariant(input);
105
+ }
106
+ /**
107
+ * Extracts a JSON Schema object from a given schema definition input. The input can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding JSON Schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns the extracted JSON Schema if successful; otherwise, it throws an error.
108
+ *
109
+ * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
110
+ * @param variant - The variant of the schema definition to extract.
111
+ * @returns The extracted JSON Schema if successful.
112
+ * @throws An error if the input does not contain a valid schema definition.
113
+ */
114
+ async function extractSchemaSchema(input, variant) {
115
+ if (isExtractedSchema(input)) return input.schema;
116
+ const resolvedVariant = variant ?? extractResolvedVariant(input);
117
+ let schema;
118
+ if (resolvedVariant === "zod3" || resolvedVariant === "json-schema" || resolvedVariant === "standard-schema" || resolvedVariant === "untyped") {
119
+ const jsonSchema = extractJsonSchema(input);
120
+ if (jsonSchema) schema = jsonSchemaToJtd(jsonSchema);
121
+ } else if (resolvedVariant === "reflection") schema = extractReflection(input);
122
+ else if (resolvedVariant === "jtd-schema") schema = input;
123
+ if (schema) return schema;
124
+ 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 JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
125
+ }
126
+ /**
127
+ * Extracts a schema definition from a given input object, which can be a Zod schema, a Standard JSON Schema, a JSON Schema object, a JTD schema, an untyped schema, or a reflected Deepkit Type object. The function checks the type of the input and attempts to extract the corresponding schema based on its variant. If the input is a Zod schema, it extracts the JSON Schema using the `extractJsonSchema` function. If the input is a Standard JSON Schema, it retrieves the JSON Schema targeting draft-07. If the input is already a JSON Schema object, it uses it directly. If the input is a reflected Deepkit Type object, it extracts the schema using the `extractReflection` function. The function returns a `Schema` containing the extracted schema and its variant if successful; otherwise, it throws an error.
128
+ *
129
+ * @param variant - The variant of the schema definition to extract.
130
+ * @param input - The input object to extract the schema definition from.
131
+ * @returns A `Schema` containing the extracted schema and its variant if successful.
132
+ * @throws An error if the input does not contain a valid schema definition.
133
+ */
134
+ function extractSource(variant, input) {
135
+ if (variant === "zod3") return {
136
+ hash: extractHash(variant, input),
137
+ variant: "zod3",
138
+ schema: input
139
+ };
140
+ else if (variant === "untyped") return {
141
+ hash: extractHash(variant, input),
142
+ variant: "untyped",
143
+ schema: input
144
+ };
145
+ else if (variant === "standard-schema") return {
146
+ hash: extractHash(variant, input),
147
+ variant: "standard-schema",
148
+ schema: input
149
+ };
150
+ else if (variant === "json-schema") return {
151
+ hash: extractHash(variant, input),
152
+ variant: "json-schema",
153
+ schema: input
154
+ };
155
+ else if (variant === "reflection") return {
156
+ hash: extractHash(variant, input),
157
+ variant: "reflection",
158
+ schema: input
159
+ };
160
+ else if (variant === "jtd-schema") return {
161
+ hash: extractHash(variant, input),
162
+ variant: "jtd-schema",
163
+ schema: input
164
+ };
165
+ 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, a JTD schema, an untyped schema, or a reflected Deepkit Type object.`);
85
166
  }
86
167
  /**
87
168
  * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
88
169
  *
89
170
  * @example
90
171
  * ```ts
91
- * const schema = await extract(context, "./schemas/*.ts");
172
+ * // Resolve a schema definition from a file path
173
+ * const schema1 = await extractSchema(context, "./schemas.ts#MySchema");
174
+ * // Resolve a schema definition from a JSON Schema object
175
+ * const schema2 = await extractSchema(context, schemaObject);
176
+ * // Resolve a schema definition from a Zod schema
177
+ * const schema3 = await extractSchema(context, zodSchema);
178
+ * // Resolve a schema definition from a reflected Deepkit Type object
179
+ * const schema4 = await extractSchema(context, reflectionType);
92
180
  * ```
93
181
  *
94
182
  * @param context - The plugin context used for resolving the schema definition input.
95
183
  * @param input - The schema definition input to extract the JSON Schema from. This can be a Zod schema, a Standard JSON Schema, a JSON Schema object, or a TypeScript type definition.
96
184
  * @param options - Optional overrides for the ESBuild configuration used during resolution.
97
- * @returns A promise that resolves to a SchemaDefinition containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
185
+ * @returns A promise that resolves to a {@link ExtractedSchema} containing the extracted JSON Schema and its variant, or the bytecode if JSON Schema extraction is not possible.
186
+ */
187
+ async function extractSchema(context, input, options = {}) {
188
+ if (isExtractedSchema(input)) return input;
189
+ if (isSchema(input)) return {
190
+ ...input,
191
+ source: {
192
+ hash: extractHash("jtd-schema", input.schema),
193
+ variant: "jtd-schema",
194
+ schema: input.schema
195
+ }
196
+ };
197
+ let source;
198
+ const variant = extractVariant(input);
199
+ if (variant === "type-definition") {
200
+ const resolved = await resolve(context, input, defu(options, { plugins: [esbuildPlugin(context, {
201
+ reflection: "default",
202
+ level: "all"
203
+ })] }));
204
+ source = extractSource(extractResolvedVariant(resolved), resolved);
205
+ } else if ([
206
+ "json-schema",
207
+ "jtd-schema",
208
+ "standard-schema",
209
+ "zod3",
210
+ "untyped",
211
+ "reflection"
212
+ ].includes(variant)) source = extractSource(variant, input);
213
+ else throw new Error(`Invalid schema definition input "${variant}". The variant must be one of "type-definition", "json-schema", "jtd-schema", "standard-schema", "zod3", "untyped", or "reflection".`);
214
+ return {
215
+ variant,
216
+ source,
217
+ schema: await extractSchemaSchema(source.schema, source.variant),
218
+ hash: extractHash(variant, input)
219
+ };
220
+ }
221
+ /**
222
+ * Resolves the provided entry points to their corresponding type definitions. The function accepts an array of entry points, which can be strings (file paths) or type definition objects. It processes each entry point, resolving file paths and glob patterns to find matching files. For each resolved file, it creates a type definition object and resolves it using the `resolveInput` function. The function returns an array of resolved entry type definitions.
223
+ *
224
+ * @example
225
+ * ```ts
226
+ * // Resolve a schema definition from a file path
227
+ * const schema1 = await extract(context, "./schemas.ts#MySchema");
228
+ * // Resolve a schema definition from a JSON Schema object
229
+ * const schema2 = await extract(context, schemaObject);
230
+ * // Resolve a schema definition from a Zod schema
231
+ * const schema3 = await extract(context, zodSchema);
232
+ * // Resolve a schema definition from a reflected Deepkit Type object
233
+ * const schema4 = await extract(context, reflectionType);
234
+ * ```
235
+ *
236
+ * @see https://github.com/colinhacks/zod
237
+ * @see https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec
238
+ * @see https://json-schema.org/
239
+ * @see https://ajv.js.org/json-type-definition.html
240
+ * @see https://deepkit.io/en/documentation/runtime-types/reflection
241
+ * @see https://github.com/unjs/untyped
242
+ * @see https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
243
+ *
244
+ * @param context - The {@link Context | context} used for resolving the {@link Schema | schema} definition input.
245
+ * @param input - The input object or string to extract the {@link Schema | schema} from. This can be {@link TypeDefinitionReference | a string that references a Typescript module}, a [Zod v3 schema](https://github.com/colinhacks/zod), any type that adheres to [the Standard JSON Schema specification](https://standardschema.dev/json-schema#what-schema-libraries-support-this-spec), a [JSON Schema object](https://json-schema.org/), a [JTD schema object](https://ajv.js.org/json-type-definition.html), an [untyped schema](https://github.com/unjs/untyped), or a [TypeScript type reflection](https://deepkit.io/en/documentation/runtime-types/reflection).
246
+ * @param options - Optional overrides for the [ESBuild configuration](https://esbuild.github.io/api/#general-options) used during resolution.
247
+ * @returns A promise that resolves to a {@link Schema | schema} object parsed from the input.
98
248
  */
99
249
  async function extract(context, input, options = {}) {
100
- if (isSchemaDefinition(input)) return input;
101
- let inputObject = input;
102
- if (isSetString(input) || isTypeDefinition(input)) inputObject = await resolve(context, input, defu(options, { plugins: [esbuildPlugin(context, {
103
- reflection: "default",
104
- level: "all"
105
- })] }));
106
- return extractSchema(inputObject);
250
+ return await extractSchema(context, input, options);
107
251
  }
108
252
 
109
253
  //#endregion
110
- export { extract, extractJsonSchema, extractReflection, extractSchema };
254
+ export { extract, extractHash, extractJsonSchema, extractReflection, extractResolvedVariant, extractSchema, extractSchemaSchema, extractSource, extractVariant };
111
255
  //# sourceMappingURL=extract.mjs.map