-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typeboxResolver): make TypeBox resolver work with compiled schema (
#674) * feat(typeboxResolver): make TypeBox resolver work with compiled schema * docs(typeboxResolver): add docs for using TypeBox resolver with compiled schema * chore: cleanup wrong indentation format
- Loading branch information
1 parent
c0d528b
commit e8e0f80
Showing
9 changed files
with
412 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
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 { typeboxResolver } from '..'; | ||
import { Type, Static } from '@sinclair/typebox'; | ||
import { TypeCompiler } from '@sinclair/typebox/compiler'; | ||
|
||
const schema = Type.Object({ | ||
username: Type.String({ minLength: 1 }), | ||
password: Type.String({ minLength: 1 }), | ||
}); | ||
|
||
const typecheck = TypeCompiler.Compile(schema) | ||
|
||
type FormData = Static<typeof schema> & { unusedProperty: string }; | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<FormData>({ | ||
resolver: typeboxResolver(typecheck), // Useful to check TypeScript regressions | ||
}); | ||
|
||
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 Typebox (with compiler) 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.getAllByText(/Expected string length greater or equal to 1/i), | ||
).toHaveLength(2); | ||
|
||
expect(handleSubmit).not.toHaveBeenCalled(); | ||
}); |
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,86 @@ | ||
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 { typeboxResolver } from '..'; | ||
|
||
import { Type, Static } from '@sinclair/typebox'; | ||
import { TypeCompiler } from '@sinclair/typebox/compiler'; | ||
|
||
const schema = Type.Object({ | ||
username: Type.String({ minLength: 1 }), | ||
password: Type.String({ minLength: 1 }), | ||
}); | ||
|
||
const typecheck = TypeCompiler.Compile(schema) | ||
|
||
type FormData = Static<typeof schema>; | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: typeboxResolver(typecheck), | ||
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 Typebox (with compiler)", 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( | ||
'Expected string length greater or equal to 1', | ||
); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe( | ||
'Expected string length greater or equal to 1', | ||
); | ||
|
||
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
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
Oops, something went wrong.