@felte/validator-yup

中文文档
1.1.4发布于 2024/10/30MITTypeScriptESM + CJS近 30 天下载 2.9万

A package to use Yup with Felte

原始使用文档
文档目录5 节
  1. Installation
  2. Usage
  3. Casting values
  4. Warnings
  5. Typescript

@felte/validator-yup

Tests Bundle size NPM Version codecov

A package to help you handle validation with Yup in Felte.

Installation

npm install --save @felte/validator-yup yup

# Or, if you use yarn

yarn add @felte/validator-yup yup

Usage

Call validator with an object containing your Yup schema in the schema property. The result of the call can be passed as an extender to Felte:

import { validator } from '@felte/validator-yup';
import * as yup from 'yup';

const schema = yup.object({
  email: yup.string().email().required(),
  password: yup.string().required(),
});

const { form } = createForm({
  // ...
  extend: validator({ schema }), // or `extend: [validator({ schema })],`
  // ...
});

OR use the validateSchema function directly in the validate option of createForm. (No need to extend Felte).

import { validateSchema } from '@felte/validator-yup';
import * as yup from 'yup';

const schema = yup.object({
  email: yup.string().email().required(),
  password: yup.string().required(),
});

const { form } = createForm({
  // ...
  validate: validateSchema(schema),
  // ...
});

Casting values

Unlike yup, by default this validator does not cast values. If you wish to have this behaviour you may set the castValues property to true on the validator's configuration:

const { form } = createForm({
  //...
  extend: validator({ schema, castValues: true }),
  //...
});

NOTE: yup throws if your schema fails to cast, we do not catch this errors so make sure your schema handles this appropriately to prevent your app from crashing. For example, lets assume we have a text input that should be cast to an integer. yup's number rule would throw an error if a string is set so, in order to prevent your app from crashing, a custom rule would be needed:

const schema = yup.object({
  shouldBeNumber: yup
    .mixed()
    .test('number', 'Must be a number', value => !isNaN(value))
    .transform(value => parseInt(value, 10)),
});

Warnings

Optionally, you can tell this package to assign the results of your validations to your warnings store by setting the level property of the validator function to warning. It's error by default:

import { validator } from '@felte/validator-yup';
import * as yup from 'yup';

const validateSchema = yup.object({
  email: yup.string().email().required(),
  password: yup.string().required(),
});

// We only warn if the user has already started typing a value
const warnSchema = yup.object({
  password: yup
    .string()
    .test('is-secure', 'password is not secure', (value) =>
      value ? value.length > 8 : true
    ),
});

const { form } = createForm({
  // ...
  extend: [
    validator({ schema }),
    validator({ schema: warnSchema, level: 'warning' }),
  ],
  // ...
});

Typescript

Yup allows you to infer the type of your schema using yup.InferType. This can be used so you don't need to create a type for your form's data:

import * as yup from 'yup';

const schema = yup.object({
  email: yup.string().email().required(),
  password: yup.string().required(),
});

const { form } = createForm<yup.InferType<typeof schema>>(/* ... */);

版本与发布记录

@felte/validator-yup 共发布 55 个版本,首次发布于 2021/03/22;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.4latest2024/10/302 年前
1.1.32023/08/303 年前
1.1.22023/06/173 年前
1.1.12023/06/153 年前
1.0.112023/01/123 年前
1.0.102022/09/274 年前

其他发布通道:next → 1.0.0-next.22。

关于 @felte/validator-yup 的常见问题

@felte/validator-yup 是什么?

A package to use Yup with Felte

如何安装 @felte/validator-yup?

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

@felte/validator-yup 支持 TypeScript 吗?

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

@felte/validator-yup 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@felte/validator-yup 需要什么版本的 Node.js?

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

@felte/validator-yup 可以商用吗?

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

@felte/validator-yup 的下载量有多少?

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

@felte/validator-yup 还在维护吗?

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