Skip to content

Commit

Permalink
fix dumb dumb logic
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy committed Oct 30, 2024
1 parent f0cfee4 commit 6b71240
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 68 deletions.
54 changes: 0 additions & 54 deletions src/create/schema/parsers/intersection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,4 @@ describe('createIntersectionSchema', () => {

expect(result).toEqual(expected);
});

it('handles z.undefined() option', () => {
const expected: Schema = {
type: 'schema',
schema: {
allOf: [
{
type: 'string',
},
],
},
};
const schema = z.intersection(z.undefined(), z.string());

const result = createIntersectionSchema(schema, createOutputState());

expect(result).toEqual(expected);
});

it('handles z.literal(undefined) option', () => {
const expected: Schema = {
type: 'schema',
schema: {
allOf: [
{
type: 'string',
},
],
},
};
const schema = z.intersection(z.string(), z.literal(undefined));

const result = createIntersectionSchema(schema, createOutputState());

expect(result).toEqual(expected);
});

it('handles z.never() option', () => {
const expected: Schema = {
type: 'schema',
schema: {
allOf: [
{
type: 'string',
},
],
},
};
const schema = z.intersection(z.string(), z.never());

const result = createIntersectionSchema(schema, createOutputState());

expect(result).toEqual(expected);
});
});
21 changes: 8 additions & 13 deletions src/create/schema/parsers/intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createSchemaObject,
} from '../../schema';

import { isOptionalObjectKey } from './optional';
import { flattenEffects } from './transform';

export const createIntersectionSchema = <
Expand All @@ -16,22 +15,18 @@ export const createIntersectionSchema = <
zodIntersection: ZodIntersection<T, U>,
state: SchemaState,
): Schema => {
const left = !isOptionalObjectKey(zodIntersection._def.left)
? createSchemaObject(zodIntersection._def.left, state, [
'intersection left',
])
: undefined;
const right = !isOptionalObjectKey(zodIntersection._def.right)
? createSchemaObject(zodIntersection._def.right, state, [
'intersection right',
])
: undefined;
const left = createSchemaObject(zodIntersection._def.left, state, [
'intersection left',
]);
const right = createSchemaObject(zodIntersection._def.right, state, [
'intersection right',
]);

return {
type: 'schema',
schema: {
allOf: [...(left ? [left.schema] : []), ...(right ? [right.schema] : [])],
allOf: [left.schema, right.schema],
},
effects: flattenEffects([left?.effects, right?.effects]),
effects: flattenEffects([left.effects, right.effects]),
};
};
1 change: 0 additions & 1 deletion src/create/schema/parsers/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ describe('required', () => {
.transform((str) => str?.length)
.pipe(z.number().optional()),
m: z.string().default('a'),
n: z.intersection(z.literal(undefined), z.number()),
});

const result = createObjectSchema(schema, createInputState());
Expand Down

0 comments on commit 6b71240

Please sign in to comment.