Vue Form Validations with Vest

Rate this content
Bookmark

Forms on the web have always been with us, yet it still feels like you always have to fight them to make validation work as you planned - and even with the significant improvements modern day libraries and frameworks give us, maintenance is mostly a hassle.

Vest is a new breed of form validations framework. It draws its syntax and style from Unit Testing frameworks like mocha or Jest (hence the name), which brings the elegance and declarative nature of these frameworks to the world of form validation, greatly reducing the overhead it takes to write, maintain and reuse validations in your JS apps.

This talk has been presented at Vue.js London Live 2021, check out the latest edition of this Tech Conference.

FAQ

Aviatar is a front-end engineer at Facebook and the author of VEST, a form validation framework inspired by unit testing libraries like Mocha or Jest.

VEST is a form validation framework inspired by unit testing libraries such as Mocha or Jest. It allows developers to write form validations in a structured and declarative manner.

VEST addresses the lack of structure in form validation logic by providing a framework that allows for declarative and reusable validations, making it easier to maintain and extend form features.

In VEST, async validations are handled by returning a promise or an async function within the validation suite. If the function rejects or throws an error, the validation fails.

While VEST is not primarily a schema validation library, it can integrate with JSON schema validators. The enforce function in VEST has extensive schema validation capabilities.

Yes, VEST can be integrated with Nuxt, Vuetify, Vuex, and other libraries. VEST is versatile and not tied to any specific framework, making it easy to use in various contexts.

VEST does not natively support file type validation as it focuses on general-purpose validations. However, custom assertions can be added to handle business-specific validations, including file types.

You can clear the validation state in VEST by using the suite.reset function to wipe all state or the suite.remove function to remove specific fields from the state.

VEST offers a unique syntax inspired by unit testing, separated UI logic, and the ability to share validations across different frameworks and platforms. It is particularly useful for multi-framework or multi-platform applications.

VEST offers features such as structured validations, multiple validations per field, warning validations, async validations, memoization for costly validations, and the ability to group and nest tests. Despite its complexity, VEST remains lightweight at less than six kilobytes when minified and zipped.

Evyatar Alush
Evyatar Alush
21 min
20 Oct, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

VEST is a form validation framework inspired by unit testing libraries. It provides a structured approach to form validation, making maintenance and reuse easier. VEST supports multiple validations per field, warning validations, interdependent field validation, async validations, and memoization. It is lightweight and can be integrated with various frameworks and libraries. The speaker is open to collaboration and contributions for adding a reactive interface using VUE's reactivity model.

1. Introduction to VEST and the Motivation Behind It

Short description:

I'm Aviatar, a front-end engineer at Facebook and the author of VEST, a form validation framework inspired by unit testing libraries. Today, I'll show you how VEST can improve form validations in Vue apps. Before diving into VEST, let me explain the motivation behind it. Previously, I struggled with the lack of structure when adding validations to forms. This made maintenance and reuse difficult. Inspired by unit tests, I developed a structure for form validation that allows for easy description of desired behavior and flexibility for different features. Let's explore this structure with an example validating the username field.

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.

2. Using VEST with a Real Live Vue App

Short description:

Inside the callback of that test, we have our assertions. I want to show you how it works with a real live Vue app. This is our app, a basic app without any validation yet. I added input components for styling, class names for error, warning, and success, props for errors and warnings, a loading spinner, and an empty validate function. Let's create our suite and import create from vest.

And inside the callback of that test, we have our assertions. So similar to assert or expect, we enforce the data.username is longer than two, or whatever validation we have there. And I want to show you how it works with a real live Vue app.

Just note, I'm using here, the options API, but it could work with the composition API just as well. No changes whatsoever to the best code.

So this is our app. It's a very basic app without any validation at all yet. And I added some input components that are there just for styling. I added a few class names. So I added a class name for error. It turns it red. Let me just refresh. Okay, it turns it red. I added a class name for warning. That turns it orange and one for success. That turns it green. And along with that, I also added a few props. So one for errors. And it takes an array of strings. And when displayed, it shows the error on the field. And same for warnings. I also added a loading spinner, because we're going to do some async validations later down the line. So loading true. And we're going to see a spinner. So that's all we have already here. We also have the validate function, which is empty. It takes the name and the value from the field that we're validating. And let's create our suite. So source. And let's import create from vest.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.

Workshops on related topic

Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetcov
Mikhail Kuznetcov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Vue.js London Live 2021Vue.js London Live 2021
117 min
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Top Content
Workshop
Daniel Roe
Daniel Roe
We'll build a Nuxt project together from scratch using Nitro, the new Nuxt rendering engine, and Nuxt Bridge. We'll explore some of the ways that you can use and deploy Nitro, whilst building a application together with some of the real-world constraints you'd face when deploying an app for your enterprise. Along the way, fire your questions at me and I'll do my best to answer them.
Monitoring 101 for React Developers
React Summit US 2023React Summit US 2023
107 min
Monitoring 101 for React Developers
Top Content
WorkshopFree
Lazar Nikolov
Sarah Guthals
2 authors
If finding errors in your frontend project is like searching for a needle in a code haystack, then Sentry error monitoring can be your metal detector. Learn the basics of error monitoring with Sentry. Whether you are running a React, Angular, Vue, or just “vanilla” JavaScript, see how Sentry can help you find the who, what, when and where behind errors in your frontend project. 
Workshop level: Intermediate
TresJS create 3D experiences declaratively with Vue Components
Vue.js London 2023Vue.js London 2023
137 min
TresJS create 3D experiences declaratively with Vue Components
Workshop
Alvaro Saburido
Alvaro Saburido
- Intro 3D - Intro WebGL- ThreeJS- Why TresJS- Installation or Stackblitz setup - Core Basics- Setting up the Canvas- Scene- Camera- Adding an object- Geometries- Arguments- Props- Slots- The Loop- UseRenderLoop composable- Before and After rendering callbacks- Basic Animations- Materials- Basic Material- Normal Material- Toon Material- Lambert Material- Standard and Physical Material- Metalness, roughness - Lights- AmbientLight- DirectionalLight- PointLights- Shadows- Textures- Loading textures with useTextures- Tips and tricks- Misc- Orbit Controls- Loading models with Cientos- Debugging your scene- Performance
Building Vue forms with VeeValidate
Vue.js London Live 2021Vue.js London Live 2021
176 min
Building Vue forms with VeeValidate
Workshop
Abdelrahman Awad
Abdelrahman Awad
In this workshop, you will learn how to use vee-validate to handle form validation, manage form values and handle submissions effectively. We will start from the basics with a simple login form all the way to using the composition API and building repeatable and multistep forms.

Table of contents:
- Introduction to vee-validate
- Building a basic form with vee-validate components
- Handling validation and form submissions
- Building validatable input components with the composition API
- Field Arrays and repeatable inputs
- Building a multistep form
Prerequisites:
VSCode setup and an empty Vite + Vue project.
Building Pinia From Scratch
Vue.js Live 2024Vue.js Live 2024
70 min
Building Pinia From Scratch
Workshop
Eduardo San Martin Morote
Eduardo San Martin Morote
Let's dive into how Pinia works under the hood by building our own `defineStore()`. During this workshop we will cover some advanced Vue concepts like dependency Injection and effect scopes. It will give you a better understanding of Vue.js Composition API and Pinia. Requirements: experience building applications with Vue and its Composition API.