-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
651 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ const subRepositories = [ | |
'arktype', | ||
'valibot', | ||
'effect-ts', | ||
'vine', | ||
]; | ||
|
||
const copySrc = () => { | ||
|
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,18 @@ | ||
{ | ||
"name": "@hookform/resolvers/vine", | ||
"amdName": "hookformResolversVine", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "React Hook Form validation resolver: vine", | ||
"main": "dist/vine.js", | ||
"module": "dist/vine.module.js", | ||
"umd:main": "dist/vine.umd.js", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"react-hook-form": "^7.0.0", | ||
"@hookform/resolvers": "^2.0.0", | ||
"@vinejs/vine": "^2.0.0" | ||
} | ||
} |
This file contains 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,81 @@ | ||
import React from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { render, screen } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
import { vineResolver } from '..'; | ||
import vine from '@vinejs/vine'; | ||
import { Infer } from '@vinejs/vine/build/src/types'; | ||
|
||
const schema = vine.compile( | ||
vine.object({ | ||
username: vine.string().minLength(1), | ||
password: vine.string().minLength(1), | ||
}), | ||
); | ||
|
||
type FormData = Infer<typeof schema> & { unusedProperty: string }; | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: vineResolver(schema), | ||
shouldUseNativeValidation: true, | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} placeholder="username" /> | ||
|
||
<input {...register('password')} placeholder="password" /> | ||
|
||
<button type="submit">submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
test("form's native validation with Zod", async () => { | ||
const handleSubmit = vi.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
// username | ||
let usernameField = screen.getByPlaceholderText( | ||
/username/i, | ||
) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(true); | ||
expect(usernameField.validationMessage).toBe(''); | ||
|
||
// password | ||
let passwordField = screen.getByPlaceholderText( | ||
/password/i, | ||
) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(true); | ||
expect(passwordField.validationMessage).toBe(''); | ||
|
||
await user.click(screen.getByText(/submit/i)); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(false); | ||
expect(usernameField.validationMessage).toBe('The username field must have at least 1 characters'); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe('The password field must have at least 1 characters'); | ||
|
||
await user.type(screen.getByPlaceholderText(/username/i), 'joe'); | ||
await user.type(screen.getByPlaceholderText(/password/i), 'password'); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(true); | ||
expect(usernameField.validationMessage).toBe(''); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(true); | ||
expect(passwordField.validationMessage).toBe(''); | ||
}); |
Oops, something went wrong.