@bedrock/validation

中文文档
7.3.0发布于 2026/10/03ES Module近 30 天下载 2031

Bedrock validation

原始使用文档
文档目录7 节
  1. Requirements
  2. Quick Examples
  3. Configuration
  4. API
  5. createValidateMiddleware({querySchema: schema, bodySchema: …
  6. getSchema(name)
  7. validateInstance(instance, schema)

bedrock-validation

A bedrock module that provides a express middleware and an API for validating data structures and their contents. bedrock-validation uses schemas based on JSON schema.

Requirements

This software requires and supports maintained recent versions of Node.js and browsers. Updates may remove support for older unmaintained platform versions. Please use dependency version lock files and testing to ensure compatibility with this software.

Quick Examples

npm install @bedrock/validation
import * as bedrock from '@bedrock/core';
import {postBarValidator, postBarQueryValidator} from '../schemas/my-schemas.js';
import {createValidateMiddleware as validate} from '@bedrock/validation';

// load schemas from '/foo'
bedrock.config.validation.schema.paths.push('/foo');

// add an express route with validation middleware
bedrock.events.on('bedrock-express.configure.routes', function(app) {
  app.post('/bar',
    // validate the query using the 'postBarQueryValidator'
    // validate the response body using the 'postBarValidator'
    validate({querySchema: postBarQueryValidator, bodySchema: postBarValidator}),
    function(req, res) {
      // do something
    });
});

bedrock.start();

Configuration

bedrock-validation will, on initialization (via the bedrock.init event), read any schemas found in the list of paths specified in bedrock.config.validation.schema.paths. Individual schemas can be skipped via the bedrock.config.validation.schema.skip configuration option. If any schema name matches a previously loaded schema, it will override that schema.

For more documentation on configuration, see config.js.

API

createValidateMiddleware({querySchema: schema, bodySchema: schema})

This method may be called with either query or body defined.

The method returns express middleware that will be used to validate a request using the schema associated with either query or body.

getSchema(name)

Retrieves a validation schema given a name for the schema. If no such schema exists, null is returned.

validateInstance(instance, schema)

Validates an instance (data) against a schema. This method may be used to validate data using a schema that wasn't necessarily registered via the configuration system. The schema must be a JSON schema instance. The return value will contain the result of the validation. This function returns a promise that resolves to:

{
  valid: <boolean>,
  error: <error> // only present when valid === false
}

版本与发布记录

@bedrock/validation 共发布 8 个版本,首次发布于 2022/04/02;过去 12 个月发布了 3 个版本。

版本发布日期距今
7.3.0latest2026/10/031 个月前
7.2.12026/09/181 个月前
7.2.02026/06/055 个月前
7.1.12024/11/152 年前
7.1.02022/10/174 年前
7.0.02022/04/294 年前

关于 @bedrock/validation 的常见问题

@bedrock/validation 是什么?

Bedrock validation

如何安装 @bedrock/validation?

在项目目录运行 npm install @bedrock/validation,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bedrock/core(^6.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bedrock/validation 支持 TypeScript 吗?

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

@bedrock/validation 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bedrock/validation 可以商用吗?

@bedrock/validation 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@bedrock/validation 的下载量有多少?

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

@bedrock/validation 还在维护吗?

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