generated from rtivital/ts-package-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Update zod4Resolver to support both zod/v4 and zod/v4-mini #6
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
Merged
rtivital
merged 2 commits into
mantinedev:master
from
tobigumo:feat/support-zod-v4-mini
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { z } from 'zod/v4-mini'; | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { useForm } from '@mantine/form'; | ||
import { ZodResolverOptions, zod4Resolver } from './zod-resolver'; | ||
|
||
const schema = z.object({ | ||
name: z.string().check(z.minLength(2, { message: 'Name should have at least 2 letters' })), | ||
email: z.string().check(z.email({ message: 'Invalid email' })), | ||
age: z.number().check(z.minimum(18, { message: 'You must be at least 18 to create an account' })), | ||
}); | ||
|
||
it('validates basic fields with given zod schema', () => { | ||
const hook = renderHook(() => | ||
useForm({ | ||
initialValues: { | ||
name: '', | ||
email: '', | ||
age: 16, | ||
}, | ||
validate: zod4Resolver(schema), | ||
}) | ||
); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({ | ||
name: 'Name should have at least 2 letters', | ||
email: 'Invalid email', | ||
age: 'You must be at least 18 to create an account', | ||
}); | ||
|
||
act(() => hook.result.current.setValues({ name: 'John', email: '[email protected]', age: 16 })); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({ | ||
age: 'You must be at least 18 to create an account', | ||
}); | ||
}); | ||
|
||
const nestedSchema = z.object({ | ||
nested: z.object({ | ||
field: z.string().check(z.minLength(2, { message: 'Field should have at least 2 letters' })), | ||
}), | ||
}); | ||
|
||
it('validates nested fields with given zod schema', () => { | ||
const hook = renderHook(() => | ||
useForm({ | ||
initialValues: { | ||
nested: { | ||
field: '', | ||
}, | ||
}, | ||
validate: zod4Resolver(nestedSchema), | ||
}) | ||
); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({ | ||
'nested.field': 'Field should have at least 2 letters', | ||
}); | ||
|
||
act(() => hook.result.current.setValues({ nested: { field: 'John' } })); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
}); | ||
|
||
const listSchema = z.object({ | ||
list: z.array( | ||
z.object({ | ||
name: z.string().check(z.minLength(2, { message: 'Name should have at least 2 letters' })), | ||
}) | ||
), | ||
}); | ||
|
||
it('validates list fields with given zod schema', () => { | ||
const hook = renderHook(() => | ||
useForm({ | ||
initialValues: { | ||
list: [{ name: '' }], | ||
}, | ||
validate: zod4Resolver(listSchema), | ||
}) | ||
); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({ | ||
'list.0.name': 'Name should have at least 2 letters', | ||
}); | ||
|
||
act(() => hook.result.current.setValues({ list: [{ name: 'John' }] })); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
}); | ||
|
||
const mandatoryHashMessage = 'There must be a # in the hashtag'; | ||
const notEmptyMessage = 'Hashtag should not be empty'; | ||
|
||
const multipleMessagesForAFieldSchema = z.object({ | ||
hashtag: z.string().check( | ||
z.refine((value) => value.length > 0, { | ||
message: notEmptyMessage, | ||
}), | ||
z.refine((value) => value.includes('#'), { | ||
message: mandatoryHashMessage, | ||
}) | ||
), | ||
}); | ||
|
||
it.each([ | ||
[ | ||
{ | ||
errorPriority: 'first', | ||
}, | ||
notEmptyMessage, | ||
], | ||
[ | ||
{ | ||
errorPriority: 'last', | ||
}, | ||
mandatoryHashMessage, | ||
], | ||
[undefined, mandatoryHashMessage], | ||
])( | ||
`provides the proper error for a schema with multiple messages for a field with resolver option %p`, | ||
(options, expectedErrorMessage) => { | ||
const hook = renderHook(() => | ||
useForm({ | ||
initialValues: { | ||
hashtag: '', | ||
}, | ||
validate: zod4Resolver(multipleMessagesForAFieldSchema, options as ZodResolverOptions), | ||
}) | ||
); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({}); | ||
act(() => hook.result.current.validate()); | ||
|
||
expect(hook.result.current.errors).toStrictEqual({ | ||
hashtag: expectedErrorMessage, | ||
}); | ||
} | ||
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid mutating the original
error.issues
array. Instead, create a shallow copy before reversing (e.g.const issues = [...error.issues].reverse()
) to prevent side effects on subsequent validations.Copilot uses AI. Check for mistakes.