Configs2026年7月27日·1 分钟阅读

Joi — Powerful Schema Validation for JavaScript

A robust schema description language and data validator for JavaScript objects. Widely adopted in Node.js backends for validating API payloads, configuration, and form data with expressive chainable syntax.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Joi
直接安装命令
npx -y tokrepo@latest install 763a4b73-897a-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Joi is a schema description language and data validator for JavaScript. Originally developed as part of the hapi ecosystem, it has become the go-to validation library for Node.js applications that need to validate request bodies, query parameters, environment variables, or any structured data. Joi's chainable API lets developers express complex validation rules in readable, declarative code.

What Joi Does

  • Validates JavaScript objects against declarative schemas with type coercion and custom error messages
  • Supports all JavaScript primitives plus dates, arrays, objects, alternatives, and binary buffers
  • Provides conditional validation with when/then/otherwise for dynamic schema branching
  • Offers schema extension to create custom types with reusable validation logic
  • Generates detailed error objects with path information and human-readable descriptions

Architecture Overview

Joi compiles schemas into an internal validation tree at definition time. When validate() is called, the tree walks the input data, applying type checks, coercions, and constraints at each node. Errors accumulate (or abort early, depending on options) and are returned as structured ValidationError objects. Joi operates synchronously and has no external dependencies beyond its companion packages for template rendering and address parsing.

Self-Hosting & Configuration

  • Install from npm: npm install joi
  • No global configuration needed; schemas are self-contained
  • Use Joi.defaults() to set project-wide schema options
  • Import as CommonJS (require) or ESM (import) in Node.js 14+
  • Works in the browser via bundlers, though server-side use is more common

Key Features

  • Chainable fluent API expresses validation rules clearly: Joi.string().email().required()
  • Schema references let one field depend on another: Joi.ref('password') for confirm-password
  • Custom error messages with template literals and i18n support
  • Type coercion converts strings to numbers, dates, or booleans during validation
  • Schema caching and compilation ensure repeated validations are fast

Comparison with Similar Tools

  • Zod — TypeScript-first with static type inference; Joi has a larger feature set but weaker TS integration
  • Yup — Similar API but smaller; Joi handles more edge cases and has richer error formatting
  • Ajv — JSON Schema validator optimized for speed; Joi's API is more ergonomic for hand-written schemas
  • Valibot — Modular and tree-shakeable; Joi is more established with broader ecosystem support
  • superstruct — Lightweight with composable validators; Joi offers more built-in rules and coercion

FAQ

Q: Is Joi still maintained after the hapi split? A: Yes. Joi is actively maintained under the @hapi scope and receives regular updates for bug fixes and new features.

Q: Can Joi validate TypeScript types? A: Joi validates runtime data. For compile-time type inference from schemas, consider Zod. Joi can be used alongside TypeScript but does not infer types from schemas.

Q: How does Joi handle async validation? A: Joi supports async validation via schema.validateAsync() for rules that require I/O, such as checking uniqueness against a database.

Q: Is Joi suitable for frontend form validation? A: It works in the browser via bundlers, but its bundle size is larger than alternatives like Zod or Valibot. For frontend-heavy projects, a lighter library may be preferable.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产