# @ember-intl/cp-validations 中文文档

规范地址：https://npmcn.com/package/%40ember-intl/cp-validations/

版本：8\.0\.0
npm 元数据更新时间：2026-10-20T12:23:52.234Z
中文译文更新时间：2026-11-13T15:00:46.429Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ember-intl/cp-validations
原文对照：https://npmcn.com/package/%40ember-intl/cp-validations/?lang=original
源码仓库：https://github.com/ember-intl/cp-validations

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：10.9 kB，6 个文件
- TypeScript 类型：未声明
- Node.js 要求：22\.\* \|\| \>= 24
- 运行依赖：3 个
- peer 依赖：ember-intl ^8\.0\.0 \|\| ^9\.0\.0，ember-cp-validations ^7\.0\.0
- 首次发布：2017-08-04T08:46:51.075Z
- 最新版本发布：2026-10-20T12:23:49.952Z
- 近 30 天下载量：29203（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 1 个）

## 中文简介

嵌入式支持嵌入式验证

## 安装

```sh
npm install @ember-intl/cp-validations
```

也可以使用：`pnpm add @ember-intl/cp-validations`、`yarn add @ember-intl/cp-validations`、`bun add @ember-intl/cp-validations`

## README

[![This project uses GitHub Actions for continuous integration.](https://github.com/ember-intl/cp-validations/actions/workflows/ci.yml/badge.svg)](https://github.com/ember-intl/cp-validations/actions/workflows/ci.yml) [![Ember Observer Score](https://emberobserver.com/badges/-ember-intl-cp-validations.svg)](https://emberobserver.com/addons/@ember-intl/cp-validations)

# @ember-intl/cp-验证

添加对 [嵌入层](https://github.com/yahoo/ember-intl) 在嵌入式验证中

## 安装

```sh
ember install @ember-intl/cp-validations
```

## 配置

在您的翻译中执行下列验证信息:

```yaml
# <project root>/translations/en.yaml
errors:
  description: "This field"
  inclusion: "{description} is not included in the list"
  exclusion: "{description} is reserved"
  invalid: "{description} is invalid"
  confirmation: "{description} doesn't match {on}"
  accepted: "{description} must be accepted"
  empty: "{description} can't be empty"
  blank: "{description} can't be blank"
  present: "{description} must be blank"
  collection: "{description} must be a collection"
  singular: "{description} can't be a collection"
  tooLong: "{description} is too long (maximum is {max} characters)"
  tooShort: "{description} is too short (minimum is {min} characters)"
  before: "{description} must be before {before}"
  after: "{description} must be after {after}"
  wrongDateFormat: "{description} must be in the format of {format}"
  wrongLength: "{description} is the wrong length (should be {is} characters)"
  notANumber: "{description} must be a number"
  notAnInteger: "{description} must be an integer"
  greaterThan: "{description} must be greater than {gt}"
  greaterThanOrEqualTo: "{description} must be greater than or equal to {gte}"
  equalTo: "{description} must be equal to {is}"
  lessThan: "{description} must be less than {lt}"
  lessThanOrEqualTo: "{description} must be less than or equal to {lte}"
  otherThan: "{description} must be other than {value}"
  odd: "{description} must be odd"
  even: "{description} must be even"
  positive: "{description} must be positive"
  date: "{description} must be a valid date"
  email: "{description} must be a valid email address"
  phone: "{description} must be a valid phone number"
  url: "{description} must be a valid url"
```

### 翻译验证器描述

翻译验证人的描述时具体说明: `descriptionKey` 以匹配翻译中的密钥.

```js
// app/models/user.js

import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  username: validator('presence', {
    presence: true,
    descriptionKey: 'key.for.username'
  })
});
```

```yml
# translations/en-us.yml
key:
  for:
    username: 'Username'
```

```yml
# translations/sv-se.yml
key:
  for:
    username: 'Användarnamn'
```

### 自定义前缀

更改错误前缀密钥 `errors` 对任何其他键,例如: `validationErrors` 您只需在 `app/validators/messages.js`现在只需要修改您的翻译文件, 以嵌入在 `validationErrors` 对象代替 `errors`。 。 。 .

```js
// app/validators/messages.js

import ValidatorsMessages from '@ember-intl/cp-validations/validators/messages';

export default ValidatorsMessages.extend({
  prefix: 'validationErrors'
});
```

### 覆盖默认翻译密钥

默认情况下,将解决翻译问题 `validatorPrefix.validatorType`。如果需要完全覆盖此功能并指定您自己的信件密钥,您可以使用 `messageKey` 在验证对象上.

```js
// app/models/user.js

import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  username: validator('presence', {
    presence: true,
    messageKey: 'username.missing'
  })
});
```

```yml
# translations/en-us.yml
username:
  missing: '{description} is missing'
```

```yml
# translations/sv-se.yml
username:
  missing: '{description} saknas'
```

### 禁用缺失的翻译警告

要压制控制台对缺少翻译的警告, 您可以设置 `intl_cp_validations.suppressWarnings` 内 `config/environment`· ;

```js
// config/environment.js
module.exports = function(environment) {
  const ENV = {};

  if (environment === 'test') {
    ENV.intl_cp_validations = ENV.intl_cp_validations || {};
    ENV.intl_cp_validations.suppressWarnings = true;
  }

  return ENV;
}
```

## 兼容性

-   `ember-cp-validations` ưμ㼯a
-   `ember-intl` v8 或以上区域
-   Ember.js v4.12 或以上 (中文(简体) )
-   节点.js v22 或以上

## 捐款

见 [捐款](https://npmcn.com/package/%40ember-intl/cp-validations/CONTRIBUTING.md) 详细指南.

## 许可证

本项目根据以下规定获得许可证: [MIT 许可证](https://npmcn.com/package/%40ember-intl/cp-validations/LICENSE.md)。 。 。 .

## 常见问题

### @ember-intl/cp-validations 是什么？

嵌入式支持嵌入式验证

### 如何安装 @ember-intl/cp-validations？

在项目目录运行 npm install @ember-intl/cp-validations，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：ember-intl（^8\.0\.0 \|\| ^9\.0\.0）、ember-cp-validations（^7\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @ember-intl/cp-validations 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/ember-intl\_\_cp-validations 是否存在。

### @ember-intl/cp-validations 需要什么版本的 Node\.js？

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

### @ember-intl/cp-validations 可以商用吗？

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

### @ember-intl/cp-validations 的下载量有多少？

根据 npm 官方下载统计，@ember-intl/cp-validations 在 2026/10/14 – 2026/11/12 这 30 天内被下载 29,203 次，日均约 973 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @ember-intl/cp-validations 还在维护吗？

最新版本 8\.0\.0 发布于 2026/10/20（24 天前）；至今共发布 8 个版本，其中过去 12 个月发布了 1 个；首次发布于 2017/08/04。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

