From fec4a426ceb8517132199ae1c4d71a189dd0d2bb Mon Sep 17 00:00:00 2001 From: ItMaga Date: Wed, 12 Apr 2023 11:22:11 +0300 Subject: [PATCH] fix: remove `instanceof` --- lib/generators/IntersectionGenerator.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/generators/IntersectionGenerator.ts b/lib/generators/IntersectionGenerator.ts index 6fca72c..a763d58 100644 --- a/lib/generators/IntersectionGenerator.ts +++ b/lib/generators/IntersectionGenerator.ts @@ -4,13 +4,15 @@ import type BaseGenerator from './BaseGenerator'; export default class IntersectionGenerator> implements BaseGenerator { public generate(schema: T) { - if (schema._def.left instanceof z.ZodUnion && schema._def.right instanceof z.ZodUnion) { - const sharedOptions = schema._def.left._def.options.filter((leftOption: z.ZodTypeAny) => { - return schema._def.right._def.options.some((rightOption: z.ZodTypeAny) => rightOption._def.typeName === leftOption._def.typeName); - }); + if ('typeName' in schema._def.left && 'typeName' in schema._def.right) { + if (schema._def.left.typeName === 'ZodUnion' && schema._def.right.typeName === 'ZodUnion') { + const sharedOptions = schema._def.left._def.options.filter((leftOption: z.ZodTypeAny) => { + return schema._def.right._def.options.some((rightOption: z.ZodTypeAny) => rightOption._def.typeName === leftOption._def.typeName); + }); - const mockGenerator = new MockGenerator(z.union(sharedOptions)); - return mockGenerator.generate(); + const mockGenerator = new MockGenerator(z.union(sharedOptions)); + return mockGenerator.generate(); + } } const leftGenerated = new MockGenerator(schema._def.left).generate();