Skip to content

feat(v3): Migrate to Zod V4 and improve zod-utils #584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: v3-main
Choose a base branch
from
Draft
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
132 changes: 132 additions & 0 deletions app/lib/zod/zod-utils-v4.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { describe, expect, test } from 'vitest';

import { z } from './zod-utils-v4';

describe('z.string', () => {
describe('nonEmpty', () => {
test.each([
{ input: 'string', output: 'string', expected: true },
{ input: undefined, expected: false },
{ input: '', expected: false },
{ input: null, expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.string().nonEmpty().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});

describe('nonEmptyNullable', () => {
test.each([
{ input: 'string', output: 'string', expected: true },
{ input: undefined, expected: false },
{ input: '', output: null, expected: true },
{ input: null, output: null, expected: true },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.string().nonEmptyNullable().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});

describe('nonEmptyNullish', () => {
test.each([
{ input: 'string', output: 'string', expected: true },
{ input: undefined, output: undefined, expected: true },
{ input: '', output: null, expected: true },
{ input: null, output: null, expected: true },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.string().nonEmptyNullish().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});

describe('email.strict', () => {
test.each([
{ input: 'name@company.com', output: 'name@company.com', expected: true },
{ input: undefined, expected: false },
{ input: '', expected: false },
{ input: 'company.com', expected: false },
{ input: null, expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.email().strict().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});

describe('email.strictNullable', () => {
test.each([
{ input: 'name@company.com', output: 'name@company.com', expected: true },
{ input: '', output: null, expected: true },
{ input: null, output: null, expected: true },
{ input: 'company.com', expected: false },
{ input: undefined, expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.email().strictNullable().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});

describe('email.strictNullish', () => {
test.each([
{ input: 'name@company.com', output: 'name@company.com', expected: true },
{ input: undefined, output: undefined, expected: true },
{ input: '', output: null, expected: true },
{ input: null, output: null, expected: true },
{ input: 'company.com', expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.email().strictNullish().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toBe(output);
}
});
});
});

describe('zu.array', () => {
describe('nonEmptyNullable', () => {
test.each([
{ input: ['string'], output: ['string'], expected: true },
{ input: [''], output: [''], expected: true },
{ input: [], output: null, expected: true },
{ input: undefined, expected: false },
{ input: [2], expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.array(z.string()).nonEmptyNullable().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toStrictEqual(output);
}
});
});

describe('nonEmptyNullish', () => {
test.each([
{ input: ['string'], output: ['string'], expected: true },
{ input: [''], output: [''], expected: true },
{ input: [], output: null, expected: true },
{ input: undefined, output: undefined, expected: true },
{ input: [2], expected: false },
])('with $input should be $expected', ({ input, output, expected }) => {
const parsed = z.array(z.string()).nonEmptyNullish().safeParse(input);
expect(parsed.success).toBe(expected);
if (parsed.success) {
expect(parsed.data).toStrictEqual(output);
}
});
});
});
72 changes: 72 additions & 0 deletions app/lib/zod/zod-utils-v4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { z as zod } from 'zod/v4';

/** Import default methods from zod */
const { string, array, email, ...rest } = zod;

type NonEmptyValidatorParams = string | zod.core.$ZodCheckMinLengthParams;

const emptyAsNullString = () => {
return zod.literal('').transform(() => null);
};

const emptyAsNullArray =
<T extends zod.core.$ZodType>(a: zod.ZodArray<T>) =>
() => {
return a.length(0).transform(() => null);
};

/**
* We augment the existing zod API with new custom validations
*/
export const z = {
...rest,
string: (params?: string | zod.core.$ZodStringParams) => {
const base = string(params);
const stringNonEmpty = base.trim().nonempty;
return Object.assign(base, {
emptyAsNull: emptyAsNullString,
nonEmpty: (params?: NonEmptyValidatorParams) => {
return stringNonEmpty(params);
},
nonEmptyNullable: (params?: NonEmptyValidatorParams) => {
return emptyAsNullString().or(stringNonEmpty(params).nullable());
},
nonEmptyNullish: (params?: NonEmptyValidatorParams) => {
return emptyAsNullString().or(stringNonEmpty(params).nullish());
},
});
},
email: (params?: string | zod.core.$ZodEmailParams) => {
const base = email(params);
const emailNonEmpty = email(params).toLowerCase().trim().nonempty;
return Object.assign(base, {
emptyAsNull: emptyAsNullString,
strict: (params?: NonEmptyValidatorParams) => {
return emailNonEmpty(params);
},
strictNullable: (params?: NonEmptyValidatorParams) => {
return emptyAsNullString().or(emailNonEmpty(params).nullable());
},
strictNullish: (params?: NonEmptyValidatorParams) => {
return emptyAsNullString().or(emailNonEmpty(params).nullish());
},
});
},
array: <T extends zod.core.$ZodType>(
element: T,
params?: string | zod.core.$ZodArrayParams
) => {
const base = array(element, params);
const emptyAsNull = emptyAsNullArray(base);
const arrayNonEmpty = base.nonempty;
return Object.assign(base, {
emptyAsNull,
nonEmptyNullable: (params?: NonEmptyValidatorParams) => {
return emptyAsNull().or(arrayNonEmpty(params).nullable());
},
nonEmptyNullish: (params?: NonEmptyValidatorParams) => {
return emptyAsNull().or(arrayNonEmpty(params).nullish());
},
});
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"tw-animate-css": "1.2.9",
"vaul": "1.1.2",
"vinxi": "0.5.6",
"zod": "3.24.4",
"zod": "3.25.17",
"zod-i18n-map": "2.27.0",
"zustand": "5.0.4"
},
Expand Down
Loading
Loading