Simple, lightweight, isomorphic, template-based validation library.
⚡ Installation ⚡ Usage ⚡ Vue.js Mixin ⚡ Template Reuse ⚡
npm i vuito
Or via JsDeliver, UNPKG, or bundle.run.
CJS
const { regex, required } = require('vuito');
Vuito is fully tree-shakable, so you can import only the methods you need.
ES6
import { regex, required } from 'vuito';
With templates you can easily create and reuse validations.
validations/auth.ts
:
import { Template, required, minLength, maxLength } from 'vuito';
export const signIn = new Template({
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.' },
]
});
index.ts
:
import { signIn } from './validations/auth';
signIn.check({
username: 'test123',
password: 'tooshort',
})
.then(() => console.log('Sign-in data is valid!'))
.catch(console.error)
// Result: console.error: Password is too short.
Use vuito with Vue.js or Nuxt.js as a breeze!
npm i @vuito/vue
pages/signin.vue
:
<template>
<span>{{ errors.username }}</span>
<input type="text" id="username"
:class="{error: !!errors.username}"
v-model="fields.username" @input.capture="onInput"
>
<span>{{ errors.password }}</span>
<input type="password" id="password"
:class="{error: !!errors.password}"
v-model="fields.password" @input.capture="onInput"
>
</template>
<script>
import { signIn } from './validations/auth';
import Vuito from '@vuito/vue';
export default {
mixins: [Vuito(signIn)]
}
</script>
To reuse your validation template you have many solutions.