diff --git a/packages-top/xsschema/package.json b/packages-top/xsschema/package.json index 8a9e1c6..216f8da 100644 --- a/packages-top/xsschema/package.json +++ b/packages-top/xsschema/package.json @@ -45,6 +45,7 @@ "@zod/mini": "^4.0.0-beta", "arktype": "catalog:schema", "effect": "catalog:schema", + "sury": "^10.0.0-rc", "zod": "^3.24.3 || ^4.0.0-beta", "zod-to-json-schema": "catalog:schema" }, @@ -61,6 +62,9 @@ "effect": { "optional": true }, + "sury": { + "optional": true + }, "zod": { "optional": true }, @@ -75,6 +79,7 @@ "@zod/mini": "^4.0.0-beta.20250420T053007", "arktype": "catalog:schema", "effect": "catalog:schema", + "sury": "^10.0.0-rc.4", "valibot": "catalog:schema", "zod": "catalog:schema", "zod-to-json-schema": "catalog:schema", diff --git a/packages-top/xsschema/src/to-json-schema/vendors/index.ts b/packages-top/xsschema/src/to-json-schema/vendors/index.ts index bcc40c2..5e85d40 100644 --- a/packages-top/xsschema/src/to-json-schema/vendors/index.ts +++ b/packages-top/xsschema/src/to-json-schema/vendors/index.ts @@ -19,6 +19,9 @@ export const getToJsonSchemaFn = async (vendor: string): Promise case 'effect': return import('./effect') .then(async ({ getToJsonSchemaFn }) => getToJsonSchemaFn()) + case 'sury': + return import('./sury') + .then(async ({ getToJsonSchemaFn }) => getToJsonSchemaFn()) case 'valibot': return import('./valibot') .then(async ({ getToJsonSchemaFn }) => getToJsonSchemaFn()) diff --git a/packages-top/xsschema/src/to-json-schema/vendors/sury.ts b/packages-top/xsschema/src/to-json-schema/vendors/sury.ts new file mode 100644 index 0000000..8028880 --- /dev/null +++ b/packages-top/xsschema/src/to-json-schema/vendors/sury.ts @@ -0,0 +1,10 @@ +import type { Schema } from 'sury' + +import type { ToJsonSchemaFn } from '.' + +import { tryImport } from '.' + +export const getToJsonSchemaFn = async (): Promise => { + const { toJSONSchema } = await tryImport(import('sury'), 'sury') + return schema => toJSONSchema((schema as Schema)) +} diff --git a/packages-top/xsschema/test/__snapshots__/to-json-schema.test.ts.snap b/packages-top/xsschema/test/__snapshots__/to-json-schema.test.ts.snap index f2c391c..c11c887 100644 --- a/packages-top/xsschema/test/__snapshots__/to-json-schema.test.ts.snap +++ b/packages-top/xsschema/test/__snapshots__/to-json-schema.test.ts.snap @@ -108,6 +108,60 @@ exports[`toJsonSchema > effect sync 1`] = ` } `; +exports[`toJsonSchema > sury 1`] = ` +{ + "additionalProperties": true, + "description": "My neat object schema", + "properties": { + "myString": { + "type": "string", + }, + "myUnion": { + "anyOf": [ + { + "type": "number", + }, + { + "type": "boolean", + }, + ], + }, + }, + "required": [ + "myString", + "myUnion", + ], + "type": "object", +} +`; + +exports[`toJsonSchema > sury sync 1`] = ` +{ + "additionalProperties": true, + "description": "My neat object schema", + "properties": { + "myString": { + "type": "string", + }, + "myUnion": { + "anyOf": [ + { + "type": "number", + }, + { + "type": "boolean", + }, + ], + }, + }, + "required": [ + "myString", + "myUnion", + ], + "type": "object", +} +`; + exports[`toJsonSchema > valibot 1`] = ` { "$schema": "http://json-schema.org/draft-07/schema#", diff --git a/packages-top/xsschema/test/to-json-schema.test.ts b/packages-top/xsschema/test/to-json-schema.test.ts index 335af34..fe777be 100644 --- a/packages-top/xsschema/test/to-json-schema.test.ts +++ b/packages-top/xsschema/test/to-json-schema.test.ts @@ -1,6 +1,7 @@ import { z as zm } from '@zod/mini' import { type } from 'arktype' import { Schema } from 'effect' +import * as S from 'sury' import * as v from 'valibot' import { describe, expect, it } from 'vitest' import { z } from 'zod' @@ -40,6 +41,16 @@ describe('toJsonSchema', () => { expect(jsonSchema).toMatchSnapshot() }) + it('sury', async () => { + const schema = S.schema({ + myString: S.string, + myUnion: S.union([S.number, S.boolean]), + }).with(S.meta, { description: 'My neat object schema' }) + + const jsonSchema = await toJsonSchema(schema) + expect(jsonSchema).toMatchSnapshot() + }) + it('valibot', async () => { const schema = v.pipe( v.object({ @@ -101,6 +112,17 @@ describe('toJsonSchema', () => { expect(jsonSchema).toMatchSnapshot() }) + it('sury sync', async () => { + const schema = S.schema({ + myString: S.string, + myUnion: S.union([S.number, S.boolean]), + }).with(S.meta, { description: 'My neat object schema' }) + + await initToJsonSchemaSyncVendor('sury') + const jsonSchema = toJsonSchemaSync(schema) + expect(jsonSchema).toMatchSnapshot() + }) + it('valibot sync', async () => { const schema = v.pipe( v.object({ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ce907c2..1442767 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -335,6 +335,9 @@ importers: effect: specifier: catalog:schema version: 3.14.5 + sury: + specifier: ^10.0.0-rc.4 + version: 10.0.0-rc.4 valibot: specifier: catalog:schema version: 1.0.0(typescript@5.6.3) @@ -5012,6 +5015,14 @@ packages: resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==} engines: {node: '>= 0.4'} + sury@10.0.0-rc.4: + resolution: {integrity: sha512-BnGcpxbhbtmJ2rNepTOIc8f1fFnVS2S7T5HLltFcmCNmij6iZq6JFERY0AxsdRd5PP0yp5FByiBngxo3TLokKQ==} + peerDependencies: + rescript: 11.x + peerDependenciesMeta: + rescript: + optional: true + sync-child-process@1.0.2: resolution: {integrity: sha512-8lD+t2KrrScJ/7KXCSyfhT3/hRq78rC0wBFqNJXv3mZyn6hW2ypM05JmlSvtqRbeq6jqA94oHbxAr2vYsJ8vDA==} engines: {node: '>=16.0.0'} @@ -10530,6 +10541,8 @@ snapshots: supports-preserve-symlinks-flag@1.0.0: {} + sury@10.0.0-rc.4: {} + sync-child-process@1.0.2: dependencies: sync-message-port: 1.1.3