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

Validator.js — String Validation and Sanitization for JavaScript

A library of string validators and sanitizers for JavaScript and Node.js covering emails, URLs, credit cards, and dozens of other formats.

Agent 就绪

这个资产会安全暂存

这个资产会先安全暂存。复制的指令会要求 Agent 读取暂存文件,并在激活脚本、MCP 配置或全局配置前先确认。

Stage only · 17/100策略:需暂存
Agent 入口
任意 MCP/CLI Agent
类型
Script
安装
Stage only
信任
信任等级:Established
入口
Validator.js
安全暂存命令
npx -y tokrepo@latest install 040a0766-75ce-11f1-9bc6-00163e2b0d79 --target codex

先暂存文件;激活前需要读取暂存 README 和安装计划。

Introduction

Validator.js is a library of string validators and sanitizers for JavaScript. It runs in both Node.js and browser environments, giving you a single dependency for checking emails, URLs, UUIDs, credit card numbers, and many more common formats.

What Validator.js Does

  • Validates over 80 string formats including email, URL, IP, UUID, and ISO dates
  • Sanitizes input by trimming, escaping HTML, normalizing emails, and stripping low bytes
  • Works in Node.js and the browser with zero external dependencies
  • Provides locale-aware validation for phone numbers, postcodes, and tax IDs
  • Supports custom validation by composing built-in checks

Architecture Overview

Validator.js is a pure-function library with no classes or state. Each validator and sanitizer is an independent function that takes a string and returns a boolean or transformed string. The library ships as a single ES module with tree-shakeable named exports, keeping bundle size small when you import only what you need.

Self-Hosting & Configuration

  • Install via npm, yarn, or pnpm for Node.js projects
  • Import individual validators to reduce bundle size in frontend builds
  • No configuration files are needed as each function is self-contained
  • Use the sanitizer functions at your API boundary before storing user input
  • Pair with schema libraries like Zod or Yup for object-level validation

Key Features

  • Over 80 built-in validators covering common web input patterns
  • Locale-specific checks for phone numbers and postal codes across 50+ countries
  • Lightweight with zero dependencies and a small bundle footprint
  • Consistent API where every function takes a string and options object
  • Active maintenance with regular updates for new validation rules

Comparison with Similar Tools

  • Zod — validates typed objects and schemas; Validator.js focuses on raw string checks
  • Yup — schema-based with async support; Validator.js is synchronous string-only
  • joi — Hapi ecosystem schema validator; Validator.js is framework-agnostic
  • class-validator — decorator-based for TypeScript classes; Validator.js uses plain functions
  • Valibot — modular schema builder; Validator.js is a flat collection of string utilities

FAQ

Q: Can I use Validator.js in the browser? A: Yes. It ships as an ES module and works in any modern browser or bundler.

Q: Does it validate objects or only strings? A: Only strings. For object validation, pair it with a schema library.

Q: Is it safe to rely on for security-critical input? A: It is widely used for input sanitization, but always combine it with server-side checks and parameterized queries.

Q: How do I add a custom validator? A: Compose existing validators in a wrapper function; the library does not have a plugin API.

Sources

讨论

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

相关资产