Skip to content

Commit 7d37775

Browse files
committed
Improved tests for 30
1 parent 322c7ff commit 7d37775

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/06-challenges/30-form-validator.problem.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, it } from "vitest";
2+
import { Equal, Expect } from "../helpers/type-utils";
23

34
const makeFormValidatorFactory = (validators: unknown) => (config: unknown) => {
45
return (values: unknown) => {
@@ -53,6 +54,17 @@ it("Should properly validate a user", () => {
5354
username: "Minimum length is 5",
5455
email: "Invalid email",
5556
});
57+
58+
type test = Expect<
59+
Equal<
60+
typeof errors,
61+
{
62+
id: string | undefined;
63+
username: string | undefined;
64+
email: string | undefined;
65+
}
66+
>
67+
>;
5668
});
5769

5870
it("Should not allow you to specify a validator that does not exist", () => {
@@ -61,3 +73,14 @@ it("Should not allow you to specify a validator that does not exist", () => {
6173
id: ["i-do-not-exist"],
6274
});
6375
});
76+
77+
it("Should not allow you to validate an object property that does not exist", () => {
78+
const validator = createFormValidator({
79+
id: ["required"],
80+
});
81+
82+
validator({
83+
// @ts-expect-error
84+
name: "123",
85+
});
86+
});

src/06-challenges/30-form-validator.solution.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, it } from "vitest";
2+
import { Equal, Expect } from "../helpers/type-utils";
23

34
type ValidatorsBase = Record<string, (value: string) => string | void>;
45

@@ -8,7 +9,7 @@ const makeFormValidatorFactory =
89
<TValidators extends ValidatorsBase>(validators: TValidators) =>
910
<TInput extends ValidatorConfig<keyof TValidators>>(config: TInput) => {
1011
return (
11-
values: Record<keyof TInput, string>,
12+
values: Record<keyof TInput, string>
1213
): Record<keyof TInput, string | undefined> => {
1314
const errors = {} as any;
1415

@@ -61,6 +62,17 @@ it("Should properly validate a user", () => {
6162
username: "Minimum length is 5",
6263
email: "Invalid email",
6364
});
65+
66+
type test = Expect<
67+
Equal<
68+
typeof errors,
69+
{
70+
id: string | undefined;
71+
username: string | undefined;
72+
email: string | undefined;
73+
}
74+
>
75+
>;
6476
});
6577

6678
it("Should not allow you to specify a validator that does not exist", () => {
@@ -69,3 +81,14 @@ it("Should not allow you to specify a validator that does not exist", () => {
6981
id: ["i-do-not-exist"],
7082
});
7183
});
84+
85+
it("Should not allow you to validate an object property that does not exist", () => {
86+
const validator = createFormValidator({
87+
id: ["required"],
88+
});
89+
90+
validator({
91+
// @ts-expect-error
92+
name: "123",
93+
});
94+
});

0 commit comments

Comments
 (0)