-
-
Notifications
You must be signed in to change notification settings - Fork 161
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
459 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ const subRepositories = [ | |
'io-ts', | ||
'nope', | ||
'computed-types', | ||
'typanion' | ||
]; | ||
|
||
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
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": "typanion", | ||
"amdName": "hookformResolversTypanion", | ||
"version": "1.0.0", | ||
"private": true, | ||
"description": "React Hook Form validation resolver: typanion", | ||
"main": "dist/typanion.js", | ||
"module": "dist/typanion.module.js", | ||
"umd:main": "dist/typanion.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", | ||
"typanion": "^3.3.2" | ||
} | ||
} |
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, act } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
import { useForm } from 'react-hook-form'; | ||
import * as t from 'typanion'; | ||
import { typanionResolver } from '..'; | ||
|
||
const ERROR_MESSAGE = 'Expected to have a length of at least 1 elements (got 0)'; | ||
|
||
const schema = t.isObject({ | ||
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]), | ||
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]), | ||
}); | ||
|
||
interface FormData { | ||
unusedProperty: string; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { register, handleSubmit } = useForm<FormData>({ | ||
resolver: typanionResolver(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 Typanion", async () => { | ||
const handleSubmit = jest.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 act(async () => { | ||
user.click(screen.getByText(/submit/i)); | ||
}); | ||
|
||
// username | ||
usernameField = screen.getByPlaceholderText(/username/i) as HTMLInputElement; | ||
expect(usernameField.validity.valid).toBe(false); | ||
expect(usernameField.validationMessage).toBe(ERROR_MESSAGE); | ||
|
||
// password | ||
passwordField = screen.getByPlaceholderText(/password/i) as HTMLInputElement; | ||
expect(passwordField.validity.valid).toBe(false); | ||
expect(passwordField.validationMessage).toBe(ERROR_MESSAGE); | ||
|
||
await act(async () => { | ||
user.type(screen.getByPlaceholderText(/username/i), 'joe'); | ||
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,57 @@ | ||
import React from 'react'; | ||
import { render, screen, act } from '@testing-library/react'; | ||
import user from '@testing-library/user-event'; | ||
import { useForm } from 'react-hook-form'; | ||
import * as t from 'typanion'; | ||
import { typanionResolver } from '..'; | ||
|
||
const schema = t.isObject({ | ||
username: t.applyCascade(t.isString(), [t.hasMinLength(1)]), | ||
password: t.applyCascade(t.isString(), [t.hasMinLength(1)]), | ||
}); | ||
|
||
interface FormData { | ||
unusedProperty: string; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
interface Props { | ||
onSubmit: (data: FormData) => void; | ||
} | ||
|
||
function TestComponent({ onSubmit }: Props) { | ||
const { | ||
register, | ||
formState: { errors }, | ||
handleSubmit, | ||
} = useForm<FormData>({ | ||
resolver: typanionResolver(schema), // 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 Typanion and TypeScript's integration", async () => { | ||
const handleSubmit = jest.fn(); | ||
render(<TestComponent onSubmit={handleSubmit} />); | ||
|
||
expect(screen.queryAllByRole(/alert/i)).toHaveLength(0); | ||
|
||
await act(async () => { | ||
user.click(screen.getByText(/submit/i)); | ||
}); | ||
|
||
expect(screen.getAllByText('Expected to have a length of at least 1 elements (got 0)')).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,77 @@ | ||
import { Field, InternalFieldName } from 'react-hook-form'; | ||
import * as t from 'typanion'; | ||
|
||
|
||
|
||
export const isSchema = t.isObject({ | ||
username: t.applyCascade(t.isString(), [ | ||
t.matchesRegExp(/^\w+$/), | ||
t.hasMinLength(2), | ||
t.hasMaxLength(30), | ||
]), | ||
password: t.applyCascade(t.isString(), [ | ||
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character | ||
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character | ||
t.matchesRegExp(new RegExp('.*\\d.*')), // one number | ||
t.matchesRegExp(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*')), // one special character | ||
t.hasMinLength(8), // Must be at least 8 characters in length | ||
]), | ||
repeatPassword: t.applyCascade(t.isString(), [ | ||
t.matchesRegExp(new RegExp('.*[A-Z].*')), // one uppercase character | ||
t.matchesRegExp(new RegExp('.*[a-z].*')), // one lowercase character | ||
t.matchesRegExp(new RegExp('.*\\d.*')), // one number | ||
t.matchesRegExp(new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*')), // one special character | ||
t.hasMinLength(8), // Must be at least 8 characters in length | ||
]), | ||
accessToken: t.isString(), | ||
birthYear: t.applyCascade(t.isNumber(), [t.isInteger(), t.isInInclusiveRange(1900, 2013)]), | ||
email: t.applyCascade(t.isString(), [t.matchesRegExp(/^\S+@\S+$/)]), | ||
tags: t.isArray(t.isString()), | ||
enabled: t.isBoolean(), | ||
like: t.isObject({ | ||
id: t.applyCascade(t.isNumber(), [t.isInteger(), t.isPositive()]), | ||
name: t.applyCascade(t.isString(), [t.hasMinLength(4)]) | ||
}), | ||
}); | ||
|
||
export const validData = { | ||
username: 'Doe', | ||
password: 'Password123_', | ||
repeatPassword: 'Password123_', | ||
birthYear: 2000, | ||
email: '[email protected]', | ||
tags: ['tag1', 'tag2'], | ||
enabled: true, | ||
accessToken: 'accessToken', | ||
like: { | ||
id: 1, | ||
name: 'name', | ||
}, | ||
}; | ||
|
||
export const invalidData = { | ||
password: '___', | ||
email: '', | ||
birthYear: 'birthYear', | ||
like: { id: 'z' }, | ||
tags: [1, 2, 3], | ||
}; | ||
|
||
export const fields: Record<InternalFieldName, Field['_f']> = { | ||
username: { | ||
ref: { name: 'username' }, | ||
name: 'username', | ||
}, | ||
password: { | ||
ref: { name: 'password' }, | ||
name: 'password', | ||
}, | ||
email: { | ||
ref: { name: 'email' }, | ||
name: 'email', | ||
}, | ||
birthday: { | ||
ref: { name: 'birthday' }, | ||
name: 'birthday', | ||
}, | ||
}; |
Oops, something went wrong.