Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

✅ Lightweight validation for Vue — just 40 lines of code

License

NotificationsYou must be signed in to change notification settings

nexxtmove/validation-composable

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.

VersionLicenseTests

Installation

npm i validation-composable

Usage

import{useValidation}from'validation-composable'const{ validate, issues}=useValidation(data,schema)

Example

<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.

API

const{ validate, issues, clearIssues}=useValidation(data,schema)
  • validate(): validates and fillsissues; returns true when valid
  • issues: reactive object that mirrors your data; failing fields contain arrays of error messages
  • clearIssues(): clears all issues

About

✅ Lightweight validation for Vue — just 40 lines of code

Topics

Resources

License

Stars

Watchers

Forks

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp