Skip to content

Commit e2702ec

Browse files
committed
typed-object-validator page
1 parent 7b7fb86 commit e2702ec

File tree

3 files changed

+78
-5
lines changed

3 files changed

+78
-5
lines changed

articles/typed-object-validator.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
11
# typed-object-validator
2+
3+
To validate your form using [typed-object-validator](https://github.com/codestix/typed-object-validator), install it:
4+
5+
```
6+
npm install typed-object-validator
7+
```
8+
9+
Then pass the validate function of your schema to the `useForm` hook.
10+
11+
## Usage example
12+
13+
```tsx
14+
import tv, { SchemaType } from "typed-object-validator";
15+
16+
const RegisterRequestSchema = tv.object({
17+
email: tv.email("Please enter a valid email address.").doCase("lower").min(1, "Please enter an email"),
18+
password: tv.string().min(1, "Please enter a password"),
19+
});
20+
type RegisterRequest = SchemaType<typeof RegisterRequestSchema>;
21+
22+
export function TypedValidationExample() {
23+
const form = useForm<RegisterRequest>(
24+
{ email: "", password: "" },
25+
(values) => RegisterRequestSchema.validate(values),
26+
true
27+
);
28+
29+
function submit() {
30+
// Transform if you want (will lowercase email in this case)
31+
console.log("submit", form.values, RegisterRequestSchema.transform(form.values));
32+
}
33+
34+
return (
35+
<form onSubmit={form.handleSubmit(submit)}>
36+
<FormInput form={form} name="email" type="email" />
37+
<FormError form={form} name="email" />
38+
<FormInput form={form} name="password" type="password" />
39+
<FormError form={form} name="password" />
40+
<button type="submit">Submit</button>
41+
</form>
42+
);
43+
}
44+
```

articles/yup.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ export function YupFormExample() {
5050
<FormError form={form} name="email" />
5151
<FormInput form={form} name="password" type="password" />
5252
<FormError form={form} name="password" />
53-
{/* Listen for any change on the form, and disable the submit button when there is an error */}
54-
<AnyListener form={form} render={(form) => <button disabled={form.error}>Submit</button>} />
53+
<button type="submit">Submit</button>
5554
</form>
5655
);
5756
}

pages/docs/test.tsx

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

1514
function MyForm() {
1615
const form = useForm({ email: "" });
@@ -235,8 +234,38 @@ export function YupFormExample() {
235234
<FormError form={form} name="email" />
236235
<FormInput form={form} name="password" type="password" />
237236
<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>} />
237+
<button type="submit">Submit</button>
238+
</form>
239+
);
240+
}
241+
242+
import tv, { SchemaType } from "typed-object-validator";
243+
244+
const RegisterRequestSchema = tv.object({
245+
email: tv.email("Please enter a valid email address.").doCase("lower").min(1, "Please enter an email"),
246+
password: tv.string().min(1, "Please enter a password"),
247+
});
248+
type RegisterRequest = SchemaType<typeof RegisterRequestSchema>;
249+
250+
export function TypedValidationExample() {
251+
const form = useForm<RegisterRequest>(
252+
{ email: "", password: "" },
253+
(values) => RegisterRequestSchema.validate(values),
254+
true
255+
);
256+
257+
function submit() {
258+
// Transform if you want (will lowercase email in this case)
259+
console.log("submit", form.values, RegisterRequestSchema.transform(form.values));
260+
}
261+
262+
return (
263+
<form onSubmit={form.handleSubmit(submit)}>
264+
<FormInput form={form} name="email" type="email" />
265+
<FormError form={form} name="email" />
266+
<FormInput form={form} name="password" type="password" />
267+
<FormError form={form} name="password" />
268+
<button type="submit">Submit</button>
240269
</form>
241270
);
242271
}
@@ -257,6 +286,8 @@ export default function Testing() {
257286
<ValidationExample />
258287
<hr />
259288
<YupFormExample />
289+
<hr />
290+
<TypedValidationExample />
260291
</>
261292
);
262293
}

0 commit comments

Comments
 (0)