npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eggjs/tegg-ajv-plugin

v3.70.1

Published

ajv plugin for egg and tegg

Readme

@eggjs/tegg-ajv-plugin

参考 egg-typebox-validate 的最佳实践,结合 ajv + typebox,只需要定义一次参数类型和规则,就能同时拥有参数校验和类型定义(完整的 ts 类型提示)。

egg 模式

Install

# tegg 注解
npm i --save @eggjs/tegg
# tegg 插件
npm i --save @eggjs/tegg-plugin
# tegg ajv 插件
npm i --save @eggjs/tegg-ajv-plugin

Prepare

// tsconfig.json
{
  "extends": "@eggjs/tsconfig"
}

Config

// config/plugin.js
exports.tegg = {
  package: '@eggjs/tegg-plugin',
  enable: true,
};

exports.teggAjv = {
  package: '@eggjs/tegg-ajv-plugin',
  enable: true,
};

standalone 模式

Install

# tegg 注解
npm i --save @eggjs/tegg
# tegg ajv 插件
npm i --save @eggjs/tegg-ajv-plugin

Prepare

// tsconfig.json
{
  "extends": "@eggjs/tsconfig"
}

Usage

1、定义入参校验 Schema

使用 typebox 定义,会内置到 tegg 导出

import { Type, TransformEnum } from '@eggjs/tegg/ajv';

const SyncPackageTaskSchema = Type.Object({
  fullname: Type.String({
    transform: [ TransformEnum.trim ],
    maxLength: 100,
  }),
  tips: Type.String({
    transform: [ TransformEnum.trim ],
    maxLength: 1024,
  }),
  skipDependencies: Type.Boolean(),
  syncDownloadData: Type.Boolean(),
  // force sync immediately, only allow by admin
  force: Type.Boolean(),
  // sync history version
  forceSyncHistory: Type.Boolean(),
  // source registry
  registryName: Type.Optional(Type.String()),
});

2、从校验 Schema 生成静态的入参类型

import { Static } from '@eggjs/tegg/ajv';

type SyncPackageTaskType = Static<typeof SyncPackageTaskSchema>;

3、在 Controller 中使用入参类型和校验 Schema

注入全局单例 ajv,调用 ajv.validate(XxxSchema, params) 进行参数校验,参数校验失败会直接抛出 AjvInvalidParamError 异常, tegg 会自动返回相应的错误响应给客户端。

import { Inject, HTTPController, HTTPMethod, HTTPMethodEnum, HTTPBody } from '@eggjs/tegg';
import { Ajv, Type, Static, TransformEnum } from '@eggjs/tegg/ajv';

const SyncPackageTaskSchema = Type.Object({
  fullname: Type.String({
    transform: [ TransformEnum.trim ],
    maxLength: 100,
  }),
  tips: Type.String({
    transform: [ TransformEnum.trim ],
    maxLength: 1024,
  }),
  skipDependencies: Type.Boolean(),
  syncDownloadData: Type.Boolean(),
  // force sync immediately, only allow by admin
  force: Type.Boolean(),
  // sync history version
  forceSyncHistory: Type.Boolean(),
  // source registry
  registryName: Type.Optional(Type.String()),
});

type SyncPackageTaskType = Static<typeof SyncPackageTaskSchema>;

@HTTPController()
export class HelloController {
  private readonly ajv: Ajv;

  @HTTPMethod({
    method: HTTPMethodEnum.POST,
    path: '/sync',
  })
  async sync(@HTTPBody() task: SyncPackageTaskType) {
    this.ajv.validate(SyncPackageTaskSchema, task);
    return {
      task,
    };
  }
}