arv-validate-vue

中文文档
1.1.4发布于 2026/11/03MITTypeScriptES Module零依赖

Official reactive Vue 3 adapter for arv-validate (Composition API, Vite, Nuxt)

原始使用文档
文档目录7 节
  1. Installation
  2. Quick Start
  3. Key Features
  4. Usage Outside Components (e.g. Pinia Stores)
  5. API Reference ( useValidator )
  6. Documentation
  7. License

arv-validate-vue

Official reactive Vue 3 adapter for arv-validate using the Composition API (<script setup>).

npm version license Vue 3

English | Español


Installation

npm install arv-validate arv-validate-vue

Quick Start

<script setup>
import { useValidator, required, email, minLength, trim } from 'arv-validate-vue'

const v = useValidator(
  {
    username: '',
    email: ''
  },
  {
    username: [required(), minLength(3), trim()],
    email: [required(), email(), trim()]
  }
)

const onSubmit = async () => {
  const isValid = await v.validate()
  if (!isValid) return

  // Clean, sanitized data ready for backend API
  const payload = v.getPayload()
  await fetch('/api/users', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload)
  })
}
</script>

<template>
  <form @submit.prevent="onSubmit" class="form">
    <div class="form-group">
      <label for="username">Username</label>
      <input
        id="username"
        v-model="v.values.username"
        @blur="v.touch('username')"
        :class="{ 'is-invalid': v.hasError('username') }"
      />
      <span v-if="v.hasError('username')" class="error-text">
        {{ v.getError('username') }}
      </span>
    </div>

    <div class="form-group">
      <label for="email">Email</label>
      <input
        id="email"
        type="email"
        v-model="v.values.email"
        @blur="v.touch('email')"
        :class="{ 'is-invalid': v.hasError('email') }"
      />
      <span v-if="v.hasError('email')" class="error-text">
        {{ v.getError('email') }}
      </span>
    </div>

    <button type="submit" :disabled="!v.isValid">
      Submit
    </button>
  </form>
</template>

Key Features

  • 🔄 Native Vue 3 Reactivity: Built directly on Vue 3's reactive and computed primitives.
  • ⚡ Live Real-Time Validation: Instant feedback as user types or on blur.
  • 🧹 Automatic Cleanup: Automatically unregisters listeners on onUnmounted. No memory leaks.
  • 📦 All-in-One Export: Re-exports all core rules, transformers, and i18n helpers. Just import from 'arv-validate-vue'.

Usage Outside Components (e.g. Pinia Stores)

In Vue components (<script setup>), cleanup is 100% automatic. When creating a validator outside of component lifecycles (like in a Pinia store or composable module), call v.dispose() to release subscriptions:

import { defineStore } from 'pinia'
import { useValidator, required, email } from 'arv-validate-vue'

export const useAuthStore = defineStore('auth', () => {
  const v = useValidator(
    { email: '', password: '' },
    {
      email: [required(), email()],
      password: [required()]
    }
  )

  const logout = () => {
    v.dispose() // Manually cleans up listeners on logout
  }

  return { v, logout }
})

API Reference (useValidator)

useValidator(initialData, schema, options) returns:

PropertyTypeDescription
v.valuesProxy (reactive)Two-way reactive values object (v-model)
v.isValidbooleanIs form valid? (reactive in templates and computed)
v.isDirtybooleanHas any value been modified? (reactive)
v.hasError(field)functionDoes field have an error and has been touched? (reactive)
v.getError(field)functionError message string or null (reactive)
v.isValidating(field?)functionIs an async validation rule running?
v.touch(field)functionMark field as touched (@blur)
v.untouch(field)functionUnmark field as touched
v.validate()async functionTouches all fields and runs all validations
v.reset(newValues?)functionResets form to clean initial snapshot or updates with new values
v.setValues(patch)functionUpdates multiple fields simultaneously
v.getPayload()functionReturns sanitized data ready for API
v.dispose()functionManual subscription cleanup

Documentation


License

MIT © jccallo

版本与发布记录

arv-validate-vue 共发布 7 个版本,首次发布于 2026/10/24;过去 12 个月发布了 7 个版本。

版本发布日期距今
1.1.4latest2026/11/03今天
1.1.32026/10/259 天前
1.1.22026/10/2410 天前
1.1.12026/10/2410 天前
1.1.02026/10/2410 天前
1.0.02026/10/2410 天前

关于 arv-validate-vue 的常见问题

arv-validate-vue 是什么?

官方响应式Vue 3适配器,用于arv-validate (Composition API, Vite, next)

如何安装 arv-validate-vue?

在项目目录运行 npm install arv-validate-vue,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(^3.0.0)、arv-validate(^1.1.4)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

arv-validate-vue 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

arv-validate-vue 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

arv-validate-vue 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

arv-validate-vue 可以商用吗?

arv-validate-vue 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

arv-validate-vue 还在维护吗?

最新版本 1.1.4 发布于 2026/11/03(今天);至今共发布 7 个版本,其中过去 12 个月发布了 7 个;首次发布于 2026/10/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。