Challenges 7 exercises
Problem

Create a Form Validation Library

In this exercise, we're creating a function called makeFormValidatorFactory which returns another function called createFormValidator, which finally returns another function validateUser:


const makeFormValidatorFactory = (validators: unknown) => (config: unknown) => {
return (

Loading exercise

Transcript

0:00 In this exercise, we're creating a function which returns another function, which returns another function. We have a createFormValidator function here, or rather makeFormValidatorFactory, which returns essentially another factory. What this does is we can specify various validation rules here.

0:20 We can specify required, min length, and email on here. Then we create a validator out of those rules and we specify, ID is going to be on this object that we're validating, username, and email, and we pass the validation rules as strings to this piece here.

0:38 There's various things that we can do here. We can say we want to validate a user. We pass it the ID, username, and email, which should be inferred from here, ID, username, and email. We're saying the errors that it produces should have username, minimum length is five, which we're getting from here, and email is an invalid email, wherever it is. Here we go.

1:00 Then, the errors type II is currently typed as any but type of errors should be equal to an object where ID is string or undefined, username is string or undefined. You get the idea.

1:13 Then you can't specify a validator that doesn't exist, so this one should be giving you errors if you don't pass in required or something like that. Then the validator that you produce here shouldn't be values unknown. It should be values based on this type that's here.

1:31 There's a lot to do here. We have some generics we need to capture, for sure, we need to decide what shapes those generics should be, whether we're going to capture the whole object or just the keys. We also need to make sure everything here is strongly typed.

1:46 This value here, it should only ever be a string because we're dealing with web form values, let's say, which are only strings. I think that's all the information you need. Probably less than you want but what you need. Good luck.