Skip to content
Merged
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
75 changes: 75 additions & 0 deletions packages/zod/src/compatibleV4.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
getZodDateFormat,
getZodTimeFormat,
getZodDateTimeFormat,
getParameterFunctions,
getObjectFunctionName,
} from './compatibleV4';

describe('isZodVersionV4', () => {
Expand Down Expand Up @@ -87,3 +89,76 @@ describe('getZodDateTimeFormat', () => {
expect(getZodDateTimeFormat(false)).toBe('datetime');
});
});

describe('getParameterFunctions', () => {
const parameters = { test: 'value' };

describe('when isZodV4 is true', () => {
describe('when strict is true', () => {
it('should return [["strictObject", parameters]]', () => {
const result = getParameterFunctions(true, true, parameters);
expect(result).toEqual([['strictObject', parameters]]);
});
});

describe('when strict is false', () => {
it('should return [["object", parameters]]', () => {
const result = getParameterFunctions(true, false, parameters);
expect(result).toEqual([['object', parameters]]);
});
});
});

describe('when isZodV4 is false', () => {
describe('when strict is true', () => {
it('should return [["object", parameters], ["strict", undefined]]', () => {
const result = getParameterFunctions(false, true, parameters);
expect(result).toEqual([
['object', parameters],
['strict', undefined],
]);
});
});

describe('when strict is false', () => {
it('should return [["object", parameters]]', () => {
const result = getParameterFunctions(false, false, parameters);
expect(result).toEqual([['object', parameters]]);
});
});
});
});

describe('getObjectFunctionName', () => {
describe('when isZodV4 is true', () => {
describe('when strict is true', () => {
it('should return "strictObject"', () => {
const result = getObjectFunctionName(true, true);
expect(result).toBe('strictObject');
});
});

describe('when strict is false', () => {
it('should return "object"', () => {
const result = getObjectFunctionName(true, false);
expect(result).toBe('object');
});
});
});

describe('when isZodV4 is false', () => {
describe('when strict is true', () => {
it('should return "object"', () => {
const result = getObjectFunctionName(false, true);
expect(result).toBe('object');
});
});

describe('when strict is false', () => {
it('should return "object"', () => {
const result = getObjectFunctionName(false, false);
expect(result).toBe('object');
});
});
});
});
21 changes: 21 additions & 0 deletions packages/zod/src/compatibleV4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,24 @@ export const getZodTimeFormat = (isZodV4: boolean) => {
export const getZodDateTimeFormat = (isZodV4: boolean) => {
return isZodV4 ? 'iso.datetime' : 'datetime';
};

export const getParameterFunctions = (
isZodV4: boolean,
strict: boolean,
parameters: Record<string, any>,
): [string, any][] => {
if (isZodV4 && strict) {
return [['strictObject', parameters]];
} else {
return strict
? [
['object', parameters],
['strict', undefined],
]
: [['object', parameters]];
}
};

export const getObjectFunctionName = (isZodV4: boolean, strict: boolean) => {
return isZodV4 && strict ? 'strictObject' : 'object';
};
Loading