@trigger.dev/schema-to-json 0.0.0-prerelease-20250808093854
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/LICENSE +201 -0
- package/README.md +151 -0
- package/dist/commonjs/index.d.ts +58 -0
- package/dist/commonjs/index.js +192 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/index.d.ts +58 -0
- package/dist/esm/index.js +185 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +107 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [2023] [Trigger.dev]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @trigger.dev/schema-to-json
|
|
2
|
+
|
|
3
|
+
Convert various schema validation libraries to JSON Schema format.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @trigger.dev/schema-to-json
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Important: Bundle Safety
|
|
12
|
+
|
|
13
|
+
This package is designed to be **bundle-safe**. It does NOT bundle any schema libraries (zod, yup, etc.) as dependencies. Instead:
|
|
14
|
+
|
|
15
|
+
1. **Built-in conversions** work immediately (ArkType, Zod 4, TypeBox)
|
|
16
|
+
2. **External conversions** (Zod 3, Yup, Effect) require the conversion libraries to be available at runtime
|
|
17
|
+
|
|
18
|
+
This design ensures that:
|
|
19
|
+
- ✅ Your bundle size stays small
|
|
20
|
+
- ✅ You only include the schema libraries you actually use
|
|
21
|
+
- ✅ Tree-shaking works properly
|
|
22
|
+
- ✅ No unnecessary dependencies are installed
|
|
23
|
+
|
|
24
|
+
## Supported Schema Libraries
|
|
25
|
+
|
|
26
|
+
- ✅ **Zod** - Full support
|
|
27
|
+
- Zod 4: Native support via built-in `toJsonSchema` method (no external deps needed)
|
|
28
|
+
- Zod 3: Requires `zod-to-json-schema` to be installed
|
|
29
|
+
- ✅ **Yup** - Requires `@sodaru/yup-to-json-schema` to be installed
|
|
30
|
+
- ✅ **ArkType** - Native support (built-in `toJsonSchema` method)
|
|
31
|
+
- ✅ **Effect/Schema** - Requires `effect` or `@effect/schema` to be installed
|
|
32
|
+
- ✅ **TypeBox** - Native support (already JSON Schema compliant)
|
|
33
|
+
- ⏳ **Valibot** - Coming soon
|
|
34
|
+
- ⏳ **Superstruct** - Coming soon
|
|
35
|
+
- ⏳ **Runtypes** - Coming soon
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
### Basic Usage (Built-in conversions only)
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { schemaToJsonSchema } from '@trigger.dev/schema-to-json';
|
|
43
|
+
import { type } from 'arktype';
|
|
44
|
+
|
|
45
|
+
// Works immediately for schemas with built-in conversion
|
|
46
|
+
const arkSchema = type({
|
|
47
|
+
name: 'string',
|
|
48
|
+
age: 'number',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const result = schemaToJsonSchema(arkSchema);
|
|
52
|
+
console.log(result);
|
|
53
|
+
// { jsonSchema: {...}, schemaType: 'arktype' }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Full Usage (With external conversion libraries)
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { schemaToJsonSchema, initializeSchemaConverters } from '@trigger.dev/schema-to-json';
|
|
60
|
+
import { z } from 'zod';
|
|
61
|
+
|
|
62
|
+
// Initialize converters once in your app (loads conversion libraries if available)
|
|
63
|
+
await initializeSchemaConverters();
|
|
64
|
+
|
|
65
|
+
// Now you can convert Zod 3, Yup, and Effect schemas
|
|
66
|
+
const zodSchema = z.object({
|
|
67
|
+
name: z.string(),
|
|
68
|
+
age: z.number(),
|
|
69
|
+
email: z.string().email(),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const result = schemaToJsonSchema(zodSchema);
|
|
73
|
+
console.log(result);
|
|
74
|
+
// {
|
|
75
|
+
// jsonSchema: {
|
|
76
|
+
// type: 'object',
|
|
77
|
+
// properties: {
|
|
78
|
+
// name: { type: 'string' },
|
|
79
|
+
// age: { type: 'number' },
|
|
80
|
+
// email: { type: 'string', format: 'email' }
|
|
81
|
+
// },
|
|
82
|
+
// required: ['name', 'age', 'email']
|
|
83
|
+
// },
|
|
84
|
+
// schemaType: 'zod'
|
|
85
|
+
// }
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API
|
|
89
|
+
|
|
90
|
+
### `schemaToJsonSchema(schema, options?)`
|
|
91
|
+
|
|
92
|
+
Convert a schema to JSON Schema format.
|
|
93
|
+
|
|
94
|
+
**Parameters:**
|
|
95
|
+
- `schema` - The schema to convert
|
|
96
|
+
- `options` (optional)
|
|
97
|
+
- `name` - Name to use for the schema (supported by some converters)
|
|
98
|
+
- `additionalProperties` - Additional properties to merge into the result
|
|
99
|
+
|
|
100
|
+
**Returns:**
|
|
101
|
+
- `{ jsonSchema, schemaType }` - The converted JSON Schema and detected type
|
|
102
|
+
- `undefined` - If the schema cannot be converted
|
|
103
|
+
|
|
104
|
+
### `initializeSchemaConverters()`
|
|
105
|
+
|
|
106
|
+
Initialize the external conversion libraries. Call this once in your application if you need to convert schemas that don't have built-in JSON Schema support (Zod 3, Yup, Effect).
|
|
107
|
+
|
|
108
|
+
**Returns:** `Promise<void>`
|
|
109
|
+
|
|
110
|
+
### `canConvertSchema(schema)`
|
|
111
|
+
|
|
112
|
+
Check if a schema can be converted to JSON Schema.
|
|
113
|
+
|
|
114
|
+
**Returns:** `boolean`
|
|
115
|
+
|
|
116
|
+
### `detectSchemaType(schema)`
|
|
117
|
+
|
|
118
|
+
Detect the type of schema.
|
|
119
|
+
|
|
120
|
+
**Returns:** `'zod' | 'yup' | 'arktype' | 'effect' | 'valibot' | 'superstruct' | 'runtypes' | 'typebox' | 'unknown'`
|
|
121
|
+
|
|
122
|
+
### `areConvertersInitialized()`
|
|
123
|
+
|
|
124
|
+
Check which conversion libraries are available.
|
|
125
|
+
|
|
126
|
+
**Returns:** `{ zod: boolean, yup: boolean, effect: boolean }`
|
|
127
|
+
|
|
128
|
+
## Peer Dependencies
|
|
129
|
+
|
|
130
|
+
Each schema library is an optional peer dependency. Install only the ones you need:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
# For Zod
|
|
134
|
+
npm install zod
|
|
135
|
+
|
|
136
|
+
# For Yup
|
|
137
|
+
npm install yup
|
|
138
|
+
|
|
139
|
+
# For ArkType
|
|
140
|
+
npm install arktype
|
|
141
|
+
|
|
142
|
+
# For Effect
|
|
143
|
+
npm install effect @effect/schema
|
|
144
|
+
|
|
145
|
+
# For TypeBox
|
|
146
|
+
npm install @sinclair/typebox
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## License
|
|
150
|
+
|
|
151
|
+
MIT
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { JSONSchema } from "@trigger.dev/core/v3";
|
|
2
|
+
export type Schema = unknown;
|
|
3
|
+
export type { JSONSchema };
|
|
4
|
+
export interface ConversionOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The name to use for the schema in the JSON Schema
|
|
7
|
+
*/
|
|
8
|
+
name?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Additional JSON Schema properties to merge
|
|
11
|
+
*/
|
|
12
|
+
additionalProperties?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
export interface ConversionResult {
|
|
15
|
+
/**
|
|
16
|
+
* The JSON Schema representation (JSON Schema Draft 7)
|
|
17
|
+
*/
|
|
18
|
+
jsonSchema: JSONSchema;
|
|
19
|
+
/**
|
|
20
|
+
* The detected schema type
|
|
21
|
+
*/
|
|
22
|
+
schemaType: "zod" | "yup" | "arktype" | "effect" | "valibot" | "superstruct" | "runtypes" | "typebox" | "unknown";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a schema from various validation libraries to JSON Schema
|
|
26
|
+
*
|
|
27
|
+
* This function attempts to convert schemas without requiring external dependencies to be bundled.
|
|
28
|
+
* It will only succeed if:
|
|
29
|
+
* 1. The schema has built-in JSON Schema conversion (ArkType, Zod 4, TypeBox)
|
|
30
|
+
* 2. The required conversion library is available at runtime (zod-to-json-schema, @sodaru/yup-to-json-schema, etc.)
|
|
31
|
+
*
|
|
32
|
+
* @param schema The schema to convert
|
|
33
|
+
* @param options Optional conversion options
|
|
34
|
+
* @returns The conversion result or undefined if conversion is not possible
|
|
35
|
+
*/
|
|
36
|
+
export declare function schemaToJsonSchema(schema: Schema, options?: ConversionOptions): ConversionResult | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Initialize the schema conversion libraries
|
|
39
|
+
* This should be called by the consuming application if they want to enable
|
|
40
|
+
* conversion for schemas that don't have built-in JSON Schema support
|
|
41
|
+
*/
|
|
42
|
+
export declare function initializeSchemaConverters(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a schema can be converted to JSON Schema
|
|
45
|
+
*/
|
|
46
|
+
export declare function canConvertSchema(schema: Schema): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Get the detected schema type
|
|
49
|
+
*/
|
|
50
|
+
export declare function detectSchemaType(schema: Schema): ConversionResult["schemaType"];
|
|
51
|
+
/**
|
|
52
|
+
* Check if the conversion libraries are initialized
|
|
53
|
+
*/
|
|
54
|
+
export declare function areConvertersInitialized(): {
|
|
55
|
+
zod: boolean;
|
|
56
|
+
yup: boolean;
|
|
57
|
+
effect: boolean;
|
|
58
|
+
};
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.schemaToJsonSchema = schemaToJsonSchema;
|
|
4
|
+
exports.initializeSchemaConverters = initializeSchemaConverters;
|
|
5
|
+
exports.canConvertSchema = canConvertSchema;
|
|
6
|
+
exports.detectSchemaType = detectSchemaType;
|
|
7
|
+
exports.areConvertersInitialized = areConvertersInitialized;
|
|
8
|
+
/**
|
|
9
|
+
* Convert a schema from various validation libraries to JSON Schema
|
|
10
|
+
*
|
|
11
|
+
* This function attempts to convert schemas without requiring external dependencies to be bundled.
|
|
12
|
+
* It will only succeed if:
|
|
13
|
+
* 1. The schema has built-in JSON Schema conversion (ArkType, Zod 4, TypeBox)
|
|
14
|
+
* 2. The required conversion library is available at runtime (zod-to-json-schema, @sodaru/yup-to-json-schema, etc.)
|
|
15
|
+
*
|
|
16
|
+
* @param schema The schema to convert
|
|
17
|
+
* @param options Optional conversion options
|
|
18
|
+
* @returns The conversion result or undefined if conversion is not possible
|
|
19
|
+
*/
|
|
20
|
+
function schemaToJsonSchema(schema, options) {
|
|
21
|
+
const parser = schema;
|
|
22
|
+
// Check if schema has a built-in toJsonSchema method (e.g., ArkType, Zod 4)
|
|
23
|
+
if (typeof parser.toJsonSchema === "function") {
|
|
24
|
+
try {
|
|
25
|
+
const jsonSchema = parser.toJsonSchema();
|
|
26
|
+
// Determine if it's Zod or ArkType based on other methods
|
|
27
|
+
const schemaType = typeof parser.parseAsync === "function" || typeof parser.parse === "function"
|
|
28
|
+
? "zod"
|
|
29
|
+
: "arktype";
|
|
30
|
+
return {
|
|
31
|
+
jsonSchema: options?.additionalProperties
|
|
32
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
33
|
+
: jsonSchema,
|
|
34
|
+
schemaType,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
// If toJsonSchema fails, continue to other checks
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Check if it's a TypeBox schema (has Static and Kind symbols)
|
|
42
|
+
if (parser[Symbol.for("TypeBox.Kind")] !== undefined) {
|
|
43
|
+
// TypeBox schemas are already JSON Schema compliant
|
|
44
|
+
return {
|
|
45
|
+
jsonSchema: options?.additionalProperties
|
|
46
|
+
? { ...parser, ...options.additionalProperties }
|
|
47
|
+
: parser,
|
|
48
|
+
schemaType: "typebox",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
// For schemas that need external libraries, we need to check if they're available
|
|
52
|
+
// This approach avoids bundling the dependencies while still allowing runtime usage
|
|
53
|
+
// Check if it's a Zod schema (without built-in toJsonSchema)
|
|
54
|
+
if (typeof parser.parseAsync === "function" || typeof parser.parse === "function") {
|
|
55
|
+
try {
|
|
56
|
+
// Try to access zod-to-json-schema if it's available
|
|
57
|
+
// @ts-ignore - This is intentionally dynamic
|
|
58
|
+
if (typeof globalThis.__zodToJsonSchema !== "undefined") {
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
const { zodToJsonSchema } = globalThis.__zodToJsonSchema;
|
|
61
|
+
const jsonSchema = options?.name
|
|
62
|
+
? zodToJsonSchema(parser, options.name)
|
|
63
|
+
: zodToJsonSchema(parser);
|
|
64
|
+
if (jsonSchema && typeof jsonSchema === "object" && "$schema" in jsonSchema) {
|
|
65
|
+
const { $schema, ...rest } = jsonSchema;
|
|
66
|
+
return {
|
|
67
|
+
jsonSchema: options?.additionalProperties
|
|
68
|
+
? { ...rest, ...options.additionalProperties }
|
|
69
|
+
: rest,
|
|
70
|
+
schemaType: "zod",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
jsonSchema: options?.additionalProperties
|
|
75
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
76
|
+
: jsonSchema,
|
|
77
|
+
schemaType: "zod",
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
// Library not available
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Check if it's a Yup schema
|
|
86
|
+
if (typeof parser.validateSync === "function" && typeof parser.describe === "function") {
|
|
87
|
+
try {
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
if (typeof globalThis.__yupToJsonSchema !== "undefined") {
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
const { convertSchema } = globalThis.__yupToJsonSchema;
|
|
92
|
+
const jsonSchema = convertSchema(parser);
|
|
93
|
+
return {
|
|
94
|
+
jsonSchema: options?.additionalProperties
|
|
95
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
96
|
+
: jsonSchema,
|
|
97
|
+
schemaType: "yup",
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
// Library not available
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// Check if it's an Effect schema
|
|
106
|
+
if (typeof parser.ast === "object" && typeof parser.ast._tag === "string") {
|
|
107
|
+
try {
|
|
108
|
+
// @ts-ignore
|
|
109
|
+
if (typeof globalThis.__effectJsonSchema !== "undefined") {
|
|
110
|
+
// @ts-ignore
|
|
111
|
+
const { JSONSchema } = globalThis.__effectJsonSchema;
|
|
112
|
+
const jsonSchema = JSONSchema.make(parser);
|
|
113
|
+
return {
|
|
114
|
+
jsonSchema: options?.additionalProperties
|
|
115
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
116
|
+
: jsonSchema,
|
|
117
|
+
schemaType: "effect",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
// Library not available
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
// Future schema types can be added here...
|
|
126
|
+
// Unknown schema type
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Initialize the schema conversion libraries
|
|
131
|
+
* This should be called by the consuming application if they want to enable
|
|
132
|
+
* conversion for schemas that don't have built-in JSON Schema support
|
|
133
|
+
*/
|
|
134
|
+
async function initializeSchemaConverters() {
|
|
135
|
+
try {
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
globalThis.__zodToJsonSchema = await import("zod-to-json-schema");
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// Zod conversion not available
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
// @ts-ignore
|
|
144
|
+
globalThis.__yupToJsonSchema = await import("@sodaru/yup-to-json-schema");
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// Yup conversion not available
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
// Try Effect first, then @effect/schema
|
|
151
|
+
let module;
|
|
152
|
+
try {
|
|
153
|
+
module = await import("effect");
|
|
154
|
+
}
|
|
155
|
+
catch { }
|
|
156
|
+
if (module?.JSONSchema) {
|
|
157
|
+
// @ts-ignore
|
|
158
|
+
globalThis.__effectJsonSchema = { JSONSchema: module.JSONSchema };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// Effect conversion not available
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Check if a schema can be converted to JSON Schema
|
|
167
|
+
*/
|
|
168
|
+
function canConvertSchema(schema) {
|
|
169
|
+
const result = schemaToJsonSchema(schema);
|
|
170
|
+
return result !== undefined;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get the detected schema type
|
|
174
|
+
*/
|
|
175
|
+
function detectSchemaType(schema) {
|
|
176
|
+
const result = schemaToJsonSchema(schema);
|
|
177
|
+
return result?.schemaType ?? "unknown";
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Check if the conversion libraries are initialized
|
|
181
|
+
*/
|
|
182
|
+
function areConvertersInitialized() {
|
|
183
|
+
return {
|
|
184
|
+
// @ts-ignore
|
|
185
|
+
zod: typeof globalThis.__zodToJsonSchema !== "undefined",
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
yup: typeof globalThis.__yupToJsonSchema !== "undefined",
|
|
188
|
+
// @ts-ignore
|
|
189
|
+
effect: typeof globalThis.__effectJsonSchema !== "undefined",
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;AAiDA,gDAsHC;AAOD,gEA6BC;AAKD,4CAGC;AAKD,4CAGC;AAKD,4DAaC;AAxMD;;;;;;;;;;;GAWG;AACH,SAAgB,kBAAkB,CAChC,MAAc,EACd,OAA2B;IAE3B,MAAM,MAAM,GAAG,MAAa,CAAC;IAE7B,4EAA4E;IAC5E,IAAI,OAAO,MAAM,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;QAC9C,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;YACzC,0DAA0D;YAC1D,MAAM,UAAU,GACd,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,UAAU;gBAC3E,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,SAAS,CAAC;YAChB,OAAO;gBACL,UAAU,EAAE,OAAO,EAAE,oBAAoB;oBACvC,CAAC,CAAC,EAAE,GAAG,UAAU,EAAE,GAAG,OAAO,CAAC,oBAAoB,EAAE;oBACpD,CAAC,CAAC,UAAU;gBACd,UAAU;aACX,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,kDAAkD;QACpD,CAAC;IACH,CAAC;IAED,+DAA+D;IAC/D,IAAI,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;QACrD,oDAAoD;QACpD,OAAO;YACL,UAAU,EAAE,OAAO,EAAE,oBAAoB;gBACvC,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,GAAG,O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AC,iBAAiB,KAAK,WAAW;QACxD,aAAa;QACb,GAAG,EAAE,OAAO,UAAU,CAAC,iBAAiB,KAAK,WAAW;QACxD,aAAa;QACb,MAAM,EAAE,OAAO,UAAU,CAAC,kBAAkB,KAAK,WAAW;KAC7D,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { JSONSchema } from "@trigger.dev/core/v3";
|
|
2
|
+
export type Schema = unknown;
|
|
3
|
+
export type { JSONSchema };
|
|
4
|
+
export interface ConversionOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The name to use for the schema in the JSON Schema
|
|
7
|
+
*/
|
|
8
|
+
name?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Additional JSON Schema properties to merge
|
|
11
|
+
*/
|
|
12
|
+
additionalProperties?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
export interface ConversionResult {
|
|
15
|
+
/**
|
|
16
|
+
* The JSON Schema representation (JSON Schema Draft 7)
|
|
17
|
+
*/
|
|
18
|
+
jsonSchema: JSONSchema;
|
|
19
|
+
/**
|
|
20
|
+
* The detected schema type
|
|
21
|
+
*/
|
|
22
|
+
schemaType: "zod" | "yup" | "arktype" | "effect" | "valibot" | "superstruct" | "runtypes" | "typebox" | "unknown";
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Convert a schema from various validation libraries to JSON Schema
|
|
26
|
+
*
|
|
27
|
+
* This function attempts to convert schemas without requiring external dependencies to be bundled.
|
|
28
|
+
* It will only succeed if:
|
|
29
|
+
* 1. The schema has built-in JSON Schema conversion (ArkType, Zod 4, TypeBox)
|
|
30
|
+
* 2. The required conversion library is available at runtime (zod-to-json-schema, @sodaru/yup-to-json-schema, etc.)
|
|
31
|
+
*
|
|
32
|
+
* @param schema The schema to convert
|
|
33
|
+
* @param options Optional conversion options
|
|
34
|
+
* @returns The conversion result or undefined if conversion is not possible
|
|
35
|
+
*/
|
|
36
|
+
export declare function schemaToJsonSchema(schema: Schema, options?: ConversionOptions): ConversionResult | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Initialize the schema conversion libraries
|
|
39
|
+
* This should be called by the consuming application if they want to enable
|
|
40
|
+
* conversion for schemas that don't have built-in JSON Schema support
|
|
41
|
+
*/
|
|
42
|
+
export declare function initializeSchemaConverters(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Check if a schema can be converted to JSON Schema
|
|
45
|
+
*/
|
|
46
|
+
export declare function canConvertSchema(schema: Schema): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Get the detected schema type
|
|
49
|
+
*/
|
|
50
|
+
export declare function detectSchemaType(schema: Schema): ConversionResult["schemaType"];
|
|
51
|
+
/**
|
|
52
|
+
* Check if the conversion libraries are initialized
|
|
53
|
+
*/
|
|
54
|
+
export declare function areConvertersInitialized(): {
|
|
55
|
+
zod: boolean;
|
|
56
|
+
yup: boolean;
|
|
57
|
+
effect: boolean;
|
|
58
|
+
};
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a schema from various validation libraries to JSON Schema
|
|
3
|
+
*
|
|
4
|
+
* This function attempts to convert schemas without requiring external dependencies to be bundled.
|
|
5
|
+
* It will only succeed if:
|
|
6
|
+
* 1. The schema has built-in JSON Schema conversion (ArkType, Zod 4, TypeBox)
|
|
7
|
+
* 2. The required conversion library is available at runtime (zod-to-json-schema, @sodaru/yup-to-json-schema, etc.)
|
|
8
|
+
*
|
|
9
|
+
* @param schema The schema to convert
|
|
10
|
+
* @param options Optional conversion options
|
|
11
|
+
* @returns The conversion result or undefined if conversion is not possible
|
|
12
|
+
*/
|
|
13
|
+
export function schemaToJsonSchema(schema, options) {
|
|
14
|
+
const parser = schema;
|
|
15
|
+
// Check if schema has a built-in toJsonSchema method (e.g., ArkType, Zod 4)
|
|
16
|
+
if (typeof parser.toJsonSchema === "function") {
|
|
17
|
+
try {
|
|
18
|
+
const jsonSchema = parser.toJsonSchema();
|
|
19
|
+
// Determine if it's Zod or ArkType based on other methods
|
|
20
|
+
const schemaType = typeof parser.parseAsync === "function" || typeof parser.parse === "function"
|
|
21
|
+
? "zod"
|
|
22
|
+
: "arktype";
|
|
23
|
+
return {
|
|
24
|
+
jsonSchema: options?.additionalProperties
|
|
25
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
26
|
+
: jsonSchema,
|
|
27
|
+
schemaType,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
// If toJsonSchema fails, continue to other checks
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Check if it's a TypeBox schema (has Static and Kind symbols)
|
|
35
|
+
if (parser[Symbol.for("TypeBox.Kind")] !== undefined) {
|
|
36
|
+
// TypeBox schemas are already JSON Schema compliant
|
|
37
|
+
return {
|
|
38
|
+
jsonSchema: options?.additionalProperties
|
|
39
|
+
? { ...parser, ...options.additionalProperties }
|
|
40
|
+
: parser,
|
|
41
|
+
schemaType: "typebox",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
// For schemas that need external libraries, we need to check if they're available
|
|
45
|
+
// This approach avoids bundling the dependencies while still allowing runtime usage
|
|
46
|
+
// Check if it's a Zod schema (without built-in toJsonSchema)
|
|
47
|
+
if (typeof parser.parseAsync === "function" || typeof parser.parse === "function") {
|
|
48
|
+
try {
|
|
49
|
+
// Try to access zod-to-json-schema if it's available
|
|
50
|
+
// @ts-ignore - This is intentionally dynamic
|
|
51
|
+
if (typeof globalThis.__zodToJsonSchema !== "undefined") {
|
|
52
|
+
// @ts-ignore
|
|
53
|
+
const { zodToJsonSchema } = globalThis.__zodToJsonSchema;
|
|
54
|
+
const jsonSchema = options?.name
|
|
55
|
+
? zodToJsonSchema(parser, options.name)
|
|
56
|
+
: zodToJsonSchema(parser);
|
|
57
|
+
if (jsonSchema && typeof jsonSchema === "object" && "$schema" in jsonSchema) {
|
|
58
|
+
const { $schema, ...rest } = jsonSchema;
|
|
59
|
+
return {
|
|
60
|
+
jsonSchema: options?.additionalProperties
|
|
61
|
+
? { ...rest, ...options.additionalProperties }
|
|
62
|
+
: rest,
|
|
63
|
+
schemaType: "zod",
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
jsonSchema: options?.additionalProperties
|
|
68
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
69
|
+
: jsonSchema,
|
|
70
|
+
schemaType: "zod",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
// Library not available
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Check if it's a Yup schema
|
|
79
|
+
if (typeof parser.validateSync === "function" && typeof parser.describe === "function") {
|
|
80
|
+
try {
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
if (typeof globalThis.__yupToJsonSchema !== "undefined") {
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
const { convertSchema } = globalThis.__yupToJsonSchema;
|
|
85
|
+
const jsonSchema = convertSchema(parser);
|
|
86
|
+
return {
|
|
87
|
+
jsonSchema: options?.additionalProperties
|
|
88
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
89
|
+
: jsonSchema,
|
|
90
|
+
schemaType: "yup",
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
// Library not available
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Check if it's an Effect schema
|
|
99
|
+
if (typeof parser.ast === "object" && typeof parser.ast._tag === "string") {
|
|
100
|
+
try {
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
if (typeof globalThis.__effectJsonSchema !== "undefined") {
|
|
103
|
+
// @ts-ignore
|
|
104
|
+
const { JSONSchema } = globalThis.__effectJsonSchema;
|
|
105
|
+
const jsonSchema = JSONSchema.make(parser);
|
|
106
|
+
return {
|
|
107
|
+
jsonSchema: options?.additionalProperties
|
|
108
|
+
? { ...jsonSchema, ...options.additionalProperties }
|
|
109
|
+
: jsonSchema,
|
|
110
|
+
schemaType: "effect",
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
// Library not available
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Future schema types can be added here...
|
|
119
|
+
// Unknown schema type
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Initialize the schema conversion libraries
|
|
124
|
+
* This should be called by the consuming application if they want to enable
|
|
125
|
+
* conversion for schemas that don't have built-in JSON Schema support
|
|
126
|
+
*/
|
|
127
|
+
export async function initializeSchemaConverters() {
|
|
128
|
+
try {
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
globalThis.__zodToJsonSchema = await import("zod-to-json-schema");
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// Zod conversion not available
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
globalThis.__yupToJsonSchema = await import("@sodaru/yup-to-json-schema");
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
// Yup conversion not available
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
// Try Effect first, then @effect/schema
|
|
144
|
+
let module;
|
|
145
|
+
try {
|
|
146
|
+
module = await import("effect");
|
|
147
|
+
}
|
|
148
|
+
catch { }
|
|
149
|
+
if (module?.JSONSchema) {
|
|
150
|
+
// @ts-ignore
|
|
151
|
+
globalThis.__effectJsonSchema = { JSONSchema: module.JSONSchema };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
// Effect conversion not available
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Check if a schema can be converted to JSON Schema
|
|
160
|
+
*/
|
|
161
|
+
export function canConvertSchema(schema) {
|
|
162
|
+
const result = schemaToJsonSchema(schema);
|
|
163
|
+
return result !== undefined;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get the detected schema type
|
|
167
|
+
*/
|
|
168
|
+
export function detectSchemaType(schema) {
|
|
169
|
+
const result = schemaToJsonSchema(schema);
|
|
170
|
+
return result?.schemaType ?? "unknown";
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Check if the conversion libraries are initialized
|
|
174
|
+
*/
|
|
175
|
+
export function areConvertersInitialized() {
|
|
176
|
+
return {
|
|
177
|
+
// @ts-ignore
|
|
178
|
+
zod: typeof globalThis.__zodToJsonSchema !== "undefined",
|
|
179
|
+
// @ts-ignore
|
|
180
|
+
yup: typeof globalThis.__yupToJsonSchema !== "undefined",
|
|
181
|
+
// @ts-ignore
|
|
182
|
+
effect: typeof globalThis.__effectJsonSchema !== "undefined",
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAqCA;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAChC,MAAc,EACd,OAA2B;IAE3B,MAAM,MAAM,GAAG,MAAa,CAAC;IAE7B,4EAA4E;IAC5E,IAAI,OAAO,MAAM,CAAC,YAAY,KAAK,UAAU,EAAE,CAAC;QAC9C,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,MAAM,CAAC,YAAY,EAAE,CAAC;YACzC,0DAA0D;YAC1D,MAAM,UAAU,GACd,OAAO,MAAM,CAAC,UAAU,KAAK,UAAU,IAAI,OAAO,MAAM,CAAC,KAAK,KAAK,UAAU;gBAC3E,CAAC,CAAC,KAAK;gBACP,CAAC,CAAC,SAAS,CAAC;YAChB,OAAO;gBACL,UAAU,EAAE,OAAO,EAAE,oBAAoB;oBACvC,CAAC,CAAC,EAAE,GAAG,UAAU,EAAE,GAAG,OAAO,CAAC,oBAAoB,EAAE;oBACpD,CAAC,CAAC,UAAU;gBACd,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AkB,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,MAAM,KAAK,SAAS,CAAC;AAC9B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC7C,MAAM,MAAM,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC1C,OAAO,MAAM,EAAE,UAAU,IAAI,SAAS,CAAC;AACzC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB;IAKtC,OAAO;QACL,aAAa;QACb,GAAG,EAAE,OAAO,UAAU,CAAC,iBAAiB,KAAK,WAAW;QACxD,aAAa;QACb,GAAG,EAAE,OAAO,UAAU,CAAC,iBAAiB,KAAK,WAAW;QACxD,aAAa;QACb,MAAM,EAAE,OAAO,UAAU,CAAC,kBAAkB,KAAK,WAAW;KAC7D,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trigger.dev/schema-to-json",
|
|
3
|
+
"version": "0.0.0-prerelease-20250808093854",
|
|
4
|
+
"description": "Convert various schema validation libraries to JSON Schema",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/triggerdotdev/trigger.dev",
|
|
12
|
+
"directory": "packages/schema-to-json"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=18.20.0"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/esm/index.d.ts",
|
|
25
|
+
"default": "./dist/esm/index.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
29
|
+
"default": "./dist/commonjs/index.js"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"zod-to-json-schema": "^3.24.5",
|
|
35
|
+
"@sodaru/yup-to-json-schema": "^2.0.1",
|
|
36
|
+
"@trigger.dev/core": "0.0.0-prerelease-20250808093854"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"arktype": "^2.0.0",
|
|
40
|
+
"effect": "^3.11.11",
|
|
41
|
+
"runtypes": "^6.7.0",
|
|
42
|
+
"superstruct": "^2.0.2",
|
|
43
|
+
"tshy": "^3.0.2",
|
|
44
|
+
"@sinclair/typebox": "^0.34.3",
|
|
45
|
+
"valibot": "^1.1.0",
|
|
46
|
+
"yup": "^1.7.0",
|
|
47
|
+
"zod": "^3.24.1 || ^4.0.0",
|
|
48
|
+
"rimraf": "6.0.1",
|
|
49
|
+
"@arethetypeswrong/cli": "^0.15.4"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"arktype": ">=2.0.0",
|
|
53
|
+
"effect": ">=3.0.0",
|
|
54
|
+
"runtypes": ">=5.0.0",
|
|
55
|
+
"superstruct": ">=0.14.2",
|
|
56
|
+
"@sinclair/typebox": ">=0.34.30",
|
|
57
|
+
"valibot": ">=0.41.0",
|
|
58
|
+
"yup": ">=1.0.0",
|
|
59
|
+
"zod": "^3.24.1 || ^4.0.0"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"arktype": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"effect": {
|
|
66
|
+
"optional": true
|
|
67
|
+
},
|
|
68
|
+
"runtypes": {
|
|
69
|
+
"optional": true
|
|
70
|
+
},
|
|
71
|
+
"superstruct": {
|
|
72
|
+
"optional": true
|
|
73
|
+
},
|
|
74
|
+
"@sinclair/typebox": {
|
|
75
|
+
"optional": true
|
|
76
|
+
},
|
|
77
|
+
"valibot": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"yup": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"zod": {
|
|
84
|
+
"optional": true
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"tshy": {
|
|
88
|
+
"selfLink": false,
|
|
89
|
+
"exports": {
|
|
90
|
+
".": "./src/index.ts"
|
|
91
|
+
},
|
|
92
|
+
"project": "./tsconfig.src.json"
|
|
93
|
+
},
|
|
94
|
+
"main": "./dist/commonjs/index.js",
|
|
95
|
+
"types": "./dist/commonjs/index.d.ts",
|
|
96
|
+
"module": "./dist/esm/index.js",
|
|
97
|
+
"scripts": {
|
|
98
|
+
"clean": "rimraf dist",
|
|
99
|
+
"build": "pnpm run clean && pnpm run build:tshy && pnpm run update-version",
|
|
100
|
+
"build:tshy": "tshy",
|
|
101
|
+
"dev": "tshy --watch",
|
|
102
|
+
"typecheck": "tsc -p tsconfig.src.json --noEmit",
|
|
103
|
+
"test": "vitest",
|
|
104
|
+
"update-version": "tsx ../../scripts/updateVersion.ts",
|
|
105
|
+
"check-exports": "attw --pack ."
|
|
106
|
+
}
|
|
107
|
+
}
|