Skip to content

Commit

Permalink
fix(zui): a ref identity is its uri (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklevasseur authored Sep 28, 2024
1 parent 4b1f571 commit 6bfeeae
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 13 deletions.
21 changes: 20 additions & 1 deletion zui/src/deref.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,19 @@ describe('dereference', () => {
const result = derefSchema.safeParse('astring')
expect(result.success).toBe(true)
})
test('should treat multiple references with the same uri as the same reference', () => {
const refSchema = z.object({
foo,
bar: z.ref('foo'),
baz: z.ref('foo'),
})
const derefSchema = refSchema.dereference(deref)
const result = derefSchema.safeParse({ foo: 'astring', bar: 'astring', baz: 'astring' })
expect(result.success).toBe(true)
})
})

describe('getRegerences', () => {
describe('getReferences', () => {
test('array', () => {
const refSchema = z.array(bar)
const refs = refSchema.getReferences()
Expand Down Expand Up @@ -204,4 +214,13 @@ describe('getRegerences', () => {
const refs = refSchema.getReferences()
expect(refs).toEqual(['foo', 'bar', 'baz'])
})
test('should treat multiple references with the same uri as the same reference', () => {
const refSchema = z.object({
foo,
bar: z.ref('foo'),
baz: z.ref('foo'),
})
const refs = refSchema.getReferences()
expect(refs).toEqual(['foo'])
})
})
3 changes: 2 additions & 1 deletion zui/src/z/types/discriminatedUnion/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodBranded,
ZodCatch,
Expand Down Expand Up @@ -104,7 +105,7 @@ export class ZodDiscriminatedUnion<
}

getReferences(): string[] {
return this.options.flatMap((option) => option.getReferences())
return unique(this.options.flatMap((option) => option.getReferences()))
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
defaultErrorMap,
getErrorMap,
Expand Down Expand Up @@ -55,7 +56,7 @@ export class ZodFunction<Args extends ZodTuple<any, any>, Returns extends ZodTyp
}

getReferences(): string[] {
return [...this._def.args.getReferences(), ...this._def.returns.getReferences()]
return unique([...this._def.args.getReferences(), ...this._def.returns.getReferences()])
}

_parse(input: ParseInput): ParseReturnType<any> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/intersection/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodIssueCode,
RawCreateParams,
Expand Down Expand Up @@ -85,7 +86,7 @@ export class ZodIntersection<T extends ZodTypeAny, U extends ZodTypeAny> extends
}

getReferences(): string[] {
return [...this._def.left.getReferences(), ...this._def.right.getReferences()]
return unique([...this._def.left.getReferences(), ...this._def.right.getReferences()])
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/map/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodIssueCode,
ParseInputLazyPath,
Expand Down Expand Up @@ -45,7 +46,7 @@ export class ZodMap<Key extends ZodTypeAny = ZodTypeAny, Value extends ZodTypeAn
}

getReferences(): string[] {
return [...this._def.keyType.getReferences(), ...this._def.valueType.getReferences()]
return unique([...this._def.keyType.getReferences(), ...this._def.valueType.getReferences()])
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodArray,
ZodEnum,
Expand Down Expand Up @@ -142,7 +143,7 @@ export class ZodObject<
for (const key in shape) {
refs.push(...shape[key]!.getReferences())
}
return refs
return unique(refs)
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/pipeline/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodFirstPartyTypeKind,
ZodType,
Expand Down Expand Up @@ -29,7 +30,7 @@ export class ZodPipeline<A extends ZodTypeAny, B extends ZodTypeAny> extends Zod
}

getReferences(): string[] {
return [...this._def.in.getReferences(), ...this._def.out.getReferences()]
return unique([...this._def.in.getReferences(), ...this._def.out.getReferences()])
}

_parse(input: ParseInput): ParseReturnType<any> {
Expand Down
3 changes: 2 additions & 1 deletion zui/src/z/types/record/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
BRAND,
ZodIssueCode,
Expand Down Expand Up @@ -59,7 +60,7 @@ export class ZodRecord<Key extends KeySchema = ZodString, Value extends ZodTypeA
}

getReferences(): string[] {
return [...this._def.keyType.getReferences(), ...this._def.valueType.getReferences()]
return unique([...this._def.keyType.getReferences(), ...this._def.valueType.getReferences()])
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
5 changes: 3 additions & 2 deletions zui/src/z/types/tuple/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
ZodIssueCode,
ParseInputLazyPath,
Expand Down Expand Up @@ -57,10 +58,10 @@ export class ZodTuple<
}

getReferences(): string[] {
return [
return unique([
...this._def.items.flatMap((item) => item.getReferences()),
...(this._def.rest ? this._def.rest.getReferences() : []),
]
])
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
9 changes: 6 additions & 3 deletions zui/src/z/types/union/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { unique } from '../../utils'
import {
RawCreateParams,
ZodFirstPartyTypeKind,
Expand Down Expand Up @@ -42,9 +43,11 @@ export class ZodUnion<T extends ZodUnionOptions> extends ZodType<
}

getReferences(): string[] {
return this._def.options.reduce<string[]>((acc, option) => {
return [...acc, ...option.getReferences()]
}, [])
return unique(
this._def.options.reduce<string[]>((acc, option) => {
return [...acc, ...option.getReferences()]
}, []),
)
}

_parse(input: ParseInput): ParseReturnType<this['_output']> {
Expand Down
3 changes: 3 additions & 0 deletions zui/src/z/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const unique = <T>(arr: T[]): T[] => {
return Array.from(new Set(arr))
}

0 comments on commit 6bfeeae

Please sign in to comment.