Hey! I'm Aviatar. I'm a front-end engineer at Facebook, and I'm the author of VEST. VEST is a form validation framework inspired by unit testing libraries like Mocha or Jest. So, if you've done even a little unit testing in your career, I think you'll feel very much at home working with VEST.
Today, I want to show you how we can use VEST to improve the way we write form validations in our Vue apps. But before I start speaking about VEST itself, I want to mention a little the motivation behind VEST and what led me to write VEST to begin with.
In my experience writing forms and building forms before using VEST, I had a big problem of lacking structure. So, I was trying to add validations to the form, and I wasn't sure where I should put the validation logic. Should I put them inside a change handlers? Should I put them somewhere in my feature in a shared library? How do I write it? How do I avoid it being too specific to my feature? And there is no specific structure that the validations should follow.
So I ended up making it work by writing it somewhere between my handlers and my feature. But then when I wanted to make changes and maintain the feature, like adding more fields in the feature, or making fields dependent on one another or even removing a field, it was very, very hard because everything was tied down to the feature. And because everything is very specific to the feature, it was very hard as well to make use of it again. So to take it and use it in a different form or a different feature, like the password field in both reset password and sign in.
So all these led me to think of a solution. And a couple of years back when I was working with a previous employer, we just started writing unit tests for our apps. And I saw that patterns that unit tests have, that we have that testing suite with describes and expect. And it looked very similar to the way I was thinking about form validation in my mind. Because unit tests are declarative by nature, so you are able to describe exactly what you want to happen. And along with that, they are very good at expressing what's there compared to how it should be. So you put a function in a test, and the same goes for form validation.
So I want my values, my data, to run through some tests. And it all seems very relevant to the world of form validation. Of course, it's not exactly the same, and the terminology is different, and we don't run unit tests in production. But with some design adjustments, I was able to come up with something that's still very similar to the way we write unit tests, and still be very relevant for form validation. And the structure I came up with is this. We first create a suite that's separate from our feature code, and add a callback to it. Inside the callback, we add our tests, similar to unit test tests, with an extra field or an extra parameter, which is the name of the field that we're validating. So in this case we have test, and we're testing that username. And then we have the error that the user will get in case of a validation failure. So username must be at least three characters.
Comments