@convidera-team/vue-form-validator

中文文档
1.1.5发布于 2025/01/21TypeScriptES Module零依赖近 30 天下载 2924

Installation

原始使用文档
文档目录8 节
  1. Installation
  2. Basic usage
  3. It's also possible to add validation rules after the field …
  4. Example of custom requiredIf rule
  5. Available validation rules
  6. Translations for vue-i18n
  7. Changelog
  8. 1.1.3

Form validator

Installation

yarn add @convidera-team/vue-form-validator

or

npm install @convidera-team/vue-form-validator --save

Basic usage

<script lang="ts" setup>
import {
  useFormValidator,
  isFormValidatorError,
  isAxiosError,
} from '@convidera-team/vue-form-validator';

const rules = useRules();
const form = useFormValidator();

const name = form.addField('name', '', [
  rules.required('The field is required'),
]);

const email = form.addField('email', '', [
  rules.required('The field is required'),
  rules.email('The field should be email'),
]);

const password = form.addField('password', '', [
  rules.required('The field is required'),
  rules.min(6, 'The field should be min 6 characters'),
]);

const passwordConfirmation = form.addField('password_confirmation', '', [
  rules.required('The field is required'),
  (value) => rules.confirmed(password.value.value, 'The confirmation is incorrect')(value),
]);

async function submit() {
  try {
    // If there are any validation errors, they will be thrown and apiRequest will not be called
    await form.handleSubmit(() => apiRequest({
      name: name.value.value,
      email: email.value.value,
    }));
  } catch (error) {
    if (isFormValidatorError(error)) {
      // Catch form validation errors
      console.warn(error.list);
    } else if (isAxiosError(error)) {
      // Catch axios errors
      console.warn(error.response?.data);
    } else {
      throw error;
    }
  }
}
</script>

<template>
  <form @submit.prevent="submit">
    <input
      v-model="name.value.value"
      label="Name"
      :valid="name.valid.value"
      @blur="name.validate()"
    >
    {{ name.errors.value[0] }}

    <input
      v-model="email.value.value"
      label="email"
      :valid="email.valid.value"
      @blur="email.validate()"
    >
    {{ email.errors.value[0] }}

    <input
      v-model="password.value.value"
      label="password"
      type="password"
      :valid="password.valid.value"
      @blur="password.validate()"
    >
    {{ password.errors.value[0] }}

    <input
      v-model="passwordConfirmation.value.value"
      label="passwordConfirmation"
      type="password"
      :valid="passwordConfirmation.valid.value"
      @blur="passwordConfirmation.validate()"
    >
    {{ passwordConfirmation.errors.value[0] }}

    <button
      type="submit"
    >
      submit
    </button>
  </form>
</template>

When you do form.addField('name', initialValue, [list of rules]) you give the field a name, initial value and list of validation rules. The name is used to identify the field in the form. For example in case of api error you want to show the error specified in error response for the field. if apiRequest in await form.handleSubmit(() => apiRequest()); fails due to api validation errors, the errors returned by api will be mapped to the fields in the form using the name specified during form.addField('fieldName'). So make sure to name fields in the same way as they are named in the api response.

You can run yarn dev to see basic usage in browser.

It's also possible to add validation rules after the field is created

const newPassword = form.addFiled('newPassword', '', [
  ...someValidationRules
]);

const passwordConfirmation = form.addFiled('passwordConfirmation', '', [
  (value) => rules.confirmed(newPassword.value.value)(value),
]);

const currentPassword = form.addFiled('currentPassword', '', [
  (value) => rawRules.requiredIf([
    newPassword.value.value,
    passwordConfirmation.value.value,
  ], 'Required if new password and password confirmation provided')(value),
]);

newPassword.addRules([
  (value) => rawRules.requiredIf([
    currentPassword.value.value,
    passwordConfirmation.value.value,
  ], 'Required if current password and password confirmation provided')(value),
]);

Example of custom requiredIf rule

You can also create your own rules if predefined do not fit you. here is a TS example of a custom requiredIf rule:

import {
  LocationTypeEnum,
} from 'project-specific';
import {
  useFormValidator,
  TFormValidatorRule,
} from '@convidera-team/vue-form-validator';

const requiredIf: (type: LocationTypeEnum) => TFormValidatorRule<undefined> = (type) => () => locationType.value.value === type;
const form = useFormValidator();
const address = form.addField(
  'address',
  undefined,
  [
    requiredIf(LocationTypeEnum.PHYSICAL),
  ],
);

Available validation rules

Please check here

Translations for vue-i18n

Important

When adding new translations, make sure to also adjust src/form-validator/translation/schema.ts file as it provides typescript support.

The package provides en and de translations which could be used to extend vue-i18n translations:

// English translation file
import {
  en as formValidation,
} from '@convidera-team/vue-form-validator';

export default {
  // other translations
  ...formValidation,
}

Then it's possible to use formValidator default translations directly without the need to specify text for each validation rule:

import {
  useI18n,
} from 'vue-i18n';
import {
  useRules
} from '@convidera-team/vue-form-validator';

const {
  t,
} = useI18n();

const rules = useRules(t);
const form = useFormValidator();

const name = form.addField('name', '', [
  rules.required(),
]);

If you don't want to use default translations but have your own, you can override translations provided by formValidator in your translation files. For this you can take the same keys provided for example in en.ts file and override them in your translation:

// Translation file
export default {
  // other translations
  formValidator: {
    required: 'Custom message for required rule',
    email: 'This field should be email',
    min: 'Custom message for min rule {min}',
    // Other overrides
  },
}

In the same way you can add translations for other languages as well.

Changelog

1.1.3

  • update dependencies

版本与发布记录

@convidera-team/vue-form-validator 共发布 8 个版本,首次发布于 2023/04/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.5latest2025/01/211.8 年前
1.1.42024/09/062 年前
1.1.32024/04/212 年前
1.1.22023/07/193 年前
1.1.12023/06/273 年前
1.1.02023/05/043 年前

关于 @convidera-team/vue-form-validator 的常见问题

@convidera-team/vue-form-validator 是什么?

Installation

如何安装 @convidera-team/vue-form-validator?

在项目目录运行 npm install @convidera-team/vue-form-validator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(>=3)、axios(>=0.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convidera-team/vue-form-validator 支持 TypeScript 吗?

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

@convidera-team/vue-form-validator 支持 ESM 还是 CommonJS?

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

@convidera-team/vue-form-validator 可以商用吗?

@convidera-team/vue-form-validator 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@convidera-team/vue-form-validator 的下载量有多少?

根据 npm 官方下载统计,@convidera-team/vue-form-validator 在 2026/10/11 – 2026/11/09 这 30 天内被下载 2,924 次,日均约 97 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@convidera-team/vue-form-validator 还在维护吗?

最新版本 1.1.5 发布于 2025/01/21(1.8 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/04/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。