@@ -30,117 +30,128 @@ Here is the list of libraries that `toJsonSchema` is compatible with:
30
30
If you're using xsAI, this package is a dependency of some of those packages - you don't need to install it separately.
31
31
</Callout >
32
32
33
- ###toJsonSchema (arktype)
33
+ ###toJsonSchema
34
34
35
35
``` ts twoslash
36
36
import {type }from ' arktype'
37
+ import {Schema }from ' effect'
38
+ import * as v from ' valibot'
37
39
import {toJsonSchema }from ' xsschema'
40
+ import {z }from ' zod'
38
41
39
- const schema = type ({
42
+ const arktypeSchema = type ({
40
43
myString:' string' ,
41
44
myUnion:' number | boolean' ,
42
45
}).describe (' My neat object schema' )
43
46
44
- const jsonSchema= await toJsonSchema (schema )
45
- ```
46
-
47
- ###toJsonSchema (effect)
48
-
49
- ``` ts twoslash
50
- import {Schema }from ' effect'
51
- import {toJsonSchema }from ' xsschema'
47
+ const arktypeJsonSchema= await toJsonSchema (arktypeSchema )
52
48
53
- const schema = Schema .Struct ({
49
+ const effectSchema = Schema .Struct ({
54
50
myString:Schema .String ,
55
51
myUnion:Schema .Union (Schema .Number ,Schema .Boolean ),
56
52
}).annotations ({ description:' My neat object schema' })
57
53
58
- const jsonSchema = await toJsonSchema (
54
+ const effectJsonSchema = await toJsonSchema (
59
55
// https://github.com/Effect-TS/effect/issues/4494 // [!code highlight]
60
- Object .assign (schema ,Schema .standardSchemaV1 (schema ))// [!code highlight]
56
+ Object .assign (effectSchema ,Schema .standardSchemaV1 (effectSchema ))// [!code highlight]
61
57
)
62
- ```
63
-
64
- ###toJsonSchema (valibot)
65
58
66
- ``` ts twoslash
67
- import * as v from ' valibot'
68
- import {toJsonSchema }from ' xsschema'
69
-
70
- const schema= v .pipe (
59
+ const valibotSchema= v .pipe (
71
60
v .object ({
72
61
myString:v .string (),
73
62
myUnion:v .union ([v .number (),v .boolean ()]),
74
63
}),
75
64
v .description (' My neat object schema' ),
76
65
)
77
66
78
- const jsonSchema= await toJsonSchema (schema )
79
- ```
80
-
81
- ###toJsonSchema (zod)
82
-
83
- ``` ts twoslash
84
- import {toJsonSchema }from ' xsschema'
85
- import * as z from ' zod'
67
+ const valibotJsonSchema= await toJsonSchema (valibotSchema )
86
68
87
- const schema = z .object ({
69
+ const zodSchema = z .object ({
88
70
myString:z .string (),
89
71
myUnion:z .union ([z .number (),z .boolean ()]),
90
72
}).describe (' My neat object schema' )
91
73
92
- const jsonSchema = await toJsonSchema (schema )
74
+ const zodJsonSchema = await toJsonSchema (zodSchema )
93
75
```
94
76
95
- ###validate
96
-
97
- ``` ts twoslash
98
- import {validate }from ' xsschema'
99
- import {type }from ' arktype'
100
- import * as v from ' valibot'
101
- import * as z from ' zod'
102
-
103
- const arktypeSchema= type (" string" )
104
- const valibotSchema= v .string ()
105
- const zodSchema= z .string ()
106
-
107
- const arktypeResult= await validate (arktypeSchema ,' 123' )
108
- const valibotResult= await validate (valibotSchema ,' 123' )
109
- const zodResult= await validate (zodSchema ,' 123' )
110
- ```
111
-
112
- ##Synchronous Usage
77
+ ###toJsonSchemaSync (experimental)
113
78
114
79
` toJsonSchema ` is async (because it has to` await import ` the vendor's dependencies), but we also support a synchronous version:` toJsonSchemaSync ` .
115
80
116
81
<Callout type = " important" >
117
- Before using` toJsonSchemaSync ` , you must call` registerStandardSchemaVendor ` with your schema vendor.
82
+ Before using` toJsonSchemaSync ` , you must call` initToJsonSchemaSyncVendor ` with your schema vendor.
118
83
</Callout >
119
84
120
85
``` ts twoslash
121
- import {registerStandardSchemaVendor }from ' xsschema'
86
+ import {initToJsonSchemaSyncVendor }from ' xsschema'
122
87
123
- await registerStandardSchemaVendor ( ' zod ' )
88
+ await initToJsonSchemaSyncVendor ( ' arktype ' )
124
89
// or
125
- await registerStandardSchemaVendor ( ' valibot ' )
90
+ await initToJsonSchemaSyncVendor ( ' effect ' )
126
91
// or
127
- await registerStandardSchemaVendor (' arktype' )
92
+ await initToJsonSchemaSyncVendor (' valibot' )
93
+ // or
94
+ await initToJsonSchemaSyncVendor (' zod' )
128
95
```
129
96
130
97
You can register multiple schema vendors if you need to.
131
98
132
- ###toJsonSchemaSync (zod)
133
99
134
100
``` ts twoslash
135
- import {toJsonSchemaSync ,registerStandardSchemaVendor }from ' xsschema'
136
- import * as z from ' zod'
101
+ import {type }from ' arktype'
102
+ import {Schema }from ' effect'
103
+ import * as v from ' valibot'
104
+ import {toJsonSchemaSync }from ' xsschema'
105
+ import {z }from ' zod'
106
+
107
+ const arktypeSchema= type ({
108
+ myString:' string' ,
109
+ myUnion:' number | boolean' ,
110
+ }).describe (' My neat object schema' )
111
+
112
+ const arktypeJsonSchema= toJsonSchemaSync (arktypeSchema )
113
+
114
+ const effectSchema= Schema .Struct ({
115
+ myString:Schema .String ,
116
+ myUnion:Schema .Union (Schema .Number ,Schema .Boolean ),
117
+ }).annotations ({ description:' My neat object schema' })
137
118
138
- await registerStandardSchemaVendor (' zod' )
119
+ const effectJsonSchema= toJsonSchemaSync (
120
+ // https://github.com/Effect-TS/effect/issues/4494 // [!code highlight]
121
+ Object .assign (effectSchema ,Schema .standardSchemaV1 (effectSchema ))// [!code highlight]
122
+ )
139
123
140
- const schema= z .object ({
124
+ const valibotSchema= v .pipe (
125
+ v .object ({
126
+ myString:v .string (),
127
+ myUnion:v .union ([v .number (),v .boolean ()]),
128
+ }),
129
+ v .description (' My neat object schema' ),
130
+ )
131
+
132
+ const valibotJsonSchema= toJsonSchemaSync (valibotSchema )
133
+
134
+ const zodSchema= z .object ({
141
135
myString:z .string (),
142
136
myUnion:z .union ([z .number (),z .boolean ()]),
143
137
}).describe (' My neat object schema' )
144
138
145
- const jsonSchema= toJsonSchemaSync (schema )
139
+ const zodJsonSchema= toJsonSchemaSync (zodSchema )
140
+ ```
141
+
142
+ ###validate
143
+
144
+ ``` ts twoslash
145
+ import {validate }from ' xsschema'
146
+ import {type }from ' arktype'
147
+ import * as v from ' valibot'
148
+ import * as z from ' zod'
149
+
150
+ const arktypeSchema= type (" string" )
151
+ const valibotSchema= v .string ()
152
+ const zodSchema= z .string ()
153
+
154
+ const arktypeResult= await validate (arktypeSchema ,' 123' )
155
+ const valibotResult= await validate (valibotSchema ,' 123' )
156
+ const zodResult= await validate (zodSchema ,' 123' )
146
157
```