The perfect, feature-complete, chainable schema-based object validator for Node.js and browsers. Inspired by Joi, rebuilt for ultimate power and flexibility with zero dependencies.
This is the definitive version of chain-schema-validator, designed to handle virtually any validation scenario you can imagine. It provides an expressive, fluent API to define schemas, transform data, and validate complex objects with ease.
-
Massive & Comprehensive API: A huge library of validation methods for every data type.
-
Powerful Data Transformation: A dedicated pipeline for sanitizing, formatting, and setting defaults on your data before validation.
-
Advanced Conditional & Relational Logic: Define complex cross-field dependencies with rules like
when,with, andswitch-like logic usingkeys. -
Deeply Nested Validation: Effortlessly validate complex nested objects and arrays.
-
Asynchronous Support: Built-in async capabilities for tasks like database lookups.
-
Zero Dependencies: Lightweight, fast, and secure.
Define individual field schemas, then compose them into an object schema.
const { schema, ref } = require('chain-schema-validator');
// 1. Define schemas for individual fields
const usernameSchema = schema.string()
.trim()
.lowercase()
.token()
.min(3)
.required();
const passwordSchema = schema.string()
.min(8)
.required();
// 2. Compose into an object schema
const registrationSchema = schema.object({
username: usernameSchema,
email: schema.string().email().required(),
password: passwordSchema,
passwordConfirm: schema.string().valid(ref('password')).required().strip(),
role: schema.string().default('user'),
birthYear: schema.number().integer().min(1920).max(2015)
}, {
abortEarly: false // Report all errors
});
// 3. Validate your data
const userData = {
username: ' TEST_USER ',
email: 'test@example.com',
password: 'password123',
passwordConfirm: 'password123',
birthYear: 1990
};
const { value, error } = registrationSchema.validate(userData);
if (error) {
console.error('❌ Validation failed:', error.details);
} else {
console.log('✅ Validation passed!');
console.log('Sanitized Value:', value);
/*
Expected output:
✅ Validation passed!
Sanitized Value: {
username: 'test_user',
email: 'test@example.com',
password: 'password123',
role: 'user',
birthYear: 1990
}
*/
}-
schema.string() -
schema.number() -
schema.boolean() -
schema.array() -
schema.object(schemaMap, options) -
schema.date() -
schema.any()
-
State Modifiers:
.required(),.optional(),.nullable(),.forbidden(),.strip() -
Defaults & Values:
.default(value),.valid(...values),.invalid(...values) -
Custom Logic:
.transform(fn),.custom(fn),.customAsync(fn) -
Schema Composition:
.concat(schema),.meta(info)
-
Transformation:
.trim(),.lowercase(),.uppercase() -
Validation:
.min(len),.max(len),.length(len),.pattern(regex),.creditCard(),.email(),.ip4(),.ip6(),.ip(),.uuid(),.hex(),.token(),.isoDate()
.min(val),.max(val),.greater(val),.less(val),.integer(),.positive(),.negative(),.port()
.min(len),.max(len),.length(len),.items(schema),.unique(),.has(schema),.single()
-
schema.object({ key: schema, ... })is the primary method. -
Options:
{ abortEarly: boolean, stripUnknown: boolean }
This library is now feature-complete and in a stable state. Bug reports are welcome. Please see our Security Policy.
If you need just object validator, then you can see objectPropValidator.
This project is licensed under the MIT License - see the LICENSE file for details.
This packages codes was created by Mamedul Islam and open for contribute.
As a passionate web developer with experience in creating interactive and user-friendly web components. Currently available for freelance projects or full-time opportunities.
Helping businesses grow their online presence with custom web solutions. Specializing in WordPress, WooCommerce, NodeJS, and Shopify. Building modern, responsive, and high-performance scalable websites with custom made plugins, codes, customizations.
Please see the CHANGELOG.md file.
