@eggjs/ajv-formats

中文文档
3.0.1发布于 2024/04/07MITTypeScriptCommonJS近 30 天下载 4146

Format validation for Ajv v7+

原始使用文档
文档目录6 节
  1. Usage
  2. Formats
  3. Keywords to compare values: formatMaximum / formatMinimum a…
  4. Options
  5. Tests
  6. License

ajv-formats

forked: remove peerDependencies and let it can dependency by eggjs.

JSON Schema formats for Ajv

Build Status npm Gitter GitHub Sponsors

Usage

// ESM/TypeScript import
import Ajv from "ajv"
import addFormats from "ajv-formats"
// Node.js require:
const Ajv = require("ajv")
const addFormats = require("ajv-formats")

const ajv = new Ajv()
addFormats(ajv)

Formats

The package defines these formats:

See regular expressions used for format validation and the sources that were used in formats.ts.

Please note: JSON Schema draft-07 also defines formats iri, iri-reference, idn-hostname and idn-email for URLs, hostnames and emails with international characters. These formats are available in ajv-formats-draft2019 plugin.

Keywords to compare values: formatMaximum / formatMinimum and formatExclusiveMaximum / formatExclusiveMinimum

These keywords allow to define minimum/maximum constraints when the format keyword defines ordering (compare function in format definition).

These keywords are added to ajv instance when ajv-formats is used without options or with option keywords: true.

These keywords apply only to strings. If the data is not a string, the validation succeeds.

The value of keywords formatMaximum/formatMinimum and formatExclusiveMaximum/formatExclusiveMinimum should be a string or $data reference. This value is the maximum (minimum) allowed value for the data to be valid as determined by format keyword. If format keyword is not present schema compilation will throw exception.

When these keyword are added, they also add comparison functions to formats "date", "time" and "date-time". User-defined formats also can have comparison functions. See addFormat method.

require("ajv-formats")(ajv)

const schema = {
  type: "string",
  format: "date",
  formatMinimum: "2016-02-06",
  formatExclusiveMaximum: "2016-12-27",
}

const validDataList = ["2016-02-06", "2016-12-26"]

const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]

Options

Options can be passed via the second parameter. Options value can be

  1. The list of format names that will be added to ajv instance:
addFormats(ajv, ["date", "time"])

Please note: when ajv encounters an undefined format it throws exception (unless ajv instance was configured with strict: false option). To allow specific undefined formats they have to be passed to ajv instance via formats option with true value:

const ajv = new Ajv({formats: {date: true, time: true}}) // to ignore "date" and "time" formats in schemas.
  1. Format validation mode (default is "full") with optional list of format names and keywords option to add additional format comparison keywords:
addFormats(ajv, {mode: "fast"})

or

addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})

In "fast" mode the following formats are simplified: "date", "time", "date-time", "iso-time", "iso-date-time", "uri", "uri-reference", "email". For example, "date", "time" and "date-time" do not validate ranges in "fast" mode, only string structure, and other formats have simplified regular expressions.

Tests

npm install
git submodule update --init
npm test

License

MIT

版本与发布记录

@eggjs/ajv-formats 共发布 1 个版本,首次发布于 2024/04/07;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.1latest2024/04/072 年前

关于 @eggjs/ajv-formats 的常见问题

@eggjs/ajv-formats 是什么?

Format validation for Ajv v7+

如何安装 @eggjs/ajv-formats?

在项目目录运行 npm install @eggjs/ajv-formats,或使用 pnpm add / yarn add / bun add。

@eggjs/ajv-formats 支持 TypeScript 吗?

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

@eggjs/ajv-formats 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@eggjs/ajv-formats 可以商用吗?

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

@eggjs/ajv-formats 的下载量有多少?

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

@eggjs/ajv-formats 还在维护吗?

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