But what types of form validations are out there? I mean, that cannot be the first form validation library. I mean, we've all used form validation libraries, both in the client and the server. So I'll try to get a brief overview of what's there, and then we'll understand how VEST is different.
And first we have the functional matchers, like basic functions like isEmail, isNumber, longer than, that basically give us a Boolean response if some value matches some criteria. And this is really simple and this is also really useful. But it doesn't care much about structure, it doesn't help us with maintenance, and neither with testing. And this is really useful, but not a full-fledged solution for form validation.
Second is that we have the schema validation libraries that take like a next step of what we just described. They take the form of the data, the shape of the data, and the shape of the data in its entirety has to conform to some criteria. And this is good mostly for the API level. But when talking about the client, not really. I mean, what do you do when the user interacts with a form, like types inside the username field? Do you validate the whole thing, the whole schema? And also it's really hard to describe complex ideas, like fields that depend on one another, inside of a schema. So it's really useful, but not necessarily the way we want it to be.
And third, we have in the client framework-specific UI form state validation or state management libraries that are specific to the framework and they give you components or event handlers or directives to use inside your app. And then what it does is basically take some control away from you, but gives you all the state management on the form and all the form validation for the form, and then you are pretty much set and it's good. But the problem is, well, it's framework-specific, UI framework-specific, so we cannot share between your client and server easily, or between Angular or React or Vue easily, and most of us have at least some apps or some different types of our apps. And also, because they take control from you, they put their stuff inside your app, it's really hard to take that control back when you need to bypass some behavior. It's good, but not necessarily, again, the way we want it to be.
And how do unit tests fit in? This is Test.js. A couple of years ago, like six years ago, I started writing unit tests as part of my work, and I realized this. When we write a unit test, we have our unit testing suite. That usually starts with a describe, it doesn't have to, but it starts with some top level suite that describes the idea that we're testing our feature or app or whatever it is. Inside of it, we have a series of tests that basically describe what assertion we're making now and what is the error that we're going to show in case of failure. Then we have our assertion, for example, expect and what we expect it to be. What if we could use that same idea inside of the world of form validation? I mean, it's pretty much the same. When we write a form, we have our top most concept of a form that we want to describe. Inside of it, we have the different fields, a set of fields that we want to test, and then we just want to make assertion. Now, instead of asserting behavior, we want to assert data. I played around with this specific idea, and this is what I came up with. First, we create our form validation suite.
Comments