- Notifications
You must be signed in to change notification settings - Fork1
✅ Lightweight validation for Vue — just 40 lines of code
License
NotificationsYou must be signed in to change notification settings
nexxtmove/validation-composable
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Lightweight, practical validation for Vue.Only 40 lines of code. No special components to wrap your forms. No proprietary schema syntax to learn. Just bring your favorite schema library and go.
🔌 Works withZod,Yup,Valibot, and any otherStandard Schema library.
npm i validation-composable
import{useValidation}from'validation-composable'const{ validate, issues}=useValidation(data,schema)
<script setup>// Use your own data (Reactive, Ref, Pinia, etc.)constdata=reactive({ subject:'', body:'',})// Use your favorite schema libraryconstschema=z.object({ subject:z.string().min(1,'Subject is required'), body:z.string().min(1,'Body is required'),})// Pass the data and schema to the composableconst {validate,issues }=useValidation(data, schema)// Validate before submission. It auto-validates on data changes.asyncfunctionsend() {constvalid=awaitvalidate()if (!valid)return …}</script><template> <form@submit="send"> <inputv-model="data.subject":class="{ 'border-red': issues.subject }" /> <spanv-if="issues.subject">{{ issues.subject[0] }}</span> <textareav-model="data.body":class="{ 'border-red': issues.body }" /> <spanv-if="issues.body">{{ issues.body[0] }}</span> </form></template>
💡 Pro Tip: Consider creating reusable input components to display validation errors automatically. This eliminates repetition and ensures consistent styling across your forms.
const{ validate, issues, clearIssues}=useValidation(data,schema)
validate()
: validates and fillsissues
; returns true when validissues
: reactive object that mirrors your data; failing fields contain arrays of error messagesclearIssues()
: clears all issues
About
✅ Lightweight validation for Vue — just 40 lines of code
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Contributors2
Uh oh!
There was an error while loading.Please reload this page.