Skip to content
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
17 changes: 9 additions & 8 deletions module/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"date-fns": "2.x",
"react": "18.x",
"react-dom": "18.x",
"zod": "3.*"
"zod": "^3.21.4 || ^4.0.0"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be "4*" as you've done a breaking change

},
"files": [
"dist"
Expand Down Expand Up @@ -100,7 +100,7 @@
"vite-plugin-static-copy": "0.16.0",
"vite-plugin-turbosnap": "1.0.3",
"wait-on": "7.0.1",
"zod": "3.21.4"
"zod": "^4.1.5"
},
"dependencies": {
"@radix-ui/react-checkbox": "1.0.4",
Expand Down
2 changes: 1 addition & 1 deletion module/src/form/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function useForm<TData extends object>(
let errors: IValidationError[] = [];

if (!results.success) {
errors = getMyZodErrors(results.error.errors, keyChainString);
errors = getMyZodErrors(results.error.issues, keyChainString);

if (errors.length && !silent) {
clientValidationDispatcher({ type: 'add-validation', errors });
Expand Down
37 changes: 17 additions & 20 deletions module/src/form/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import {
type ZodBigInt,
type ZodBoolean,
type ZodDate,
type ZodEffects,
type ZodTransform,
type ZodEnum,
type ZodLiteral,
type ZodNativeEnum,
type ZodNullable,
type ZodNumber,
type ZodObject,
Expand Down Expand Up @@ -498,12 +497,7 @@ export interface IArrayOfZod<TProp> {
/** A function which defines the validation to apply to the array itself (e.g. `opts: arr => arr.min(1).max(5)`) */
opts?: (
arr: ZodArray<TProp & ZodTypeAny>
) =>
| ZodArray<TProp & ZodTypeAny>
| ZodNullable<ZodTypeAny>
| ZodOptional<ZodTypeAny>
| ZodOptional<ZodNullable<ZodTypeAny>>
| ZodEffects<ZodTypeAny>;
) => ZodTypeAny;
}

/**
Expand All @@ -516,24 +510,27 @@ export interface IObjectOfZod<TProp> {
/** A function which defines the validation to apply to the object itself (e.g. `opts: ob => ob.required()`) */
opts?: (
ob: ZodObject<TProp & ZodRawShape>
) =>
| ZodObject<TProp & ZodRawShape>
| ZodNullable<ZodTypeAny>
| ZodOptional<ZodTypeAny>
| ZodOptional<ZodNullable<ZodTypeAny>>
| ZodEffects<ZodTypeAny>;
) => ZodTypeAny;
}

type WithZodAdditions<T extends ZodTypeAny, K> =
type WithZodAdditions<T extends ZodTypeAny, K extends string | number | boolean> =
| T
| ZodLiteral<K>
| ZodOptional<T>
| ZodNullable<T>
| ZodOptional<ZodNullable<T>>
| ZodEffects<T, K, K>
| ZodUnion<[WithZodAdditions<T, K>, ...ZodTypeAny[]]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any required for zod native enum
| ZodNativeEnum<any>
| ZodTransform<T, K>
| ZodUnion<[T, ...ZodTypeAny[]]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any required for zod enum
| ZodEnum<any>;

type WithZodAdditionsDate<T extends ZodTypeAny> =
| T
| ZodOptional<T>
| ZodNullable<T>
| ZodOptional<ZodNullable<T>>
| ZodTransform<T, Date>
| ZodUnion<[T, ...ZodTypeAny[]]>
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- any required for zod enum
| ZodEnum<any>;

Expand All @@ -548,7 +545,7 @@ export type ToZod<TProp> = {
bigint: ZodNullOrUndefined<TProp, WithZodAdditions<ZodBigInt, number>>;
number: ZodNullOrUndefined<TProp, WithZodAdditions<ZodNumber, number>>;
boolean: ZodNullOrUndefined<TProp, WithZodAdditions<ZodBoolean, boolean>>;
date: ZodNullOrUndefined<TProp, WithZodAdditions<ZodDate, Date>>;
date: ZodNullOrUndefined<TProp, WithZodAdditionsDate<ZodDate>>;
object: IObjectOfZod<{ [TKey in keyof TProp]: ToZod<TProp[TKey]> }>;
rest: never;
}[StringType<TProp>];
Expand Down
4 changes: 2 additions & 2 deletions module/src/form/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export function zodFromValidationSchema<TData>(schema: IRootValidationSchema<TDa
*/
export const getMyZodErrors = (errors: ZodIssue[], keyChainString?: string) => {
return errors
.filter(e => (keyChainString ? isMyKeyChainItem(keyStringFromKeyChain(e.path, 'dots'), keyChainString) : true))
.filter(e => (keyChainString ? isMyKeyChainItem(keyStringFromKeyChain(e.path as KeyChain, 'dots'), keyChainString) : true))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid casting?

.map(e => ({
key: keyStringFromKeyChain(e.path, 'dots'),
key: keyStringFromKeyChain(e.path as KeyChain, 'dots'),
message: e.message,
}));
};
Expand Down