@ember-intl/cp-validations

中文文档
8.0.0发布于 2026/10/20MIT构建来源已验证近 30 天下载 2.9万

ember-intl support for ember-cp-validations

原始使用文档
文档目录9 节
  1. Installation
  2. Configuration
  3. Translating Validator description
  4. Customizing the prefix
  5. Overriding default translation key
  6. Disabling Missing Translation Warnings
  7. Compatibility
  8. Contributing
  9. License

This project uses GitHub Actions for continuous integration. Ember Observer Score

@ember-intl/cp-validations

Adds support for ember-intl in ember-cp-validations

Installation

ember install @ember-intl/cp-validations

Configuration

Implement the following validation messages across your translations:

# <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"

Translating Validator description

To translate the description of a Validator specify the descriptionKey to match a key in your translations.

// app/models/user.js

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

const Validations = buildValidations({
  username: validator('presence', {
    presence: true,
    descriptionKey: 'key.for.username'
  })
});
# translations/en-us.yml
key:
  for:
    username: 'Username'
# translations/sv-se.yml
key:
  for:
    username: 'Användarnamn'

Customizing the prefix

To change the errors prefix key from errors to any other key, such as validationErrors you simply add the following to app/validators/messages.js. Now just amend your translation files to be nested under the validationErrors object instead of errors.

// app/validators/messages.js

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

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

Overriding default translation key

By default, translations will be resolved to validatorPrefix.validatorType. If you need to override this functionality entirely and specify your own message key, you can do so with messageKey on the validator object.

// app/models/user.js

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

const Validations = buildValidations({
  username: validator('presence', {
    presence: true,
    messageKey: 'username.missing'
  })
});
# translations/en-us.yml
username:
  missing: '{description} is missing'
# translations/sv-se.yml
username:
  missing: '{description} saknas'

Disabling Missing Translation Warnings

To suppress console warnings for missing translations, you can do so by setting intl_cp_validations.suppressWarnings in config/environment;

// 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;
}

Compatibility

  • ember-cp-validations v7
  • ember-intl v8 or above
  • Ember.js v4.12 or above
  • Node.js v22 or above

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

版本与发布记录

@ember-intl/cp-validations 共发布 8 个版本,首次发布于 2017/08/04;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.0.0latest2026/10/2024 天前
7.0.22024/07/102 年前
7.0.12024/07/102 年前
7.0.02024/07/102 年前
6.0.02023/05/313 年前
5.0.02022/09/194 年前

关于 @ember-intl/cp-validations 的常见问题

@ember-intl/cp-validations 是什么?

ember-intl support for ember-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 和提交记录判断。