Skip to content

Commit 544b20c

Browse files
fix(zui): export json types (#629)
Co-authored-by: Francois Levasseur <[email protected]>
1 parent 363b99a commit 544b20c

File tree

9 files changed

+24
-30
lines changed

9 files changed

+24
-30
lines changed

zui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bpinternal/zui",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A fork of Zod with additional features",
55
"type": "module",
66
"source": "./src/index.ts",

zui/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { JSONSchema7 } from 'json-schema'
22

3+
export * as json from './transforms/common/json-schema'
34
export * as transforms from './transforms'
45
export * from './z'

zui/src/transforms/common/json-schema.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,38 +62,32 @@ type _BooleanSchema = util.Satisfies<{ type: 'boolean' }, JSONSchema7>
6262
type _NullSchema = util.Satisfies<{ type: 'null' }, JSONSchema7>
6363
type _UndefinedSchema = util.Satisfies<{ not: true }, JSONSchema7>
6464
type _NeverSchema = util.Satisfies<{ not: true }, JSONSchema7>
65-
type _ArraySchema = util.Satisfies<
66-
{ type: 'array'; items: ZuiJSONSchema; minItems?: number; maxItems?: number },
67-
JSONSchema7
68-
>
69-
type _UnionSchema = util.Satisfies<{ anyOf: ZuiJSONSchema[] }, JSONSchema7>
70-
type _DiscriminatedUnionSchema = util.Satisfies<{ anyOf: ZuiJSONSchema[] }, JSONSchema7>
71-
type _IntersectionSchema = util.Satisfies<{ allOf: ZuiJSONSchema[] }, JSONSchema7>
65+
type _ArraySchema = util.Satisfies<{ type: 'array'; items: Schema; minItems?: number; maxItems?: number }, JSONSchema7>
66+
type _UnionSchema = util.Satisfies<{ anyOf: Schema[] }, JSONSchema7>
67+
type _DiscriminatedUnionSchema = util.Satisfies<{ anyOf: Schema[] }, JSONSchema7>
68+
type _IntersectionSchema = util.Satisfies<{ allOf: Schema[] }, JSONSchema7>
7269
type _SetSchema = util.Satisfies<
73-
{ type: 'array'; items: ZuiJSONSchema; uniqueItems: true; minItems?: number; maxItems?: number },
70+
{ type: 'array'; items: Schema; uniqueItems: true; minItems?: number; maxItems?: number },
7471
JSONSchema7
7572
>
7673
type _EnumSchema = util.Satisfies<{ type: 'string'; enum: string[] }, JSONSchema7>
7774
type _RefSchema = util.Satisfies<{ $ref: string }, JSONSchema7>
7875
type _ObjectSchema = util.Satisfies<
7976
{
8077
type: 'object'
81-
properties: { [key: string]: ZuiJSONSchema }
82-
additionalProperties?: ZuiJSONSchema | boolean
78+
properties: { [key: string]: Schema }
79+
additionalProperties?: Schema | boolean
8380
required?: string[]
8481
},
8582
JSONSchema7
8683
>
87-
type _TupleSchema = util.Satisfies<
88-
{ type: 'array'; items: ZuiJSONSchema[]; additionalItems?: ZuiJSONSchema },
89-
JSONSchema7
90-
>
91-
type _RecordSchema = util.Satisfies<{ type: 'object'; additionalProperties: ZuiJSONSchema }, JSONSchema7>
84+
type _TupleSchema = util.Satisfies<{ type: 'array'; items: Schema[]; additionalItems?: Schema }, JSONSchema7>
85+
type _RecordSchema = util.Satisfies<{ type: 'object'; additionalProperties: Schema }, JSONSchema7>
9286
type _LiteralStringSchema = util.Satisfies<{ type: 'string'; const: string }, JSONSchema7>
9387
type _LiteralNumberSchema = util.Satisfies<{ type: 'number'; const: number }, JSONSchema7>
9488
type _LiteralBooleanSchema = util.Satisfies<{ type: 'boolean'; const: boolean }, JSONSchema7>
95-
type _OptionalSchema = util.Satisfies<{ anyOf: [ZuiJSONSchema, UndefinedSchema] }, JSONSchema7>
96-
type _NullableSchema = util.Satisfies<{ anyOf: [ZuiJSONSchema, NullSchema] }, JSONSchema7>
89+
type _OptionalSchema = util.Satisfies<{ anyOf: [Schema, UndefinedSchema] }, JSONSchema7>
90+
type _NullableSchema = util.Satisfies<{ anyOf: [Schema, NullSchema] }, JSONSchema7>
9791

9892
export type StringSchema = _StringSchema & BaseZuiJSONSchema
9993
export type NumberSchema = _NumberSchema & BaseZuiJSONSchema
@@ -118,10 +112,12 @@ export type LiteralNumberSchema = _LiteralNumberSchema & BaseZuiJSONSchema
118112
export type LiteralBooleanSchema = _LiteralBooleanSchema & BaseZuiJSONSchema
119113
export type OptionalSchema = _OptionalSchema & BaseZuiJSONSchema<OptionalDef>
120114
export type NullableSchema = _NullableSchema & BaseZuiJSONSchema<NullableDef>
121-
122115
export type LiteralSchema = LiteralStringSchema | LiteralNumberSchema | LiteralBooleanSchema
123116

124-
export type ZuiJSONSchema =
117+
/**
118+
* Zui flavored JSON Schema; a subset of JSONSchema7 that includes Zui extensions
119+
*/
120+
export type Schema =
125121
| StringSchema
126122
| NumberSchema
127123
| BooleanSchema

zui/src/transforms/zui-from-json-schema/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import z from '../../z'
22
import { describe, test, expect } from 'vitest'
33
import { fromJSONSchema } from './index'
44
import { JSONSchema7 } from 'json-schema'
5-
import { ZuiJSONSchema } from '../common/json-schema'
5+
import { Schema as ZuiJSONSchema } from '../common/json-schema'
66

77
const buildSchema = (s: JSONSchema7, xZui: ZuiJSONSchema['x-zui'] = undefined): JSONSchema7 => {
88
return { ...s, 'x-zui': xZui } as JSONSchema7

zui/src/transforms/zui-to-json-schema/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { zodTupleToJsonTuple } from './type-processors/tuple'
1212
* @param schema zui schema
1313
* @returns ZUI flavored JSON schema
1414
*/
15-
export function toJSONSchema(schema: z.Schema): json.ZuiJSONSchema {
15+
export function toJSONSchema(schema: z.Schema): json.Schema {
1616
const schemaTyped = schema as z.ZodFirstPartySchemaTypes
1717
const def = schemaTyped._def
1818

@@ -80,7 +80,7 @@ export function toJSONSchema(schema: z.Schema): json.ZuiJSONSchema {
8080
const required = requiredProperties.length ? requiredProperties.map(([key]) => key) : undefined
8181
const properties = shape
8282
.map(([key, value]) => [key, value.mandatory()] satisfies [string, z.ZodType])
83-
.map(([key, value]) => [key, toJSONSchema(value)] satisfies [string, json.ZuiJSONSchema])
83+
.map(([key, value]) => [key, toJSONSchema(value)] satisfies [string, json.Schema])
8484

8585
let additionalProperties: json.ObjectSchema['additionalProperties'] = false
8686
if (def.unknownKeys instanceof z.ZodType) {

zui/src/transforms/zui-to-json-schema/type-processors/array.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as json from '../../common/json-schema'
44

55
export const zodArrayToJsonArray = (
66
zodArray: z.ZodArray,
7-
toSchema: (x: z.ZodTypeAny) => json.ZuiJSONSchema,
7+
toSchema: (x: z.ZodTypeAny) => json.Schema,
88
): json.ArraySchema => {
99
const schema: json.ArraySchema = {
1010
type: 'array',

zui/src/transforms/zui-to-json-schema/type-processors/set.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import { zuiKey } from '../../../ui/constants'
22
import z from '../../../z'
33
import * as json from '../../common/json-schema'
44

5-
export const zodSetToJsonSet = (
6-
zodSet: z.ZodSet,
7-
toSchema: (x: z.ZodTypeAny) => json.ZuiJSONSchema,
8-
): json.SetSchema => {
5+
export const zodSetToJsonSet = (zodSet: z.ZodSet, toSchema: (x: z.ZodTypeAny) => json.Schema): json.SetSchema => {
96
const schema: json.SetSchema = {
107
type: 'array',
118
description: zodSet.description,

zui/src/transforms/zui-to-json-schema/type-processors/tuple.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as json from '../../common/json-schema'
44

55
export const zodTupleToJsonTuple = (
66
zodTuple: z.ZodTuple,
7-
toSchema: (x: z.ZodTypeAny) => json.ZuiJSONSchema,
7+
toSchema: (x: z.ZodTypeAny) => json.Schema,
88
): json.TupleSchema => {
99
const schema: json.TupleSchema = {
1010
type: 'array',

zui/src/z/types/basetype/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { CatchFn } from '../catch'
4545
import { toTypescriptType, TypescriptGenerationOptions } from '../../../transforms/zui-to-typescript-type'
4646
import { toTypescriptSchema } from '../../../transforms/zui-to-typescript-schema'
4747
import { toJSONSchema } from '../../../transforms/zui-to-json-schema'
48-
import { ZuiJSONSchema } from '../../../transforms/common/json-schema'
48+
import { Schema as ZuiJSONSchema } from '../../../transforms/common/json-schema'
4949

5050
/**
5151
* This type is not part of the original Zod library, it's been added in Zui to:

0 commit comments

Comments
 (0)