Skip to content

Commit

Permalink
fix(zui): allow dereferencing a default schema (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklevasseur authored Oct 2, 2024
1 parent 7f96400 commit 0833358
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions zui/src/deref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ describe('dereference', () => {
const result = derefSchema.safeParse('astring')
expect(result.success).toBe(true)
})
test('default', () => {
const refSchema = z.array(foo).default([])
const derefSchema = refSchema.dereference(deref)

expect(derefSchema.safeParse(['astring']).success).toBe(true)
expect(derefSchema.safeParse([111111111]).success).toBe(false)
expect(derefSchema.safeParse(undefined).success).toBe(true)
})
test('should treat multiple references with the same uri as the same reference', () => {
const refSchema = z.object({
foo,
Expand Down
7 changes: 7 additions & 0 deletions zui/src/z/types/default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export class ZodDefault<T extends ZodTypeAny> extends ZodType<
return this._def.innerType
}

dereference(defs: Record<string, ZodTypeAny>): ZodTypeAny {
return new ZodDefault({
...this._def,
innerType: this._def.innerType.dereference(defs),
})
}

static create = <T extends ZodTypeAny>(
type: T,
params: RawCreateParams & {
Expand Down

0 comments on commit 0833358

Please sign in to comment.