Skip to content

Commit 7b7fb86

Browse files
committed
yup information
1 parent d799745 commit 7b7fb86

File tree

8 files changed

+126
-23
lines changed

8 files changed

+126
-23
lines changed

articles/Getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ Tweak the above example to your desire by...
107107
- [Toggling a field using a checkbox](/docs/Toggling-a-field)
108108
- [Creating a component which shows the current form values in JSON](/docs/Live-json-component)
109109
- [Creating a custom input like FormInput, FormSelect ...](/docs/Custom-input)
110-
- **Look at the sidebar for more...**
110+
- Look at the sidebar for more...

articles/Validation.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ A validator is a function that takes form values, and returns errors for these v
44

55
You can use a validator by passing it to the [`useForm`](/docs/useForm) hook.
66

7-
Its it recommended to use a validation library, as this makes the process of validating data mush easier. This library has [built-in support for yup](/docs/yup).
7+
Its it recommended to use a validation library, as this makes the process of validating data mush easier. This library has drop-in functionallity for:
8+
9+
- [typed-object-validator](/docs/typed-object-validator): recommended, a typed-checked validation library
10+
- [yup](/docs/yup): a widely used validation library
811

912
## Example
1013

articles/typed-object-validator.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# typed-object-validator

articles/yup.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
# yup
22

3-
**typed-react-form** has built-in support for [yup](https://github.com/jquense/yup). This is a validation library which makes it mush easier to validate data.
3+
To validate your form using [yup](https://www.npmjs.com/package/yup), do the following:
44

5-
Use the `yupValidator(yupSchema, options?)` helper function to create a validator function for your yup schema.
5+
First, install [typed-react-form-yup](https://www.npmjs.com/package/typed-react-form-yup)
6+
7+
```
8+
npm install typed-react-form-yup
9+
```
10+
11+
Then use the `yupValidator(yupSchema, options?)` helper function to create a validator function for your yup schema, which you can then pass to the `useForm` hook.
612

713
## Parameters
814

915
#### `yupSchema` **(required)**
1016

1117
The yup validation schema that will validate the form values.
1218

13-
---
14-
1519
#### `options` **(optional)**
1620

1721
Yup validation options.
1822

1923
## Usage example
2024

2125
```tsx
22-
import { useForm, yupValidator, FormError, FormInput, AnyListener } from "typed-react-form";
26+
import { useForm, FormError, FormInput, AnyListener } from "typed-react-form";
27+
import { yupValidator } from "typed-react-form-yup";
2328
import * as yup from "yup";
2429

2530
interface LoginRequest {
@@ -32,15 +37,15 @@ const LoginRequestSchema = yup.object({
3237
password: yup.string().required("Please enter a password").min(5, "Password must be longer"),
3338
});
3439

35-
function FormExample() {
36-
const form = useForm<LoginRequest>({ email: "", password: "" }, yupValidator(LoginRequestSchema)); // Use the yup validator
40+
export function YupFormExample() {
41+
const form = useForm<LoginRequest>({ email: "", password: "" }, yupValidator(LoginRequestSchema), true);
42+
43+
function submit() {
44+
console.log("submit", form.values);
45+
}
46+
3747
return (
38-
<form
39-
onSubmit={(ev) => {
40-
ev.preventDefault();
41-
console.log("submit", form.values);
42-
}}
43-
>
48+
<form onSubmit={form.handleSubmit(submit)}>
4449
<FormInput form={form} name="email" type="email" />
4550
<FormError form={form} name="email" />
4651
<FormInput form={form} name="password" type="password" />

components/SideBar.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const CATEGORIES = [
2727
category: "Validation",
2828
items: [
2929
{ name: "Validation", url: "/docs/Validation" },
30-
{ name: "yup", url: "/docs/yup" },
30+
{ name: "Using yup", url: "/docs/yup" },
31+
{ name: "Using typed-object-validator", url: "/docs/typed-object-validator" },
3132
],
3233
},
3334
{

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
"react-markdown": "^5.0.3",
1919
"react-syntax-highlighter": "^15.4.3",
2020
"styled-components": "^5.2.1",
21-
"typed-react-form": "^1.2.11"
21+
"typed-object-validator": "^1.0.2",
22+
"typed-react-form": "^1.3.0",
23+
"typed-react-form-yup": "^1.0.2",
24+
"yup": "^0.32.9"
2225
},
2326
"devDependencies": {
2427
"@types/node": "^14.14.34",
2528
"@types/react": "^17.0.3",
2629
"@types/react-syntax-highlighter": "^13.5.0",
2730
"@types/styled-components": "^5.1.8",
31+
"@types/yup": "^0.29.11",
2832
"typescript": "^4.2.3"
2933
}
3034
}

pages/docs/test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ErrorMap,
1111
FormError,
1212
} from "typed-react-form";
13+
import tv from "typed-object-validator";
1314

1415
function MyForm() {
1516
const form = useForm({ email: "" });
@@ -207,6 +208,39 @@ function ValidationExample() {
207208
);
208209
}
209210

211+
// import { useForm, FormError, FormInput, AnyListener } from "typed-react-form";
212+
import * as yup from "yup";
213+
import { yupValidator } from "typed-react-form-yup";
214+
215+
interface LoginRequest {
216+
email: string;
217+
password: string;
218+
}
219+
220+
const LoginRequestSchema = yup.object({
221+
email: yup.string().required("Please enter an email").email("Please enter a valid email address."),
222+
password: yup.string().required("Please enter a password").min(5, "Password must be longer"),
223+
});
224+
225+
export function YupFormExample() {
226+
const form = useForm<LoginRequest>({ email: "", password: "" }, yupValidator(LoginRequestSchema), true);
227+
228+
function submit() {
229+
console.log("submit", form.values);
230+
}
231+
232+
return (
233+
<form onSubmit={form.handleSubmit(submit)}>
234+
<FormInput form={form} name="email" type="email" />
235+
<FormError form={form} name="email" />
236+
<FormInput form={form} name="password" type="password" />
237+
<FormError form={form} name="password" />
238+
{/* Listen for any change on the form, and disable the submit button when there is an error */}
239+
<AnyListener form={form} render={(form) => <button disabled={form.error}>Submit</button>} />
240+
</form>
241+
);
242+
}
243+
210244
export default function Testing() {
211245
return (
212246
<>
@@ -221,6 +255,8 @@ export default function Testing() {
221255
<FormJsonExample />
222256
<hr />
223257
<ValidationExample />
258+
<hr />
259+
<YupFormExample />
224260
</>
225261
);
226262
}

yarn.lock

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
dependencies:
8989
regenerator-runtime "^0.13.4"
9090

91-
"@babel/runtime@^7.3.1":
91+
"@babel/runtime@^7.10.5", "@babel/runtime@^7.3.1":
9292
version "7.13.10"
9393
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.13.10.tgz#47d42a57b6095f4468da440388fdbad8bebf0d7d"
9494
integrity sha512-4QPkjJq6Ns3V/RgpEahRk+AGfL0eO6RHHtTWoNNr5mO49G6B5+X6d6THgWEAvTrznU5xYpbAlVKRYcsCgh/Akw==
@@ -271,6 +271,11 @@
271271
"@types/react" "*"
272272
hoist-non-react-statics "^3.3.0"
273273

274+
"@types/lodash@^4.14.165":
275+
version "4.14.168"
276+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
277+
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==
278+
274279
"@types/mdast@^3.0.0", "@types/mdast@^3.0.3":
275280
version "3.0.3"
276281
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.3.tgz#2d7d671b1cd1ea3deb306ea75036c2a0407d2deb"
@@ -323,6 +328,11 @@
323328
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e"
324329
integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ==
325330

331+
"@types/yup@^0.29.11":
332+
version "0.29.11"
333+
resolved "https://registry.yarnpkg.com/@types/yup/-/yup-0.29.11.tgz#d654a112973f5e004bf8438122bd7e56a8e5cd7e"
334+
integrity sha512-9cwk3c87qQKZrT251EDoibiYRILjCmxBvvcb4meofCmx1vdnNcR9gyildy5vOHASpOKMsn42CugxUvcwK5eu1g==
335+
326336
327337
version "1.4.9"
328338
resolved "https://registry.yarnpkg.com/anser/-/anser-1.4.9.tgz#1f85423a5dcf8da4631a341665ff675b96845760"
@@ -1248,6 +1258,11 @@ locate-path@^5.0.0:
12481258
dependencies:
12491259
p-locate "^4.1.0"
12501260

1261+
lodash-es@^4.17.15:
1262+
version "4.17.21"
1263+
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.21.tgz#43e626c46e6591b7750beb2b50117390c609e3ee"
1264+
integrity sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==
1265+
12511266
lodash.camelcase@^4.3.0:
12521267
version "4.3.0"
12531268
resolved "https://registry.yarnpkg.com/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz#b28aa6288a2b9fc651035c7711f65ab6190331a6"
@@ -1258,7 +1273,7 @@ lodash.sortby@^4.7.0:
12581273
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
12591274
integrity sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=
12601275

1261-
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.19:
1276+
lodash@^4.17.11, lodash@^4.17.13, lodash@^4.17.19, lodash@^4.17.20:
12621277
version "4.17.21"
12631278
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
12641279
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
@@ -1363,6 +1378,11 @@ [email protected]:
13631378
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
13641379
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
13651380

1381+
nanoclone@^0.2.1:
1382+
version "0.2.1"
1383+
resolved "https://registry.yarnpkg.com/nanoclone/-/nanoclone-0.2.1.tgz#dd4090f8f1a110d26bb32c49ed2f5b9235209ed4"
1384+
integrity sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA==
1385+
13661386
nanoid@^3.1.16:
13671387
version "3.1.21"
13681388
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.21.tgz#25bfee7340ac4185866fbfb2c9006d299da1be7f"
@@ -1633,6 +1653,11 @@ [email protected], prop-types@^15.7.2:
16331653
object-assign "^4.1.1"
16341654
react-is "^16.8.1"
16351655

1656+
property-expr@^2.0.4:
1657+
version "2.0.4"
1658+
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.4.tgz#37b925478e58965031bb612ec5b3260f8241e910"
1659+
integrity sha512-sFPkHQjVKheDNnPvotjQmm3KD3uk1fWKUN7CrpdbwmUx3CrG3QiM8QpTSimvig5vTXmTvjz7+TDvXOI9+4rkcg==
1660+
16361661
property-information@^5.0.0:
16371662
version "5.6.0"
16381663
resolved "https://registry.yarnpkg.com/property-information/-/property-information-5.6.0.tgz#61675545fb23002f245c6540ec46077d4da3ed69"
@@ -2079,6 +2104,11 @@ [email protected]:
20792104
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
20802105
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==
20812106

2107+
toposort@^2.0.2:
2108+
version "2.0.2"
2109+
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
2110+
integrity sha1-riF2gXXRVZ1IvvNUILL0li8JwzA=
2111+
20822112
tr46@^1.0.1:
20832113
version "1.0.1"
20842114
resolved "https://registry.yarnpkg.com/tr46/-/tr46-1.0.1.tgz#a8b13fd6bfd2489519674ccde55ba3693b706d09"
@@ -2106,10 +2136,20 @@ type-fest@^0.7.1:
21062136
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.7.1.tgz#8dda65feaf03ed78f0a3f9678f1869147f7c5c48"
21072137
integrity sha512-Ne2YiiGN8bmrmJJEuTWTLJR32nh/JdL1+PSicowtNb0WFpn59GK8/lfD61bVtzguz7b3PBt74nxpv/Pw5po5Rg==
21082138

2109-
typed-react-form@^1.2.11:
2110-
version "1.2.11"
2111-
resolved "https://registry.yarnpkg.com/typed-react-form/-/typed-react-form-1.2.11.tgz#bf0ad996df8d2344193dfb323d1087cd5a3e2bd7"
2112-
integrity sha512-yxDt4KsHC4tVTbeshtuwLFiFE1k/BnHL8m0pMpbTKnifA/G98Dl82ui1UxzgZSgOYNYhnFdGxJXE9SmN+dttfA==
2139+
typed-object-validator@^1.0.2:
2140+
version "1.0.2"
2141+
resolved "https://registry.yarnpkg.com/typed-object-validator/-/typed-object-validator-1.0.2.tgz#19ae734eedeca91a06f1535445ed8188509ba149"
2142+
integrity sha512-6A9qDfEViI7lB41LjAGsO7MvupvQJg6Yu1V8DWwEjfWhDuZZCD145UfZLMWM3Ani6a53jE2LbjLhn8FCXjejfg==
2143+
2144+
typed-react-form-yup@^1.0.2:
2145+
version "1.0.2"
2146+
resolved "https://registry.yarnpkg.com/typed-react-form-yup/-/typed-react-form-yup-1.0.2.tgz#74eecb70fe5561318ef59fb69baf67aa693069b9"
2147+
integrity sha512-8fLmfVf2C1qP8pXP+8T9noXiHoSDiKn1jGzHDWjCjHHA42a0K56DVkZ6Km2l58l8t3zjsQlgdrt/vvVJKi0K2Q==
2148+
2149+
typed-react-form@^1.3.0:
2150+
version "1.3.0"
2151+
resolved "https://registry.yarnpkg.com/typed-react-form/-/typed-react-form-1.3.0.tgz#82f1ea14464f7598f5b1afc22066d7724a0fd916"
2152+
integrity sha512-/kAZbJ3BUmGFHYr/GrD41c6iQ1kTSXFuQjZ70Tm0LmHxq4u1FGT1tqREmHq12D4lkwfvM/p1KtGFbpSeyzp7dg==
21132153

21142154
typescript@^4.2.3:
21152155
version "4.2.3"
@@ -2255,3 +2295,16 @@ yocto-queue@^0.1.0:
22552295
version "0.1.0"
22562296
resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
22572297
integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
2298+
2299+
yup@^0.32.9:
2300+
version "0.32.9"
2301+
resolved "https://registry.yarnpkg.com/yup/-/yup-0.32.9.tgz#9367bec6b1b0e39211ecbca598702e106019d872"
2302+
integrity sha512-Ci1qN+i2H0XpY7syDQ0k5zKQ/DoxO0LzPg8PAR/X4Mpj6DqaeCoIYEEjDJwhArh3Fa7GWbQQVDZKeXYlSH4JMg==
2303+
dependencies:
2304+
"@babel/runtime" "^7.10.5"
2305+
"@types/lodash" "^4.14.165"
2306+
lodash "^4.17.20"
2307+
lodash-es "^4.17.15"
2308+
nanoclone "^0.2.1"
2309+
property-expr "^2.0.4"
2310+
toposort "^2.0.2"

0 commit comments

Comments
 (0)