@ember-intl/cp-validations

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

嵌入式支持嵌入式验证

中文使用文档
文档目录9 节
  1. 安装
  2. 配置
  3. 翻译验证器描述
  4. 自定义前缀
  5. 覆盖默认翻译密钥
  6. 禁用缺失的翻译警告
  7. 兼容性
  8. 捐款
  9. 许可证

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

@ember-intl/cp-验证

添加对 嵌入层 在嵌入式验证中

安装

ember install @ember-intl/cp-validations

配置

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

# <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 以匹配翻译中的密钥.

// 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'

自定义前缀

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

// app/validators/messages.js

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

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

覆盖默认翻译密钥

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

// 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'

禁用缺失的翻译警告

要压制控制台对缺少翻译的警告, 您可以设置 intl_cp_validations.suppressWarnings 内 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;
}

兼容性

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

捐款

见 捐款 详细指南.

许可证

本项目根据以下规定获得许可证: MIT 许可证。 。 。 .

版本与发布记录

@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/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 和提交记录判断。