Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(zui): handle optional unions without the need for never #485

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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
37 changes: 36 additions & 1 deletion zui/src/transforms/json-schema-to-zui/parsers/parseSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ import { parseNullable } from './parseNullable'
import { parseRef } from './parseRef'
import { ParserSelector, Refs, JsonSchemaObject, JsonSchema, Serializable, JSONSchemaExtended } from '../types'
import { parseDiscriminator } from './parseDiscriminator'
import { isEqual } from 'lodash'

export const parseSchema = (
schema: JSONSchemaExtended,
refs: Refs = { seen: new Map(), path: [] },
blockMeta?: boolean,
): string => {
if (typeof schema !== 'object') return schema ? 'z.any()' : 'z.never()'
if (its.anything(schema)) {
return 'z.any()'
}
if (its.nothing(schema)) {
/**
* Nothing in JSONSchema both means undefined and never in TypeScript
* We map to never for backwards compatibility
*/
return 'z.never()'
}

if (refs.parserOverride) {
const custom = refs.parserOverride(schema, refs)
Expand Down Expand Up @@ -121,6 +131,29 @@ const selectParser: ParserSelector = (schema, refs) => {
}
}

const anything = (x: JSONSchemaExtended): boolean => {
if (typeof x === 'boolean') {
return x
}
if (isEqual(x, {})) {
return true
}
if (x.not) {
return nothing(x.not)
}
return false
}

const nothing = (x: JSONSchemaExtended): boolean => {
if (typeof x === 'boolean') {
return !x
}
if (x.not) {
return anything(x.not)
}
return false
}

export const its = {
an: {
object: (x: JsonSchemaObject): x is JsonSchemaObject & { type: 'object' } => x.type === 'object',
Expand Down Expand Up @@ -178,4 +211,6 @@ export const its = {
} => x.oneOf !== undefined,
ref: (x: JsonSchemaObject): x is JsonSchemaObject & { $ref: string } => x.$ref !== undefined,
},
anything,
nothing,
}
2 changes: 2 additions & 0 deletions zui/src/transforms/json-schema-to-zui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export type JsonSchemaObject = {
const?: Serializable
enum?: Serializable[]

not?: JsonSchema

errorMessage?: { [key: string]: string | undefined }
[zuiKey]?: ZuiExtensionObject
} & { [key: string]: any }
Expand Down
31 changes: 31 additions & 0 deletions zui/src/transforms/transform-pipeline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { zodToJsonSchema } from './zui-to-json-schema'
import { jsonSchemaToZui } from './json-schema-to-zui'
import { toTypescriptSchema } from './zui-to-typescript-schema'
import { test, expect } from 'vitest'
import z from '../z'
import { evalZuiString } from './common/eval-zui-string'

/**
* This test file contains integration tests to ensure the multiple transforms can be chained together
*/

const transformAll = (originalSchema: z.ZodType): z.ZodType => {
const jsonSchema = zodToJsonSchema(originalSchema)
console.log(JSON.stringify(jsonSchema))
const zuiSchema = jsonSchemaToZui(jsonSchema)
const typescriptSchema = toTypescriptSchema(zuiSchema)
console.log(typescriptSchema)
const evalResult = evalZuiString(typescriptSchema)
if (evalResult.sucess === false) {
throw new Error(`Failed to evaluate zui schema "${typescriptSchema}"; ${evalResult.error}`)
}
return evalResult.value
}

test('optional union from one end to the other and back', () => {
const originalSchema = z.object({
foo: z.literal('42').or(z.literal(42)).optional().nullable(),
})
const destinationSchema = transformAll(originalSchema)
expect(originalSchema.isEqual(destinationSchema)).toBe(true)

Check failure on line 30 in zui/src/transforms/transform-pipeline.test.ts

View workflow job for this annotation

GitHub Actions / zui

src/transforms/transform-pipeline.test.ts > optional union from one end to the other and back

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/transforms/transform-pipeline.test.ts:30:53
})
Loading