Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course

The "Creating the Validation Monoid" Lesson is part of the full, Hardcore Functional Architecture Patterns in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian continues to develop the validation library, and demonstrates how to create a validation monoid.

Preview
Close

Transcript from the "Creating the Validation Monoid" Lesson

[00:00:00]
>> So this is the beginning of a pretty, pretty strong validation library that we can make better, and we're using fail and success. I want to do one last thing to this library, just to make it a little cooler. Is we wanna say, instead of just concatenating the results.

[00:00:19]
We could actually concat the entire validation itself. Now, I can make a couple decisions here. I can put a validation around this. So I have to have something to combine, right? So instead of just having a function, I have a validation holding the function. And now I can combine these things.

[00:00:46]
So that's one decision I can make. Another one could be, I can demand that this function return me a success or a failure. So I can say well, all right, let's do, if it's actually there, we'll keep you, otherwise, we'll see you a failure. And now I can actually embed a message instead of just key is bad, right?

[00:01:05]
And I can do a message saying, needs to be present, right? So that's nice, but then, we're missing the key here. So these are things I'm making decisions right now considering my options. So, I think what I'm gonna do, let's say a validation takes in a key and an x, and we're gonna be able to say, construct our message saying the key here.

[00:01:40]
And then, we can run our validation with the x. So this is the shape of a validation that I'm going to define. And that kinda has some architectural implications. Notice also I'm putting this in an array. Because I just won't fail to concat whatever it's holding, I can concat a bunch of strings, that still works.

[00:02:04]
But then I would need to have these spaces in here to make it look nice and I just rather have an array of results, so cool. So we can go make a validation monoid now, let's go. So we have two things to do, we have to do a validation monoid and we have to update this little slow part down there.

[00:02:25]
So, what's happening here is we could run the validation right here. But that's no longer a function, right? It's gonna be a validation function, validation object. So we'll give that a run method, run. And then that will return me a success or a fail. So I could actually just get rid of that, right.

[00:02:50]
And I told it that I would give it a key and the value. So that's pretty nice. Does everybody see how that works? I haven't made validation yet, but this is kind of what I'm sketching out to make, is that validation will return me a success or a failure.

[00:03:06]
And when I run the validation, I'm gonna combine a bunch of validations by probably effectively combining their failures underneath. And this gives me the ability to combine. This is present now with a maybe concat isEmail. So let's go ahead and get through writing this. So make a little easy mail validation to go along with isPresent, and let's do the exact same thing like isEmail, whoops, isEmail will be a validation, we'll just say like x.

[00:03:44]
About at testX, there we go [LAUGH]. So if it has an add sign, we'll believe it's an email, otherwise must be an email, cool. And let's go ahead and write validation, so we'll do that up here, const validation. That takes an F. We'll just call that run, so we can do punting, we'll say run.

[00:04:14]
Right, and if I was gonna combine two validations, we'll say well, were always gonna return a new validation. That validation takes a key, and an x, and we'll run. Let's see, we'll run our validation on the key and the x. And we'll concat the result which is going to be a fail or a success with the result of running the others, or the other validation on the key in the x.

[00:04:49]
How cool is that? [LAUGH] so what we're doing here is saying, we have this validation that we can combine altogether with other validations, and when we run it, it's gonna run all of them. And it's gonna combine all the results and they're gonna combine their results and [INAUDIBLE] so on, etc.

[00:05:06]
Let's see if this works, I'm sure I'm gonna hit a bug.
>> [INAUDIBLE] key in your fail cases.
>> Thank you, where is that?
>> IsEmail,
>> IsEmail got the key, it's even telling me, it say you're not using key. All right, cool, and it's probably the same in success, yeah, thank you.

[00:05:25]
Appreciate the second pair of Is. So let's see if this works. I don't wanna spend too much time on it, all right? So it got through that. Let's get rid of the email. See if it gets through that. Email must be an email, that's for sure. What if I take this out?

[00:05:41]
Name needs to be present, email must be email, and this is pretty cool. If I do Brian and I get rid of this altogether. The email needs to be present and must be an email so it's working. It's working well. Let's just quickly look over this code for a minute, and see what we've done.

[00:06:00]
We can actually keep going. [LAUGH] More and more intense, right? Like what if, one thing that would be fun is to define a validation that is, if I'm gonna combine it. What if I do or [LAUGH] Or this or that, right? Or alternatively I could say, instead of just combining them I could say or right there, but that wouldn't be working with monoids and these abstract interfaces that know how to concat stuff.

[00:06:31]
So we can keep building this up and it turns out and an order form this nice. It will be called semi ring where they interact and distribute over each other and create a really, really nice way to say like. I'm gonna combine all these validations in a way that's, a mixture of ANDs and ORs.

[00:06:50]
So I can say, well, if you do give me an email to make sure it looks like an email, but you don't have to, right, that kind of stuff. And that can get really efficient as well. So, cool, any questions on validations before we kind of button this up, we're actually going to use this later so let's go ahead and just export, validate [LAUGH].

[00:07:11]
Whoops, export, validate and actually I think I have to do module export sequels because we're not gonna be using packages and such.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now