Templates
Templates are designed to group validations for fields.
For example if you need to validate account creation,
you could imagine a template named signin
containing
the needed checks for the username
and password
fields.
Using templates allows you to check a whole form in just one line.
Simply regroup your forms data in an object to match the template structure, and run signin.check(formObj)
.
Define a template
A good practice is to create a new directory/repository to store all your validation templates. Learn why.
Here we use ~/validations/
.
~/validations/auth.ts
import { templateify, required, minLength, maxLength } from 'vuito';
export const signin = templateify({
username: [
{ test: required, message: 'Please enter a username.' },
{ test: minLength(3), message: 'Username is too short.' },
{ test: maxLength(20), message: 'Username is too big.' },
],
password: [
{ test: required, message: 'Please enter a password.' },
{ test: minLength(12), message: 'Password is too short.' },
],
});
Use a template
~/app.ts
import { signin } from '~/validations/auth.js';
const formData = {
username: 'bwayne',
password: 'GothamIsMyCity123',
};
signin.check(formData)
.then(() => console.info("You're good bro!"))
.catch((err) => console.error(`Nah, ${err}`));
Table of Contents