Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit89234cb

Browse files
feat(xsschema): addtoJsonSchemaSync (#114)
* feat: make xsschema.toJsonSchema sync and add registerStandardSchemaVendor* Add test for `toJsonSchemaSync` with `effect` vendorThis commit adds a new test case for the `toJsonSchemaSync` functionusing the `effect` vendor. The test verifies the conversion of aspecific schema to JSON schema format.* fix: missing sync exports
1 parent5d55de2 commit89234cb

File tree

16 files changed

+326
-62
lines changed

16 files changed

+326
-62
lines changed

‎docs/content/docs/packages-top/xsschema.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,39 @@ const arktypeResult = await validate(arktypeSchema, '123')
108108
const valibotResult=awaitvalidate(valibotSchema,'123')
109109
const zodResult=awaitvalidate(zodSchema,'123')
110110
```
111+
112+
##Synchronous Usage
113+
114+
`toJsonSchema` is async (because it has to`await import` the vendor's dependencies), but we also support a synchronous version:`toJsonSchemaSync`.
115+
116+
<Callouttype="important">
117+
Before using`toJsonSchemaSync`, you must call`registerStandardSchemaVendor` with your schema vendor.
118+
</Callout>
119+
120+
```ts twoslash
121+
import {registerStandardSchemaVendor }from'xsschema'
122+
123+
awaitregisterStandardSchemaVendor('zod')
124+
// or
125+
awaitregisterStandardSchemaVendor('valibot')
126+
// or
127+
awaitregisterStandardSchemaVendor('arktype')
128+
```
129+
130+
You can register multiple schema vendors if you need to.
131+
132+
###toJsonSchemaSync (zod)
133+
134+
```ts twoslash
135+
import {toJsonSchemaSync,registerStandardSchemaVendor }from'xsschema'
136+
import*aszfrom'zod'
137+
138+
awaitregisterStandardSchemaVendor('zod')
139+
140+
const schema=z.object({
141+
myString:z.string(),
142+
myUnion:z.union([z.number(),z.boolean()]),
143+
}).describe('My neat object schema')
144+
145+
const jsonSchema=toJsonSchemaSync(schema)
146+
```

‎packages-top/xsschema/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export{toJsonSchema,toJsonSchemaastoJSONSchema}from'./to-json-schema'
2-
exporttype{Infer,InferIn,JSONSchemaasJsonSchema,JSONSchema,Schema}from'./types'
1+
export{toJsonSchema,toJsonSchemaastoJSONSchema,toJsonSchemaSync,toJsonSchemaSyncastoJSONSchemaSync}from'./to-json-schema'
2+
exporttype{Infer,InferIn,JSONSchemaasJsonSchema,JSONSchema,Schema,StandardSchemaVendor}from'./types'
33
export{validate}from'./validate'
4+
export{registerStandardSchemaVendor,registerStandardSchemaVendors}from'./vendors'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
importtype{StandardSchemaV1}from'@standard-schema/spec'
2+
importtype{JSONSchema7}from'json-schema'
3+
4+
importtype{StandardSchemaVendor}from'./types'
5+
6+
import{getVendorToJsonSchemaFunction,registerStandardSchemaVendor}from'./vendors'
7+
8+
/**
9+
* Converts a Standard Schema to a JSON schema synchronously.
10+
*
11+
*@note Make sure to call `registerStandardSchemaVendor` with the appropriate vendor before calling this function, or you can call `toJsonSchema` instead which is async.
12+
*/
13+
exportconsttoJsonSchemaSync=(schema:StandardSchemaV1):JSONSchema7=>{
14+
const{ vendor}=schema['~standard']
15+
consttoJsonSchema=getVendorToJsonSchemaFunction(vendorasStandardSchemaVendor)
16+
if(!toJsonSchema){
17+
thrownewError(`xsschema: Unregistered or unsupported schema vendor "${vendor}". Make sure to register the vendor using "await registerStandardSchemaVendor('${vendor}')" before calling toJsonSchema.`)
18+
}
19+
20+
returntoJsonSchema(schema)
21+
}
22+
23+
/**
24+
* Converts a Standard Schema to a JSON schema.
25+
*
26+
*@note This method is `async` because it has to `await import` the schema vendor's dependencies.
27+
*/
28+
exportconsttoJsonSchema=async(schema:StandardSchemaV1):Promise<JSONSchema7>=>{
29+
const{ vendor}=schema['~standard']
30+
awaitregisterStandardSchemaVendor(vendorasStandardSchemaVendor)
31+
returntoJsonSchemaSync(schema)
32+
}

‎packages-top/xsschema/src/to-json-schema/arktype.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎packages-top/xsschema/src/to-json-schema/effect.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎packages-top/xsschema/src/to-json-schema/index.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎packages-top/xsschema/src/to-json-schema/valibot.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎packages-top/xsschema/src/to-json-schema/zod.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎packages-top/xsschema/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
importtype{StandardSchemaV1}from'@standard-schema/spec'
2+
importtype{JSONSchema7}from'json-schema'
23

34
exporttype{StandardSchemaV1asSchema}from'@standard-schema/spec'
45
exporttype{JSONSchema7asJSONSchema}from'json-schema'
56

67
exporttypeInfer<TextendsStandardSchemaV1>=StandardSchemaV1.InferOutput<T>
78
exporttypeInferIn<TextendsStandardSchemaV1>=StandardSchemaV1.InferInput<T>
9+
10+
exporttypeStandardSchemaToJsonSchemaFunction=(schema:StandardSchemaV1)=>JSONSchema7
11+
12+
exporttypeStandardSchemaVendor='arktype'|'effect'|'valibot'|'zod'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
importtype{StandardSchemaV1}from'@standard-schema/spec'
2+
importtype{Type}from'arktype'
3+
importtype{JSONSchema7}from'json-schema'
4+
5+
exportconsttoJsonSchema=(schema:StandardSchemaV1):JSONSchema7=>
6+
(schemaasType).toJsonSchema()asJSONSchema7

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp