schema-sync-validator
v1.0.0
Published
A lightweight, zero-dependency, chainable data validation library for Node.js and browsers. A fast alternative to Zod/Joi.
Maintainers
Readme
schema-sync-validator
A lightweight, zero-dependency, chainable data validation library for Node.js and browsers. A faster and simpler alternative to Zod and Joi, designed for both frontend form validation and backend API validation from a single schema definition.
Features
- 🚀 Zero dependencies — extremely lightweight and fast.
- ⛓️ Chainable API — fluent, readable schema definitions (
.string().min(3).max(50).email()). - 🧩 Rich Type Support — built-in schemas for strings, numbers, booleans, dates, arrays, and nested objects.
- ✅ Validation + Transformation — validate and transform data in one pass (
.trim(),.lowercase(),.transform()). - 📦 Object Utilities —
.extend(),.pick(),.omit(),.strict(), and.passthrough()for flexible object schemas. - 🏷️ Custom Labels —
.label('Email Address')for user-friendly error messages. - 🎯 Custom Rules —
.refine(fn, msg)for any custom validation logic. - 🔧 Defaults —
.default(value)for fallback values. - ❓ Optional & Nullable —
.optional()and.nullable()support.
Installation
npm install schema-sync-validatorQuick Start
const v = require('schema-sync-validator');
// Define a user registration schema
const registerSchema = v.object({
username: v.string().min(3).max(20).trim().lowercase(),
email: v.string().email().label('Email Address'),
password: v.string().min(8, 'Password must be at least 8 characters.'),
age: v.number().integer().min(13).optional(),
role: v.string().enum(['user', 'admin']).default('user'),
tags: v.array(v.string()).nonempty().optional()
});
// Validate input data
const result = registerSchema.validate({
username: ' JohnDoe ',
email: '[email protected]',
password: 'secure123',
age: 25
});
if (result.success) {
console.log('Valid data:', result.data);
// { username: 'johndoe', email: '[email protected]', password: 'secure123', age: 25, role: 'user' }
} else {
console.log('Errors:', result.errors);
}Schema Types
String
v.string()
.min(3) // Minimum length
.max(100) // Maximum length
.email() // Must be valid email
.url() // Must be valid URL
.pattern(/regex/) // Must match regex
.enum(['a', 'b']) // Must be one of
.nonempty() // Cannot be empty string
.trim() // Auto-trim whitespace
.lowercase() // Auto-lowercase
.uppercase() // Auto-uppercaseNumber
v.number()
.min(0) // Minimum value
.max(1000) // Maximum value
.integer() // Must be integer
.positive() // Must be > 0
.port() // Valid port (1-65535)Boolean, Date
v.boolean()
v.date().min('2020-01-01').max('2030-12-31')Array
v.array(v.string()) // Array of strings
.min(1) // Min items
.max(10) // Max items
.nonempty() // Cannot be emptyObject
const userSchema = v.object({
name: v.string(),
email: v.string().email()
});
// Utilities
userSchema.strict() // Disallow unknown keys
userSchema.passthrough() // Allow & preserve unknown keys
userSchema.extend({ age: v.number() }) // Add new fields
userSchema.pick(['name']) // Keep only specified fields
userSchema.omit(['email']) // Remove specified fieldsCommon Methods (All Types)
.optional() // undefined is OK
.nullable() // null is OK
.default('value') // Fallback if undefined
.label('Field') // Custom error label
.transform(fn) // Transform after validation
.refine(fn, msg) // Custom validation ruleLicense
MIT
