@kingbri1/standard-json 0.2.1-pre1
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/README.md +58 -0
- package/dist/arktype-Dl6TXwAj.cjs +1 -0
- package/dist/arktype-rPtBJvA_.js +1 -0
- package/dist/effect-CW3DGCIF.js +1 -0
- package/dist/effect-CjGTzXSx.cjs +1 -0
- package/dist/index-CD6qJBQN.cjs +1 -0
- package/dist/index-I9ikUX_B.js +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +230 -0
- package/dist/index.d.ts +230 -0
- package/dist/index.js +1 -0
- package/dist/valibot-Bj3vthVP.cjs +1 -0
- package/dist/valibot-DoNv6iQ4.js +1 -0
- package/dist/zod-CCbk5bPB.cjs +3 -0
- package/dist/zod-w00sr6hq.js +3 -0
- package/package.json +68 -0
package/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Standard JSON
|
2
|
+
|
3
|
+
[](https://npmjs.org/package/@standard-community/standard-json "View this project on NPM")
|
4
|
+
[](https://www.npmjs.com/package/@standard-community/standard-json)
|
5
|
+
[](LICENSE)
|
6
|
+
|
7
|
+
Standard Schema Validator's JSON Schema Converter
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Install the main package -
|
12
|
+
|
13
|
+
```sh
|
14
|
+
pnpm add @standard-community/standard-json
|
15
|
+
```
|
16
|
+
|
17
|
+
For some specific vendor, install the respective package also -
|
18
|
+
|
19
|
+
| Vendor | Package |
|
20
|
+
| ------- | ------- |
|
21
|
+
| Zod | `zod-to-json-schema` |
|
22
|
+
| Valibot | `@valibot/to-json-schema` |
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ts
|
27
|
+
import { toJsonSchema } from "@standard-community/standard-json";
|
28
|
+
|
29
|
+
// Define your schema
|
30
|
+
const schema = v.pipe(
|
31
|
+
v.object({
|
32
|
+
myString: v.string(),
|
33
|
+
myUnion: v.union([v.number(), v.boolean()]),
|
34
|
+
}),
|
35
|
+
v.description("My neat object schema"),
|
36
|
+
);
|
37
|
+
|
38
|
+
// Convert it to JSON Schema
|
39
|
+
const jsonSchema = await toJsonSchema(schema);
|
40
|
+
```
|
41
|
+
|
42
|
+
## Compatibility
|
43
|
+
|
44
|
+
List of supported validators -
|
45
|
+
|
46
|
+
| Vendor | Supported |
|
47
|
+
| ------- | ------- |
|
48
|
+
| Zod | ✅ |
|
49
|
+
| Valibot | ✅ |
|
50
|
+
| ArkType | ✅ |
|
51
|
+
| Typebox | ✅ (Using [TypeMap](https://github.com/sinclairzx81/typemap) |
|
52
|
+
| Effect Schema | 🛠️ |
|
53
|
+
|
54
|
+
You can check the compatibility versions at [standardschema.dev](https://standardschema.dev/)
|
55
|
+
|
56
|
+
## Credit
|
57
|
+
|
58
|
+
- This project is inspired by the work of [kwaa](https://github.com/kwaa) and their [xsschema](https://xsai.js.org/docs/packages/top-level/xsschema) package.
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var c=Object.defineProperty;var o=(n,s)=>c(n,"name",{value:s,configurable:!0});const e=o(async()=>n=>n.toJsonSchema(),"getToJsonSchemaFn");exports.getToJsonSchemaFn=e;
|
@@ -0,0 +1 @@
|
|
1
|
+
var e=Object.defineProperty;var n=(o,c)=>e(o,"name",{value:c,configurable:!0});const s=n(async()=>o=>o.toJsonSchema(),"getToJsonSchemaFn");export{s as getToJsonSchemaFn};
|
@@ -0,0 +1 @@
|
|
1
|
+
var r=Object.defineProperty;var e=(t,o)=>r(t,"name",{value:o,configurable:!0});import{t as a}from"./index-I9ikUX_B.js";const m=e(async()=>{const{JSONSchema:t}=await a(import("effect"),"effect");return o=>t.make(o)},"getToJsonSchemaFn");export{m as getToJsonSchemaFn};
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var s=Object.create;var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var o=(e,t)=>a(e,"name",{value:t,configurable:!0});var u=(e,t,n,c)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of i(t))!h.call(e,r)&&r!==n&&a(e,r,{get:()=>t[r],enumerable:!(c=m(t,r))||c.enumerable});return e};var J=(e,t,n)=>(n=e!=null?s(S(e)):{},u(t||!e||!e.__esModule?a(n,"default",{value:e,enumerable:!0}):n,e));var f=require("./index-CD6qJBQN.cjs");const g=o(async()=>{const{JSONSchema:e}=await f.tryImport(import("effect"),"effect");return t=>e.make(t)},"getToJsonSchemaFn");exports.getToJsonSchemaFn=g;
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var t=Object.defineProperty;var r=(n,e)=>t(n,"name",{value:e,configurable:!0});const s=r(async(n,e)=>{try{return await n}catch{throw new Error(`standard-json: Missing dependencies "${e}".`)}},"tryImport"),o=r(async n=>{switch(n){case"arktype":return Promise.resolve().then(function(){return require("./arktype-Dl6TXwAj.cjs")}).then(async({getToJsonSchemaFn:e})=>e());case"effect":return Promise.resolve().then(function(){return require("./effect-CjGTzXSx.cjs")}).then(async({getToJsonSchemaFn:e})=>e());case"valibot":return Promise.resolve().then(function(){return require("./valibot-Bj3vthVP.cjs")}).then(async({getToJsonSchemaFn:e})=>e());case"zod":return Promise.resolve().then(function(){return require("./zod-CCbk5bPB.cjs")}).then(async({getToJsonSchemaFn:e})=>e());default:throw new Error(`standard-json: Unsupported schema vendor "${n}"`)}},"getToJsonSchemaFn"),a=r(async n=>o(n["~standard"].vendor).then(async e=>await e(n)),"toJsonSchema");exports.toJsonSchema=a,exports.tryImport=s;
|
@@ -0,0 +1 @@
|
|
1
|
+
var a=Object.defineProperty;var e=(t,n)=>a(t,"name",{value:n,configurable:!0});const s=e(async(t,n)=>{try{return await t}catch{throw new Error(`standard-json: Missing dependencies "${n}".`)}},"tryImport"),r=e(async t=>{switch(t){case"arktype":return import("./arktype-rPtBJvA_.js").then(async({getToJsonSchemaFn:n})=>n());case"effect":return import("./effect-CW3DGCIF.js").then(async({getToJsonSchemaFn:n})=>n());case"valibot":return import("./valibot-DoNv6iQ4.js").then(async({getToJsonSchemaFn:n})=>n());case"zod":return import("./zod-w00sr6hq.js").then(async({getToJsonSchemaFn:n})=>n());default:throw new Error(`standard-json: Unsupported schema vendor "${t}"`)}},"getToJsonSchemaFn"),o=e(async t=>r(t["~standard"].vendor).then(async n=>await n(t)),"toJsonSchema");export{o as a,s as t};
|
package/dist/index.cjs
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var e=require("./index-CD6qJBQN.cjs");exports.toJsonSchema=e.toJsonSchema;
|
package/dist/index.d.cts
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
/** The Standard Schema interface. */
|
2
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
3
|
+
/** The Standard Schema properties. */
|
4
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
5
|
+
}
|
6
|
+
declare namespace StandardSchemaV1 {
|
7
|
+
/** The Standard Schema properties interface. */
|
8
|
+
export interface Props<Input = unknown, Output = Input> {
|
9
|
+
/** The version number of the standard. */
|
10
|
+
readonly version: 1;
|
11
|
+
/** The vendor name of the schema library. */
|
12
|
+
readonly vendor: string;
|
13
|
+
/** Validates unknown input values. */
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
15
|
+
/** Inferred types associated with the schema. */
|
16
|
+
readonly types?: Types<Input, Output> | undefined;
|
17
|
+
}
|
18
|
+
/** The result interface of the validate function. */
|
19
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
20
|
+
/** The result interface if validation succeeds. */
|
21
|
+
export interface SuccessResult<Output> {
|
22
|
+
/** The typed output value. */
|
23
|
+
readonly value: Output;
|
24
|
+
/** The non-existent issues. */
|
25
|
+
readonly issues?: undefined;
|
26
|
+
}
|
27
|
+
/** The result interface if validation fails. */
|
28
|
+
export interface FailureResult {
|
29
|
+
/** The issues of failed validation. */
|
30
|
+
readonly issues: ReadonlyArray<Issue>;
|
31
|
+
}
|
32
|
+
/** The issue interface of the failure output. */
|
33
|
+
export interface Issue {
|
34
|
+
/** The error message of the issue. */
|
35
|
+
readonly message: string;
|
36
|
+
/** The path of the issue, if any. */
|
37
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
38
|
+
}
|
39
|
+
/** The path segment interface of the issue. */
|
40
|
+
export interface PathSegment {
|
41
|
+
/** The key representing a path segment. */
|
42
|
+
readonly key: PropertyKey;
|
43
|
+
}
|
44
|
+
/** The Standard Schema types interface. */
|
45
|
+
export interface Types<Input = unknown, Output = Input> {
|
46
|
+
/** The input type of the schema. */
|
47
|
+
readonly input: Input;
|
48
|
+
/** The output type of the schema. */
|
49
|
+
readonly output: Output;
|
50
|
+
}
|
51
|
+
/** Infers the input type of a Standard Schema. */
|
52
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
53
|
+
/** Infers the output type of a Standard Schema. */
|
54
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
55
|
+
export { };
|
56
|
+
}
|
57
|
+
|
58
|
+
// ==================================================================================================
|
59
|
+
// JSON Schema Draft 07
|
60
|
+
// ==================================================================================================
|
61
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
62
|
+
// --------------------------------------------------------------------------------------------------
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Primitive type
|
66
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
67
|
+
*/
|
68
|
+
type JSONSchema7TypeName =
|
69
|
+
| "string" //
|
70
|
+
| "number"
|
71
|
+
| "integer"
|
72
|
+
| "boolean"
|
73
|
+
| "object"
|
74
|
+
| "array"
|
75
|
+
| "null";
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Primitive type
|
79
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
80
|
+
*/
|
81
|
+
type JSONSchema7Type =
|
82
|
+
| string //
|
83
|
+
| number
|
84
|
+
| boolean
|
85
|
+
| JSONSchema7Object
|
86
|
+
| JSONSchema7Array
|
87
|
+
| null;
|
88
|
+
|
89
|
+
// Workaround for infinite type recursion
|
90
|
+
interface JSONSchema7Object {
|
91
|
+
[key: string]: JSONSchema7Type;
|
92
|
+
}
|
93
|
+
|
94
|
+
// Workaround for infinite type recursion
|
95
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
96
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
97
|
+
|
98
|
+
/**
|
99
|
+
* Meta schema
|
100
|
+
*
|
101
|
+
* Recommended values:
|
102
|
+
* - 'http://json-schema.org/schema#'
|
103
|
+
* - 'http://json-schema.org/hyper-schema#'
|
104
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
105
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
106
|
+
*
|
107
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
108
|
+
*/
|
109
|
+
type JSONSchema7Version = string;
|
110
|
+
|
111
|
+
/**
|
112
|
+
* JSON Schema v7
|
113
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
114
|
+
*/
|
115
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
116
|
+
interface JSONSchema7 {
|
117
|
+
$id?: string | undefined;
|
118
|
+
$ref?: string | undefined;
|
119
|
+
$schema?: JSONSchema7Version | undefined;
|
120
|
+
$comment?: string | undefined;
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
124
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
125
|
+
*/
|
126
|
+
$defs?: {
|
127
|
+
[key: string]: JSONSchema7Definition;
|
128
|
+
} | undefined;
|
129
|
+
|
130
|
+
/**
|
131
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
132
|
+
*/
|
133
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
134
|
+
enum?: JSONSchema7Type[] | undefined;
|
135
|
+
const?: JSONSchema7Type | undefined;
|
136
|
+
|
137
|
+
/**
|
138
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
139
|
+
*/
|
140
|
+
multipleOf?: number | undefined;
|
141
|
+
maximum?: number | undefined;
|
142
|
+
exclusiveMaximum?: number | undefined;
|
143
|
+
minimum?: number | undefined;
|
144
|
+
exclusiveMinimum?: number | undefined;
|
145
|
+
|
146
|
+
/**
|
147
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
148
|
+
*/
|
149
|
+
maxLength?: number | undefined;
|
150
|
+
minLength?: number | undefined;
|
151
|
+
pattern?: string | undefined;
|
152
|
+
|
153
|
+
/**
|
154
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
155
|
+
*/
|
156
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
157
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
158
|
+
maxItems?: number | undefined;
|
159
|
+
minItems?: number | undefined;
|
160
|
+
uniqueItems?: boolean | undefined;
|
161
|
+
contains?: JSONSchema7Definition | undefined;
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
165
|
+
*/
|
166
|
+
maxProperties?: number | undefined;
|
167
|
+
minProperties?: number | undefined;
|
168
|
+
required?: string[] | undefined;
|
169
|
+
properties?: {
|
170
|
+
[key: string]: JSONSchema7Definition;
|
171
|
+
} | undefined;
|
172
|
+
patternProperties?: {
|
173
|
+
[key: string]: JSONSchema7Definition;
|
174
|
+
} | undefined;
|
175
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
176
|
+
dependencies?: {
|
177
|
+
[key: string]: JSONSchema7Definition | string[];
|
178
|
+
} | undefined;
|
179
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
180
|
+
|
181
|
+
/**
|
182
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
183
|
+
*/
|
184
|
+
if?: JSONSchema7Definition | undefined;
|
185
|
+
then?: JSONSchema7Definition | undefined;
|
186
|
+
else?: JSONSchema7Definition | undefined;
|
187
|
+
|
188
|
+
/**
|
189
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
190
|
+
*/
|
191
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
192
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
193
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
194
|
+
not?: JSONSchema7Definition | undefined;
|
195
|
+
|
196
|
+
/**
|
197
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
198
|
+
*/
|
199
|
+
format?: string | undefined;
|
200
|
+
|
201
|
+
/**
|
202
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
203
|
+
*/
|
204
|
+
contentMediaType?: string | undefined;
|
205
|
+
contentEncoding?: string | undefined;
|
206
|
+
|
207
|
+
/**
|
208
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
209
|
+
*/
|
210
|
+
definitions?: {
|
211
|
+
[key: string]: JSONSchema7Definition;
|
212
|
+
} | undefined;
|
213
|
+
|
214
|
+
/**
|
215
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
216
|
+
*/
|
217
|
+
title?: string | undefined;
|
218
|
+
description?: string | undefined;
|
219
|
+
default?: JSONSchema7Type | undefined;
|
220
|
+
readOnly?: boolean | undefined;
|
221
|
+
writeOnly?: boolean | undefined;
|
222
|
+
examples?: JSONSchema7Type | undefined;
|
223
|
+
}
|
224
|
+
|
225
|
+
/**
|
226
|
+
* Converts a Standard Schema to a JSON schema.
|
227
|
+
*/
|
228
|
+
declare const toJsonSchema: (schema: StandardSchemaV1) => Promise<JSONSchema7>;
|
229
|
+
|
230
|
+
export { toJsonSchema };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
/** The Standard Schema interface. */
|
2
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
3
|
+
/** The Standard Schema properties. */
|
4
|
+
readonly "~standard": StandardSchemaV1.Props<Input, Output>;
|
5
|
+
}
|
6
|
+
declare namespace StandardSchemaV1 {
|
7
|
+
/** The Standard Schema properties interface. */
|
8
|
+
export interface Props<Input = unknown, Output = Input> {
|
9
|
+
/** The version number of the standard. */
|
10
|
+
readonly version: 1;
|
11
|
+
/** The vendor name of the schema library. */
|
12
|
+
readonly vendor: string;
|
13
|
+
/** Validates unknown input values. */
|
14
|
+
readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>;
|
15
|
+
/** Inferred types associated with the schema. */
|
16
|
+
readonly types?: Types<Input, Output> | undefined;
|
17
|
+
}
|
18
|
+
/** The result interface of the validate function. */
|
19
|
+
export type Result<Output> = SuccessResult<Output> | FailureResult;
|
20
|
+
/** The result interface if validation succeeds. */
|
21
|
+
export interface SuccessResult<Output> {
|
22
|
+
/** The typed output value. */
|
23
|
+
readonly value: Output;
|
24
|
+
/** The non-existent issues. */
|
25
|
+
readonly issues?: undefined;
|
26
|
+
}
|
27
|
+
/** The result interface if validation fails. */
|
28
|
+
export interface FailureResult {
|
29
|
+
/** The issues of failed validation. */
|
30
|
+
readonly issues: ReadonlyArray<Issue>;
|
31
|
+
}
|
32
|
+
/** The issue interface of the failure output. */
|
33
|
+
export interface Issue {
|
34
|
+
/** The error message of the issue. */
|
35
|
+
readonly message: string;
|
36
|
+
/** The path of the issue, if any. */
|
37
|
+
readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined;
|
38
|
+
}
|
39
|
+
/** The path segment interface of the issue. */
|
40
|
+
export interface PathSegment {
|
41
|
+
/** The key representing a path segment. */
|
42
|
+
readonly key: PropertyKey;
|
43
|
+
}
|
44
|
+
/** The Standard Schema types interface. */
|
45
|
+
export interface Types<Input = unknown, Output = Input> {
|
46
|
+
/** The input type of the schema. */
|
47
|
+
readonly input: Input;
|
48
|
+
/** The output type of the schema. */
|
49
|
+
readonly output: Output;
|
50
|
+
}
|
51
|
+
/** Infers the input type of a Standard Schema. */
|
52
|
+
export type InferInput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["input"];
|
53
|
+
/** Infers the output type of a Standard Schema. */
|
54
|
+
export type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
|
55
|
+
export { };
|
56
|
+
}
|
57
|
+
|
58
|
+
// ==================================================================================================
|
59
|
+
// JSON Schema Draft 07
|
60
|
+
// ==================================================================================================
|
61
|
+
// https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
62
|
+
// --------------------------------------------------------------------------------------------------
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Primitive type
|
66
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
67
|
+
*/
|
68
|
+
type JSONSchema7TypeName =
|
69
|
+
| "string" //
|
70
|
+
| "number"
|
71
|
+
| "integer"
|
72
|
+
| "boolean"
|
73
|
+
| "object"
|
74
|
+
| "array"
|
75
|
+
| "null";
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Primitive type
|
79
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
|
80
|
+
*/
|
81
|
+
type JSONSchema7Type =
|
82
|
+
| string //
|
83
|
+
| number
|
84
|
+
| boolean
|
85
|
+
| JSONSchema7Object
|
86
|
+
| JSONSchema7Array
|
87
|
+
| null;
|
88
|
+
|
89
|
+
// Workaround for infinite type recursion
|
90
|
+
interface JSONSchema7Object {
|
91
|
+
[key: string]: JSONSchema7Type;
|
92
|
+
}
|
93
|
+
|
94
|
+
// Workaround for infinite type recursion
|
95
|
+
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
|
96
|
+
interface JSONSchema7Array extends Array<JSONSchema7Type> {}
|
97
|
+
|
98
|
+
/**
|
99
|
+
* Meta schema
|
100
|
+
*
|
101
|
+
* Recommended values:
|
102
|
+
* - 'http://json-schema.org/schema#'
|
103
|
+
* - 'http://json-schema.org/hyper-schema#'
|
104
|
+
* - 'http://json-schema.org/draft-07/schema#'
|
105
|
+
* - 'http://json-schema.org/draft-07/hyper-schema#'
|
106
|
+
*
|
107
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
|
108
|
+
*/
|
109
|
+
type JSONSchema7Version = string;
|
110
|
+
|
111
|
+
/**
|
112
|
+
* JSON Schema v7
|
113
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
|
114
|
+
*/
|
115
|
+
type JSONSchema7Definition = JSONSchema7 | boolean;
|
116
|
+
interface JSONSchema7 {
|
117
|
+
$id?: string | undefined;
|
118
|
+
$ref?: string | undefined;
|
119
|
+
$schema?: JSONSchema7Version | undefined;
|
120
|
+
$comment?: string | undefined;
|
121
|
+
|
122
|
+
/**
|
123
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
|
124
|
+
* @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
|
125
|
+
*/
|
126
|
+
$defs?: {
|
127
|
+
[key: string]: JSONSchema7Definition;
|
128
|
+
} | undefined;
|
129
|
+
|
130
|
+
/**
|
131
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
|
132
|
+
*/
|
133
|
+
type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
|
134
|
+
enum?: JSONSchema7Type[] | undefined;
|
135
|
+
const?: JSONSchema7Type | undefined;
|
136
|
+
|
137
|
+
/**
|
138
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
|
139
|
+
*/
|
140
|
+
multipleOf?: number | undefined;
|
141
|
+
maximum?: number | undefined;
|
142
|
+
exclusiveMaximum?: number | undefined;
|
143
|
+
minimum?: number | undefined;
|
144
|
+
exclusiveMinimum?: number | undefined;
|
145
|
+
|
146
|
+
/**
|
147
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
|
148
|
+
*/
|
149
|
+
maxLength?: number | undefined;
|
150
|
+
minLength?: number | undefined;
|
151
|
+
pattern?: string | undefined;
|
152
|
+
|
153
|
+
/**
|
154
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
|
155
|
+
*/
|
156
|
+
items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
|
157
|
+
additionalItems?: JSONSchema7Definition | undefined;
|
158
|
+
maxItems?: number | undefined;
|
159
|
+
minItems?: number | undefined;
|
160
|
+
uniqueItems?: boolean | undefined;
|
161
|
+
contains?: JSONSchema7Definition | undefined;
|
162
|
+
|
163
|
+
/**
|
164
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
|
165
|
+
*/
|
166
|
+
maxProperties?: number | undefined;
|
167
|
+
minProperties?: number | undefined;
|
168
|
+
required?: string[] | undefined;
|
169
|
+
properties?: {
|
170
|
+
[key: string]: JSONSchema7Definition;
|
171
|
+
} | undefined;
|
172
|
+
patternProperties?: {
|
173
|
+
[key: string]: JSONSchema7Definition;
|
174
|
+
} | undefined;
|
175
|
+
additionalProperties?: JSONSchema7Definition | undefined;
|
176
|
+
dependencies?: {
|
177
|
+
[key: string]: JSONSchema7Definition | string[];
|
178
|
+
} | undefined;
|
179
|
+
propertyNames?: JSONSchema7Definition | undefined;
|
180
|
+
|
181
|
+
/**
|
182
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
|
183
|
+
*/
|
184
|
+
if?: JSONSchema7Definition | undefined;
|
185
|
+
then?: JSONSchema7Definition | undefined;
|
186
|
+
else?: JSONSchema7Definition | undefined;
|
187
|
+
|
188
|
+
/**
|
189
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
|
190
|
+
*/
|
191
|
+
allOf?: JSONSchema7Definition[] | undefined;
|
192
|
+
anyOf?: JSONSchema7Definition[] | undefined;
|
193
|
+
oneOf?: JSONSchema7Definition[] | undefined;
|
194
|
+
not?: JSONSchema7Definition | undefined;
|
195
|
+
|
196
|
+
/**
|
197
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
|
198
|
+
*/
|
199
|
+
format?: string | undefined;
|
200
|
+
|
201
|
+
/**
|
202
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
|
203
|
+
*/
|
204
|
+
contentMediaType?: string | undefined;
|
205
|
+
contentEncoding?: string | undefined;
|
206
|
+
|
207
|
+
/**
|
208
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
|
209
|
+
*/
|
210
|
+
definitions?: {
|
211
|
+
[key: string]: JSONSchema7Definition;
|
212
|
+
} | undefined;
|
213
|
+
|
214
|
+
/**
|
215
|
+
* @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
|
216
|
+
*/
|
217
|
+
title?: string | undefined;
|
218
|
+
description?: string | undefined;
|
219
|
+
default?: JSONSchema7Type | undefined;
|
220
|
+
readOnly?: boolean | undefined;
|
221
|
+
writeOnly?: boolean | undefined;
|
222
|
+
examples?: JSONSchema7Type | undefined;
|
223
|
+
}
|
224
|
+
|
225
|
+
/**
|
226
|
+
* Converts a Standard Schema to a JSON schema.
|
227
|
+
*/
|
228
|
+
declare const toJsonSchema: (schema: StandardSchemaV1) => Promise<JSONSchema7>;
|
229
|
+
|
230
|
+
export { toJsonSchema };
|
package/dist/index.js
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
import{a as e}from"./index-I9ikUX_B.js";export{e as toJsonSchema};
|
@@ -0,0 +1 @@
|
|
1
|
+
"use strict";var c=Object.create;var s=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var h=Object.getPrototypeOf,u=Object.prototype.hasOwnProperty;var r=(t,o)=>s(t,"name",{value:o,configurable:!0});var J=(t,o,e,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of m(o))!u.call(t,n)&&n!==e&&s(t,n,{get:()=>o[n],enumerable:!(a=i(o,n))||a.enumerable});return t};var S=(t,o,e)=>(e=t!=null?c(h(t)):{},J(o||!t||!t.__esModule?s(e,"default",{value:t,enumerable:!0}):e,t));var g=require("./index-CD6qJBQN.cjs");const p=r(async()=>{const{toJsonSchema:t}=await g.tryImport(import("@valibot/to-json-schema"),"@valibot/to-json-schema");return(o,e)=>t(o,e)},"getToJsonSchemaFn");exports.getToJsonSchemaFn=p;
|
@@ -0,0 +1 @@
|
|
1
|
+
var r=Object.defineProperty;var a=(o,t)=>r(o,"name",{value:t,configurable:!0});import{t as s}from"./index-I9ikUX_B.js";const m=a(async()=>{const{toJsonSchema:o}=await s(import("@valibot/to-json-schema"),"@valibot/to-json-schema");return(t,n)=>o(t,n)},"getToJsonSchemaFn");export{m as getToJsonSchemaFn};
|
@@ -0,0 +1,3 @@
|
|
1
|
+
"use strict";var O=Object.create;var S=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var g=(l,t)=>S(l,"name",{value:t,configurable:!0});var N=(l,t,h,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of j(t))!T.call(l,r)&&r!==h&&S(l,r,{get:()=>t[r],enumerable:!(a=_(t,r))||a.enumerable});return l};var I=(l,t,h)=>(h=l!=null?O(v(l)):{},N(t||!l||!l.__esModule?S(h,"default",{value:l,enumerable:!0}):h,l));var J=require("./index-CD6qJBQN.cjs");class k{static{g(this,"$ZodRegistry")}constructor(){this._map=new WeakMap,this._idmap=new Map}add(t,...h){const a=h[0];if(this._map.set(t,a),a&&typeof a=="object"&&"id"in a){if(this._idmap.has(a.id))throw new Error(`ID ${a.id} already exists in the registry`);this._idmap.set(a.id,t)}return this}remove(t){return this._map.delete(t),this}get(t){const h=t._zod.parent;if(h){const a={...this.get(h)??{}};return delete a.id,{...a,...this._map.get(t)}}return this._map.get(t)}has(t){return this._map.has(t)}}function E(){return new k}g(E,"registry");const $=E(),z={guid:"uuid",url:"uri",datetime:"date-time",json_string:"json-string"};class x{static{g(this,"JSONSchemaGenerator")}constructor(t){this.counter=0,this.metadataRegistry=t?.metadata??$,this.target=t?.target??"draft-2020-12",this.unrepresentable=t?.unrepresentable??"throw",this.override=t?.override??(()=>{}),this.io=t?.io??"output",this.seen=new Map}process(t,h={path:[],schemaPath:[]}){var a;const r=t._zod.def,y=this.seen.get(t);if(y)return y.count++,h.schemaPath.includes(t)&&(y.cycle=h.path),y.count++,y.schema;const c={schema:{},count:1,cycle:void 0};this.seen.set(t,c),t._zod.toJSONSchema&&(c.schema=t._zod.toJSONSchema());const s={...h,schemaPath:[...h.schemaPath,t],path:h.path},m=t._zod.parent;if(m)c.ref=m,this.process(m,s),this.seen.get(m).isParent=!0;else{const i=c.schema;switch(r.type){case"string":{const e=i;e.type="string";const{minimum:n,maximum:o,format:d,pattern:p,contentEncoding:u}=t._zod.bag;typeof n=="number"&&(e.minLength=n),typeof o=="number"&&(e.maxLength=o),d&&(e.format=z[d]??d),p&&(e.pattern=p.source),u&&(e.contentEncoding=u);break}case"number":{const e=i,{minimum:n,maximum:o,format:d,multipleOf:p,exclusiveMaximum:u,exclusiveMinimum:w}=t._zod.bag;typeof d=="string"&&d.includes("int")?e.type="integer":e.type="number",typeof w=="number"&&(e.exclusiveMinimum=w),typeof n=="number"&&(e.minimum=n,typeof w=="number"&&(w>=n?delete e.minimum:delete e.exclusiveMinimum)),typeof u=="number"&&(e.exclusiveMaximum=u),typeof o=="number"&&(e.maximum=o,typeof u=="number"&&(u<=o?delete e.maximum:delete e.exclusiveMaximum)),typeof p=="number"&&(e.multipleOf=p);break}case"boolean":{const e=i;e.type="boolean";break}case"bigint":{if(this.unrepresentable==="throw")throw new Error("BigInt cannot be represented in JSON Schema");break}case"symbol":{if(this.unrepresentable==="throw")throw new Error("Symbols cannot be represented in JSON Schema");break}case"undefined":{const e=i;e.type="null";break}case"null":{i.type="null";break}case"any":break;case"unknown":break;case"never":{i.not={};break}case"void":{if(this.unrepresentable==="throw")throw new Error("Void cannot be represented in JSON Schema");break}case"date":{if(this.unrepresentable==="throw")throw new Error("Date cannot be represented in JSON Schema");break}case"array":{const e=i,{minimum:n,maximum:o}=t._zod.bag;typeof n=="number"&&(e.minItems=n),typeof o=="number"&&(e.maxItems=o),e.type="array",e.items=this.process(r.element,{...s,path:[...s.path,"items"]});break}case"object":{const e=i;e.type="object",e.properties={};const n=r.shape;for(const p in n)e.properties[p]=this.process(n[p],{...s,path:[...s.path,"properties",p]});const o=new Set(Object.keys(n)),d=new Set([...o].filter(p=>{const u=r.shape[p]._zod;return this.io==="input"?u.optin===void 0:u.optout===void 0}));e.required=Array.from(d),r.catchall?._zod.def.type==="never"?e.additionalProperties=!1:r.catchall&&(e.additionalProperties=this.process(r.catchall,{...s,path:[...s.path,"additionalProperties"]}));break}case"union":{const e=i;e.anyOf=r.options.map((n,o)=>this.process(n,{...s,path:[...s.path,"anyOf",o]}));break}case"intersection":{const e=i;e.allOf=[this.process(r.left,{...s,path:[...s.path,"allOf",0]}),this.process(r.right,{...s,path:[...s.path,"allOf",1]})];break}case"tuple":{const e=i;e.type="array";const n=r.items.map((p,u)=>this.process(p,{...s,path:[...s.path,"prefixItems",u]}));if(this.target==="draft-2020-12"?e.prefixItems=n:e.items=n,r.rest){const p=this.process(r.rest,{...s,path:[...s.path,"items"]});this.target==="draft-2020-12"?e.items=p:e.additionalItems=p}r.rest&&(e.items=this.process(r.rest,{...s,path:[...s.path,"items"]}));const{minimum:o,maximum:d}=t._zod.bag;typeof o=="number"&&(e.minItems=o),typeof d=="number"&&(e.maxItems=d);break}case"record":{const e=i;e.type="object",e.propertyNames=this.process(r.keyType,{...s,path:[...s.path,"propertyNames"]}),e.additionalProperties=this.process(r.valueType,{...s,path:[...s.path,"additionalProperties"]});break}case"map":{if(this.unrepresentable==="throw")throw new Error("Map cannot be represented in JSON Schema");break}case"set":{if(this.unrepresentable==="throw")throw new Error("Set cannot be represented in JSON Schema");break}case"enum":{const e=i;e.enum=Object.values(r.entries);break}case"literal":{const e=i,n=[];for(const o of r.values)if(o===void 0){if(this.unrepresentable==="throw")throw new Error("Literal `undefined` cannot be represented in JSON Schema")}else if(typeof o=="bigint"){if(this.unrepresentable==="throw")throw new Error("BigInt literals cannot be represented in JSON Schema");n.push(Number(o))}else n.push(o);if(n.length!==0)if(n.length===1){const o=n[0];e.const=o}else e.enum=n;break}case"file":{if(this.unrepresentable==="throw")throw new Error("File cannot be represented in JSON Schema");break}case"transform":{if(this.unrepresentable==="throw")throw new Error("Transforms cannot be represented in JSON Schema");break}case"nullable":{const e=this.process(r.innerType,s);i.anyOf=[e,{type:"null"}];break}case"nonoptional":{this.process(r.innerType,s),c.ref=r.innerType;break}case"success":{const e=i;e.type="boolean";break}case"default":{this.process(r.innerType,s),c.ref=r.innerType,i.default=r.defaultValue;break}case"prefault":{this.process(r.innerType,s),c.ref=r.innerType,this.io==="input"&&(i._prefault=r.defaultValue);break}case"catch":{this.process(r.innerType,s),c.ref=r.innerType;let e;try{e=r.catchValue(void 0)}catch{throw new Error("Dynamic catch values are not supported in JSON Schema")}i.default=e;break}case"nan":{if(this.unrepresentable==="throw")throw new Error("NaN cannot be represented in JSON Schema");break}case"template_literal":{const e=i,n=t._zod.pattern;if(!n)throw new Error("Pattern not found in template literal");e.type="string",e.pattern=n.source;break}case"pipe":{const e=this.io==="input"?r.in:r.out;this.process(e,s),c.ref=e;break}case"readonly":{this.process(r.innerType,s),c.ref=r.innerType,i.readOnly=!0;break}case"promise":{this.process(r.innerType,s),c.ref=r.innerType;break}case"optional":{this.process(r.innerType,s),c.ref=r.innerType;break}case"lazy":{const e=t._zod.innerType;this.process(e,s),c.ref=e;break}case"custom":{if(this.unrepresentable==="throw")throw new Error("Custom types cannot be represented in JSON Schema");break}}}const b=this.metadataRegistry.get(t);return b&&Object.assign(c.schema,b),this.io==="input"&&r.type==="pipe"&&(delete c.schema.examples,delete c.schema.default,c.schema._prefault&&(c.schema.default=c.schema._prefault)),this.io==="input"&&c.schema._prefault&&((a=c.schema).default??(a.default=c.schema._prefault)),delete c.schema._prefault,this.seen.get(t).schema}emit(t,h){const a={cycles:h?.cycles??"ref",reused:h?.reused??"inline",external:h?.external??void 0},r=this.seen.get(t);if(!r)throw new Error("Unprocessed schema. This is a bug in Zod.");const y=g(f=>{const i=this.target==="draft-2020-12"?"$defs":"definitions";if(a.external){const d=a.external.registry.get(f[0])?.id;if(d)return{ref:a.external.uri(d)};const p=f[1].defId??f[1].schema.id??`schema${this.counter++}`;return f[1].defId=p,{defId:p,ref:`${a.external.uri("__shared")}#/${i}/${p}`}}if(f[1]===r)return{ref:"#"};const n=`#/${i}/`,o=f[1].schema.id??`__schema${this.counter++}`;return{defId:o,ref:n+o}},"makeURI"),c=g(f=>{if(f[1].schema.$ref)return;const i=f[1],{ref:e,defId:n}=y(f);i.def={...i.schema},n&&(i.defId=n);const o=i.schema;for(const d in o)delete o[d],o.$ref=e},"extractToDef");for(const f of this.seen.entries()){const i=f[1];if(t===f[0]){c(f);continue}if(a.external){const n=a.external.registry.get(f[0])?.id;if(t!==f[0]&&n){c(f);continue}}if(this.metadataRegistry.get(f[0])?.id){c(f);continue}if(i.cycle){if(a.cycles==="throw")throw new Error(`Cycle detected: #/${i.cycle?.join("/")}/<root>
|
2
|
+
|
3
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);a.cycles==="ref"&&c(f);continue}if(i.count>1&&a.reused==="ref"){c(f);continue}}const s=g((f,i)=>{const e=this.seen.get(f),n=e.def??e.schema,o={...n};if(e.ref===null)return;const d=e.ref;if(e.ref=null,d){s(d,i);const p=this.seen.get(d).schema;p.$ref&&i.target==="draft-7"?(n.allOf=n.allOf??[],n.allOf.push(p)):(Object.assign(n,p),Object.assign(n,o))}e.isParent||this.override({zodSchema:f,jsonSchema:n})},"flattenRef");for(const f of[...this.seen.entries()].reverse())s(f[0],{target:this.target});const m={...r.def},b=a.external?.defs??{};for(const f of this.seen.entries()){const i=f[1];i.def&&i.defId&&(b[i.defId]=i.def)}!a.external&&Object.keys(b).length>0&&(this.target==="draft-2020-12"?m.$defs=b:m.definitions=b),this.target==="draft-2020-12"?m.$schema="https://json-schema.org/draft-2020-12/schema":this.target==="draft-7"?m.$schema="https://json-schema.org/draft-07/schema":console.warn(`Invalid target: ${this.target}`);try{return JSON.parse(JSON.stringify(m))}catch{throw new Error("Error converting schema to JSON.")}}}function P(l,t){if(l instanceof k){const a=new x(t),r={};for(const s of l._idmap.entries()){const[m,b]=s;a.process(b)}const y={},c={registry:l,uri:t?.uri||(s=>s),defs:r};for(const s of l._idmap.entries()){const[m,b]=s;y[m]=a.emit(b,{...t,external:c})}if(Object.keys(r).length>0){const s=a.target==="draft-2020-12"?"$defs":"definitions";y.__shared={[s]:r}}return{schemas:y}}const h=new x(t);return h.process(l),h.emit(l,t)}g(P,"toJSONSchema");const M=g(async()=>async(l,t)=>{const h=l;if("_zod"in h)return P(h,{target:"draft-7"});{const{zodToJsonSchema:a}=await J.tryImport(import("zod-to-json-schema"),"zod-to-json-schema");return a(h,t)}},"getToJsonSchemaFn");exports.getToJsonSchemaFn=M;
|
@@ -0,0 +1,3 @@
|
|
1
|
+
var x=Object.defineProperty;var g=(d,r)=>x(d,"name",{value:r,configurable:!0});import{t as O}from"./index-I9ikUX_B.js";class k{static{g(this,"$ZodRegistry")}constructor(){this._map=new WeakMap,this._idmap=new Map}add(r,...p){const o=p[0];if(this._map.set(r,o),o&&typeof o=="object"&&"id"in o){if(this._idmap.has(o.id))throw new Error(`ID ${o.id} already exists in the registry`);this._idmap.set(o.id,r)}return this}remove(r){return this._map.delete(r),this}get(r){const p=r._zod.parent;if(p){const o={...this.get(p)??{}};return delete o.id,{...o,...this._map.get(r)}}return this._map.get(r)}has(r){return this._map.has(r)}}function _(){return new k}g(_,"registry");const j=_(),v={guid:"uuid",url:"uri",datetime:"date-time",json_string:"json-string"};class S{static{g(this,"JSONSchemaGenerator")}constructor(r){this.counter=0,this.metadataRegistry=r?.metadata??j,this.target=r?.target??"draft-2020-12",this.unrepresentable=r?.unrepresentable??"throw",this.override=r?.override??(()=>{}),this.io=r?.io??"output",this.seen=new Map}process(r,p={path:[],schemaPath:[]}){var o;const t=r._zod.def,y=this.seen.get(r);if(y)return y.count++,p.schemaPath.includes(r)&&(y.cycle=p.path),y.count++,y.schema;const c={schema:{},count:1,cycle:void 0};this.seen.set(r,c),r._zod.toJSONSchema&&(c.schema=r._zod.toJSONSchema());const s={...p,schemaPath:[...p.schemaPath,r],path:p.path},m=r._zod.parent;if(m)c.ref=m,this.process(m,s),this.seen.get(m).isParent=!0;else{const i=c.schema;switch(t.type){case"string":{const e=i;e.type="string";const{minimum:n,maximum:a,format:l,pattern:h,contentEncoding:u}=r._zod.bag;typeof n=="number"&&(e.minLength=n),typeof a=="number"&&(e.maxLength=a),l&&(e.format=v[l]??l),h&&(e.pattern=h.source),u&&(e.contentEncoding=u);break}case"number":{const e=i,{minimum:n,maximum:a,format:l,multipleOf:h,exclusiveMaximum:u,exclusiveMinimum:w}=r._zod.bag;typeof l=="string"&&l.includes("int")?e.type="integer":e.type="number",typeof w=="number"&&(e.exclusiveMinimum=w),typeof n=="number"&&(e.minimum=n,typeof w=="number"&&(w>=n?delete e.minimum:delete e.exclusiveMinimum)),typeof u=="number"&&(e.exclusiveMaximum=u),typeof a=="number"&&(e.maximum=a,typeof u=="number"&&(u<=a?delete e.maximum:delete e.exclusiveMaximum)),typeof h=="number"&&(e.multipleOf=h);break}case"boolean":{const e=i;e.type="boolean";break}case"bigint":{if(this.unrepresentable==="throw")throw new Error("BigInt cannot be represented in JSON Schema");break}case"symbol":{if(this.unrepresentable==="throw")throw new Error("Symbols cannot be represented in JSON Schema");break}case"undefined":{const e=i;e.type="null";break}case"null":{i.type="null";break}case"any":break;case"unknown":break;case"never":{i.not={};break}case"void":{if(this.unrepresentable==="throw")throw new Error("Void cannot be represented in JSON Schema");break}case"date":{if(this.unrepresentable==="throw")throw new Error("Date cannot be represented in JSON Schema");break}case"array":{const e=i,{minimum:n,maximum:a}=r._zod.bag;typeof n=="number"&&(e.minItems=n),typeof a=="number"&&(e.maxItems=a),e.type="array",e.items=this.process(t.element,{...s,path:[...s.path,"items"]});break}case"object":{const e=i;e.type="object",e.properties={};const n=t.shape;for(const h in n)e.properties[h]=this.process(n[h],{...s,path:[...s.path,"properties",h]});const a=new Set(Object.keys(n)),l=new Set([...a].filter(h=>{const u=t.shape[h]._zod;return this.io==="input"?u.optin===void 0:u.optout===void 0}));e.required=Array.from(l),t.catchall?._zod.def.type==="never"?e.additionalProperties=!1:t.catchall&&(e.additionalProperties=this.process(t.catchall,{...s,path:[...s.path,"additionalProperties"]}));break}case"union":{const e=i;e.anyOf=t.options.map((n,a)=>this.process(n,{...s,path:[...s.path,"anyOf",a]}));break}case"intersection":{const e=i;e.allOf=[this.process(t.left,{...s,path:[...s.path,"allOf",0]}),this.process(t.right,{...s,path:[...s.path,"allOf",1]})];break}case"tuple":{const e=i;e.type="array";const n=t.items.map((h,u)=>this.process(h,{...s,path:[...s.path,"prefixItems",u]}));if(this.target==="draft-2020-12"?e.prefixItems=n:e.items=n,t.rest){const h=this.process(t.rest,{...s,path:[...s.path,"items"]});this.target==="draft-2020-12"?e.items=h:e.additionalItems=h}t.rest&&(e.items=this.process(t.rest,{...s,path:[...s.path,"items"]}));const{minimum:a,maximum:l}=r._zod.bag;typeof a=="number"&&(e.minItems=a),typeof l=="number"&&(e.maxItems=l);break}case"record":{const e=i;e.type="object",e.propertyNames=this.process(t.keyType,{...s,path:[...s.path,"propertyNames"]}),e.additionalProperties=this.process(t.valueType,{...s,path:[...s.path,"additionalProperties"]});break}case"map":{if(this.unrepresentable==="throw")throw new Error("Map cannot be represented in JSON Schema");break}case"set":{if(this.unrepresentable==="throw")throw new Error("Set cannot be represented in JSON Schema");break}case"enum":{const e=i;e.enum=Object.values(t.entries);break}case"literal":{const e=i,n=[];for(const a of t.values)if(a===void 0){if(this.unrepresentable==="throw")throw new Error("Literal `undefined` cannot be represented in JSON Schema")}else if(typeof a=="bigint"){if(this.unrepresentable==="throw")throw new Error("BigInt literals cannot be represented in JSON Schema");n.push(Number(a))}else n.push(a);if(n.length!==0)if(n.length===1){const a=n[0];e.const=a}else e.enum=n;break}case"file":{if(this.unrepresentable==="throw")throw new Error("File cannot be represented in JSON Schema");break}case"transform":{if(this.unrepresentable==="throw")throw new Error("Transforms cannot be represented in JSON Schema");break}case"nullable":{const e=this.process(t.innerType,s);i.anyOf=[e,{type:"null"}];break}case"nonoptional":{this.process(t.innerType,s),c.ref=t.innerType;break}case"success":{const e=i;e.type="boolean";break}case"default":{this.process(t.innerType,s),c.ref=t.innerType,i.default=t.defaultValue;break}case"prefault":{this.process(t.innerType,s),c.ref=t.innerType,this.io==="input"&&(i._prefault=t.defaultValue);break}case"catch":{this.process(t.innerType,s),c.ref=t.innerType;let e;try{e=t.catchValue(void 0)}catch{throw new Error("Dynamic catch values are not supported in JSON Schema")}i.default=e;break}case"nan":{if(this.unrepresentable==="throw")throw new Error("NaN cannot be represented in JSON Schema");break}case"template_literal":{const e=i,n=r._zod.pattern;if(!n)throw new Error("Pattern not found in template literal");e.type="string",e.pattern=n.source;break}case"pipe":{const e=this.io==="input"?t.in:t.out;this.process(e,s),c.ref=e;break}case"readonly":{this.process(t.innerType,s),c.ref=t.innerType,i.readOnly=!0;break}case"promise":{this.process(t.innerType,s),c.ref=t.innerType;break}case"optional":{this.process(t.innerType,s),c.ref=t.innerType;break}case"lazy":{const e=r._zod.innerType;this.process(e,s),c.ref=e;break}case"custom":{if(this.unrepresentable==="throw")throw new Error("Custom types cannot be represented in JSON Schema");break}}}const b=this.metadataRegistry.get(r);return b&&Object.assign(c.schema,b),this.io==="input"&&t.type==="pipe"&&(delete c.schema.examples,delete c.schema.default,c.schema._prefault&&(c.schema.default=c.schema._prefault)),this.io==="input"&&c.schema._prefault&&((o=c.schema).default??(o.default=c.schema._prefault)),delete c.schema._prefault,this.seen.get(r).schema}emit(r,p){const o={cycles:p?.cycles??"ref",reused:p?.reused??"inline",external:p?.external??void 0},t=this.seen.get(r);if(!t)throw new Error("Unprocessed schema. This is a bug in Zod.");const y=g(f=>{const i=this.target==="draft-2020-12"?"$defs":"definitions";if(o.external){const l=o.external.registry.get(f[0])?.id;if(l)return{ref:o.external.uri(l)};const h=f[1].defId??f[1].schema.id??`schema${this.counter++}`;return f[1].defId=h,{defId:h,ref:`${o.external.uri("__shared")}#/${i}/${h}`}}if(f[1]===t)return{ref:"#"};const n=`#/${i}/`,a=f[1].schema.id??`__schema${this.counter++}`;return{defId:a,ref:n+a}},"makeURI"),c=g(f=>{if(f[1].schema.$ref)return;const i=f[1],{ref:e,defId:n}=y(f);i.def={...i.schema},n&&(i.defId=n);const a=i.schema;for(const l in a)delete a[l],a.$ref=e},"extractToDef");for(const f of this.seen.entries()){const i=f[1];if(r===f[0]){c(f);continue}if(o.external){const n=o.external.registry.get(f[0])?.id;if(r!==f[0]&&n){c(f);continue}}if(this.metadataRegistry.get(f[0])?.id){c(f);continue}if(i.cycle){if(o.cycles==="throw")throw new Error(`Cycle detected: #/${i.cycle?.join("/")}/<root>
|
2
|
+
|
3
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);o.cycles==="ref"&&c(f);continue}if(i.count>1&&o.reused==="ref"){c(f);continue}}const s=g((f,i)=>{const e=this.seen.get(f),n=e.def??e.schema,a={...n};if(e.ref===null)return;const l=e.ref;if(e.ref=null,l){s(l,i);const h=this.seen.get(l).schema;h.$ref&&i.target==="draft-7"?(n.allOf=n.allOf??[],n.allOf.push(h)):(Object.assign(n,h),Object.assign(n,a))}e.isParent||this.override({zodSchema:f,jsonSchema:n})},"flattenRef");for(const f of[...this.seen.entries()].reverse())s(f[0],{target:this.target});const m={...t.def},b=o.external?.defs??{};for(const f of this.seen.entries()){const i=f[1];i.def&&i.defId&&(b[i.defId]=i.def)}!o.external&&Object.keys(b).length>0&&(this.target==="draft-2020-12"?m.$defs=b:m.definitions=b),this.target==="draft-2020-12"?m.$schema="https://json-schema.org/draft-2020-12/schema":this.target==="draft-7"?m.$schema="https://json-schema.org/draft-07/schema":console.warn(`Invalid target: ${this.target}`);try{return JSON.parse(JSON.stringify(m))}catch{throw new Error("Error converting schema to JSON.")}}}function N(d,r){if(d instanceof k){const o=new S(r),t={};for(const s of d._idmap.entries()){const[m,b]=s;o.process(b)}const y={},c={registry:d,uri:r?.uri||(s=>s),defs:t};for(const s of d._idmap.entries()){const[m,b]=s;y[m]=o.emit(b,{...r,external:c})}if(Object.keys(t).length>0){const s=o.target==="draft-2020-12"?"$defs":"definitions";y.__shared={[s]:t}}return{schemas:y}}const p=new S(r);return p.process(d),p.emit(d,r)}g(N,"toJSONSchema");const T=g(async()=>async(d,r)=>{const p=d;if("_zod"in p)return N(p,{target:"draft-7"});{const{zodToJsonSchema:o}=await O(import("zod-to-json-schema"),"zod-to-json-schema");return o(p,r)}},"getToJsonSchemaFn");export{T as getToJsonSchemaFn};
|
package/package.json
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
{
|
2
|
+
"name": "@kingbri1/standard-json",
|
3
|
+
"version": "0.2.1-pre1",
|
4
|
+
"type": "module",
|
5
|
+
"license": "MIT",
|
6
|
+
"main": "dist/index.cjs",
|
7
|
+
"module": "dist/index.js",
|
8
|
+
"types": "dist/index.d.ts",
|
9
|
+
"files": [
|
10
|
+
"dist"
|
11
|
+
],
|
12
|
+
"keywords": [
|
13
|
+
"standard-schema",
|
14
|
+
"standard-community",
|
15
|
+
"standard-schema-community",
|
16
|
+
"json-schema",
|
17
|
+
"convertor"
|
18
|
+
],
|
19
|
+
"homepage": "https://github.com/standard-community",
|
20
|
+
"publishConfig": {
|
21
|
+
"access": "public"
|
22
|
+
},
|
23
|
+
"repository": {
|
24
|
+
"type": "git",
|
25
|
+
"url": "git+https://github.com/standard-community/standard-json.git",
|
26
|
+
"directory": "packages/core"
|
27
|
+
},
|
28
|
+
"bugs": {
|
29
|
+
"url": "https://github.com/standard-community/standard-json/issues"
|
30
|
+
},
|
31
|
+
"exports": {
|
32
|
+
"import": {
|
33
|
+
"types": "./dist/index.d.ts",
|
34
|
+
"default": "./dist/index.js"
|
35
|
+
},
|
36
|
+
"require": {
|
37
|
+
"types": "./dist/index.d.cts",
|
38
|
+
"default": "./dist/index.cjs"
|
39
|
+
}
|
40
|
+
},
|
41
|
+
"peerDependencies": {
|
42
|
+
"@valibot/to-json-schema": "^1.0.0-rc.0",
|
43
|
+
"effect": "^3.13.10",
|
44
|
+
"zod-to-json-schema": "^3.24.1"
|
45
|
+
},
|
46
|
+
"peerDependenciesMeta": {
|
47
|
+
"@valibot/to-json-schema": {
|
48
|
+
"optional": true
|
49
|
+
},
|
50
|
+
"effect": {
|
51
|
+
"optional": true
|
52
|
+
},
|
53
|
+
"zod-to-json-schema": {
|
54
|
+
"optional": true
|
55
|
+
}
|
56
|
+
},
|
57
|
+
"devDependencies": {
|
58
|
+
"@standard-schema/spec": "^1.0.0",
|
59
|
+
"@types/json-schema": "^7.0.15",
|
60
|
+
"arktype": "^2.0.4",
|
61
|
+
"pkgroll": "^2.5.1",
|
62
|
+
"valibot": "^1.0.0-rc.4",
|
63
|
+
"zod": "^3.25.0"
|
64
|
+
},
|
65
|
+
"scripts": {
|
66
|
+
"build": "pkgroll --minify --clean-dist"
|
67
|
+
}
|
68
|
+
}
|