@ampproject/toolbox-validator-rules

中文文档
2.10.1发布于 2024/06/14Apache-2.0CommonJS近 30 天下载 14万

A library that helps query AMP Validator rules

原始使用文档
文档目录5 节
  1. Usage
  2. Including the Module
  3. Using the module
  4. Format of rules
  5. Options

AMP-Toolbox Validator Rules

npm version

Queries published AMP Validator rules and extracts information about required markup and attributes for all AMP formats.

Usage

Install via:

$ npm install @ampproject/toolbox-validator-rules

Including the Module

ES Module (Browser)

import validatorRules from '@ampproject/toolbox-validator-rules';

CommonJs (Node)

const validatorRules = require('@ampproject/toolbox-validator-rules');

Using the module

  // Loads the validator rules remotely with default options
  const rules = await validatorRules.fetch();


  // The raw unprocessed rules
  console.log(rules.raw);

  // All tags, combined with their respective attribute lists
  console.log(rules.tags);

  // All extensions
  console.log(rules.extensions);

  // Get all tag names used in AMP for Email
  // The supported formats are AMP, AMP4EMAIL, AMP4ADS and ACTIONS
  const tags = rules.getTagsForFormat('AMP4EMAIL');

  // Display their names
  console.log(tags.map(tag => tag.tagName));

  // Get information about an extension
  const ext = rules.getExtension('AMP4EMAIL', 'amp-carousel');

  // Display supported versions
  console.log(ext.versions);

Format of rules

The rules used closely follow the proto definitions from validator.proto.

Specifically:

  • The raw property is unprocessed ValidatorRules, the same format used by https://cdn.ampproject.org/v0/validator.json
  • The result of getTagsForFormat and the tags property is a list of TagSpec
  • The result of getExtension is ExtensionSpec with the htmlFormat field from TagSpec
  • The extensions property a list of ExtensionSpec with the htmlFormat field from TagSpec
  • The errors property combines ErrorFormat and ErrorSpecificity

Options

fetch optionally accepts an options object allowing you to customize its behaviour.

The following options are supported:

  • noCache: true to always fetch latest rules (by default, subsequent calls to fetch reuse the same result).
  • rules: object to use locally specified rules instead of fetching them from the AMP CDN.
  • url: override the URL where validator rules are fetched from.
  • source: one of 'local' (load rules from local file named "validator.json"), 'remote' (fetch rules from CDN) or 'auto' which is the default (tries looking for the local file first, then tries to fetch from CDN).

Example:

validatorRules.fetch({
  noCache: true,
  source: 'remote'
});

版本与发布记录

@ampproject/toolbox-validator-rules 共发布 29 个版本,首次发布于 2019/07/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.10.1latest2024/06/142 年前
2.10.02024/06/142 年前
2.9.02022/06/244 年前
3.0.0-canary.2canary2022/01/124 年前
2.8.02021/05/315 年前
2.8.0-canary.92021/03/225 年前

其他发布通道:beta → 1.1.0-beta.0,alpha → 2.7.1-alpha.0,canary → 3.0.0-canary.2。

关于 @ampproject/toolbox-validator-rules 的常见问题

@ampproject/toolbox-validator-rules 是什么?

A library that helps query AMP Validator rules

如何安装 @ampproject/toolbox-validator-rules?

在项目目录运行 npm install @ampproject/toolbox-validator-rules,或使用 pnpm add / yarn add / bun add。

@ampproject/toolbox-validator-rules 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/ampproject__toolbox-validator-rules 是否存在。

@ampproject/toolbox-validator-rules 支持 ESM 还是 CommonJS?

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

@ampproject/toolbox-validator-rules 可以商用吗?

@ampproject/toolbox-validator-rules 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@ampproject/toolbox-validator-rules 的下载量有多少?

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

@ampproject/toolbox-validator-rules 还在维护吗?

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