Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/zui",
"version": "1.2.1",
"version": "1.2.2",
"description": "A fork of Zod with additional features",
"type": "module",
"source": "./src/index.ts",
Expand Down
20 changes: 20 additions & 0 deletions zui/src/transforms/zui-to-json-schema/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as errs from '../common/errors'
import z from '../../z'
import { describe, test, expect } from 'vitest'
import { toJSONSchema } from './index'
import { JSONSchema7 } from 'json-schema'

const asJsonSchema7 = <T extends JSONSchema7>(schema: T): T => schema

describe('zuiToJSONSchemaNext', () => {
test('should map ZodString to StringSchema', () => {
Expand Down Expand Up @@ -374,4 +377,21 @@ describe('zuiToJSONSchemaNext', () => {
const schema = toJSONSchema(z.ref('foo'))
expect(schema).toEqual({ $ref: 'foo' })
})

test('should preserve title and description of nullable schemas', () => {
const title = 'My Title'
const description = 'My Description'
const expected = asJsonSchema7({
anyOf: [{ type: 'string', description, 'x-zui': { title: title } }, { type: 'null' }],
'x-zui': {
def: { typeName: 'ZodNullable' },
},
})

const schema1 = toJSONSchema(z.string().nullable().title(title).describe(description))
expect(schema1).toEqual(expected)

const schema2 = toJSONSchema(z.string().title(title).describe(description).nullable())
expect(schema2).toEqual(expected)
})
})
2 changes: 1 addition & 1 deletion zui/src/transforms/zui-to-json-schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export function toJSONSchema(schema: z.Schema): json.Schema {
return {
anyOf: [toJSONSchema(def.innerType), nullSchema()],
'x-zui': {
...def['x-zui'],
// ...def['x-zui'],
def: { typeName: z.ZodFirstPartyTypeKind.ZodNullable },
},
} satisfies json.NullableSchema
Expand Down