-
-
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.
* feat: effect-ts resolver * refactor(effect-ts): replace spread operator with explicit assignment * fix(effect-ts): provide build aliases for globals * fix(effect-ts): include effect-ts in node-13-exports config * fix(ci): bumped workflow pnpm action setups to version 9 * docs(effect-ts): add quickstart guide to readme * refactor(effect-ts): optimize imports for better tree shaking, add encode generic, allow for async transforms --------- Co-authored-by: Trent Cox <[email protected]>
- Loading branch information
1 parent
1bfc6ab
commit 9ba2f89
Showing
16 changed files
with
5,004 additions
and
3,696 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
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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ const subRepositories = [ | |
'typebox', | ||
'arktype', | ||
'valibot', | ||
'effect-ts', | ||
]; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "@hookform/resolvers/effect-ts", | ||
"amdName": "hookformResolversEffectTs", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "React Hook Form validation resolver: effect-ts", | ||
"main": "dist/effect-ts.js", | ||
"module": "dist/effect-ts.module.js", | ||
"umd:main": "dist/effect-ts.umd.js", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"@hookform/resolvers": "^2.0.0", | ||
"@effect/schema": "^0.66.14", | ||
"effect": "^3.1.2", | ||
"react-hook-form": "^7.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,88 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
import { useForm } from 'react-hook-form'; | ||
import { effectTsResolver } from '..'; | ||
import { Schema } from '@effect/schema'; | ||
|
||
const USERNAME_REQUIRED_MESSAGE = 'username field is required'; | ||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; | ||
|
||
const schema = Schema.Struct({ | ||
username: Schema.String.pipe( | ||
Schema.nonEmpty({ message: () => USERNAME_REQUIRED_MESSAGE }), | ||
), | ||
password: Schema.String.pipe( | ||
Schema.nonEmpty({ message: () => PASSWORD_REQUIRED_MESSAGE }), | ||
), | ||
}); | ||
|
||
interface FormData { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: effectTsResolver(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 effect-ts", 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(USERNAME_REQUIRED_MESSAGE); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe(PASSWORD_REQUIRED_MESSAGE); | ||
|
||
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(''); | ||
}); |
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,59 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
import { useForm } from 'react-hook-form'; | ||
import { effectTsResolver } from '..'; | ||
import { Schema } from '@effect/schema'; | ||
|
||
const USERNAME_REQUIRED_MESSAGE = 'username field is required'; | ||
const PASSWORD_REQUIRED_MESSAGE = 'password field is required'; | ||
|
||
const schema = Schema.Struct({ | ||
username: Schema.String.pipe( | ||
Schema.nonEmpty({ message: () => USERNAME_REQUIRED_MESSAGE }), | ||
), | ||
password: Schema.String.pipe( | ||
Schema.nonEmpty({ message: () => PASSWORD_REQUIRED_MESSAGE }), | ||
), | ||
}); | ||
|
||
type FormData = Schema.Schema.Type<typeof schema>; | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm({ | ||
resolver: effectTsResolver(schema), | ||
}); | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<input {...register('username')} /> | ||
{errors.username && <span role="alert">{errors.username.message}</span>} | ||
|
||
<input {...register('password')} /> | ||
{errors.password && <span role="alert">{errors.password.message}</span>} | ||
|
||
<button type="submit">submit</button> | ||
</form> | ||
); | ||
} | ||
|
||
test("form's validation with Zod and TypeScript's integration", async () => { | ||
const handleSubmit = vi.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
expect(screen.queryAllByRole('alert')).toHaveLength(0); | ||
|
||
await user.click(screen.getByText(/submit/i)); | ||
|
||
expect(screen.getByText(/username field is required/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/password field is required/i)).toBeInTheDocument(); | ||
expect(handleSubmit).not.toHaveBeenCalled(); | ||
}); |
Oops, something went wrong.