Quick Start
Prerequisites
Installation
Install the library using your preferred package manager.
shell
npm install @alexovn/vuelidshell
yarn add @alexovn/vuelidshell
pnpm add @alexovn/vuelidshell
bun add @alexovn/vuelidUsage
Import useValidation composable from the library, pass validation schema, form data and optional settings to its params and use its built-in functions.
Vue
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { z } from 'zod'
import { useValidation } from '@alexovn/vuelid';
const schema = ref(
z.object({
name: z.string().min(1, { error: 'The name field is required' }),
email: z
.string()
.min(1, { error: 'The email field is required' })
.pipe(z.email({ error: 'Invalid email' })),
})
);
const form = reactive({
name: '',
email: ''
})
const { validate, getError, isValid } = useValidation(schema, form);
const submit = async () => {
await validate();
if (isValid.value) {
alert('Validation succeeded!');
}
};
</script>
<template>
<form @submit.prevent="submit">
<div>
<div>
<label>
<span>Name</span>
<input v-model="form.name" placeholder="Name" name="name" type="text">
</label>
<div>{{ getError('name') }}</div>
</div>
<div>
<label>
<span>Email</span>
<input v-model="form.email" placeholder="Email" name="email" type="text">
</label>
<div>{{ getError('email') }}</div>
</div>
</div>
<button type="submit">
Submit
</button>
</form>
</template>