@fanboykun/doz-validator@1.0.2Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
A powerful, type-safe validation library for TypeScript/JavaScript with zero dependencies.
Doz Validator
A powerful, type-safe validation library for TypeScript/JavaScript with zero dependencies.
Features
- 🎯 Type-safe validation
- 🚀 Zero dependencies
- 💪 Extensive validation rules
- 🔄 Composable validation
- 📝 Clear error messages
- 🌟 TypeScript support
Installation
deno add jsr:@fanboykun/doz-validator
Quick Start
import { Rule, Validate } from "doz-validator"; const validation = new Validate({ name: Rule.string("John"), age: Rule.number(25, { min: 0, max: 120 }), email: Rule.email("john@example.com"), }); if (validation.result.valid) { console.log("Valid data:", validation.result.data); } else { console.log("Validation errors:", validation.result.exception); }
Available Validation Rules
Basic Types
Rule.string(value)
Validates that the input is a non-empty string.
Rule.string("hello"); // ✅ Rule.string(""); // ❌ "must be string"
Rule.number(value, options?)
Validates numbers with optional min/max constraints.
Rule.number(25, { min: 0, max: 100 }); // ✅ Rule.number(-1, { min: 0 }); // ❌ "must be greater than 0"
Rule.boolean(value)
Validates boolean values.
Rule.boolean(true); // ✅ Rule.boolean("true"); // ❌ "must be boolean"
Arrays
Rule.array(value, options?)
Validates arrays with optional length constraints.
Rule.array([1, 2, 3], { minLength: 1, maxLength: 5 }); // ✅ Rule.array([], { minLength: 1 }); // ❌ "length must be at least 1"
Rule.arrayOf(value, validator)
Validates array elements using a specified validator.
Rule.arrayOf([1, 2, 3], Rule.number); // ✅ Rule.arrayOf([1, "2", 3], Rule.number); // ❌ "Item at index 1: must be number"
Rule.arrayIncludes(value, items)
Validates that an array includes all specified items.
Rule.arrayIncludes(["a", "b", "c"], ["a", "b"]); // ✅ Rule.arrayIncludes(["a"], ["a", "b"]); // ❌ "must include all of these items: b"
Objects
Rule.object(value)
Validates that the input is an object.
Rule.object({ key: "value" }); // ✅ Rule.object(null); // ❌ "must be object"
Rule.shape(value, shape)
Validates object properties against specified validators.
Rule.shape( { name: "John", age: 25 }, { name: Rule.string, age: Rule.number }, ); // ✅
Rule.hasProperties(value, properties)
Validates that an object has specified properties.
Rule.hasProperties({ a: 1, b: 2 }, ["a", "b"]); // ✅ Rule.hasProperties({ a: 1 }, ["a", "b"]); // ❌ "must have all of these properties: b"
Special Types
Rule.email(value)
Validates email addresses.
Rule.email("test@example.com"); // ✅ Rule.email("invalid-email"); // ❌ "must be valid email address"
Rule.date(value)
Validates dates.
Rule.date("2023-12-25"); // ✅ Rule.date("invalid-date"); // ❌ "must be valid date"
Rule.url(value, options?)
Validates URLs with optional protocol restrictions.
Rule.url("https://example.com", { protocols: ["https"] }); // ✅ Rule.url("ftp://example.com", { protocols: ["https"] }); // ❌ "must use one of these protocols: https"
Rule.password(value, options?)
Validates passwords with customizable requirements.
Rule.password("Test123!", { minLength: 8, requireUppercase: true, requireNumbers: true, requireSpecialChars: true, }); // ✅
Rule.uuidv4(value)
Validates UUIDv4 strings.
Rule.uuidv4("123e4567-e89b-4d3c-8456-426614174000"); // ✅ Rule.uuidv4("invalid-uuid"); // ❌ "must be a valid UUIDv4"
Rule.ip(value, allowLocal?)
Validates IP addresses (v4 and v6).
Rule.ip("192.168.1.1", true); // ✅ (allows local IPs) Rule.ip("256.256.256.256"); // ❌ "must be valid IPv4 or IPv6 address"
Modifiers
Rule.nullable(validator)
Makes a validator accept null values.
Rule.nullable(Rule.string)(null); // ✅ Rule.nullable(Rule.string)("hello"); // ✅
Rule.optional(validator)
Makes a validator accept undefined values.
Rule.optional(Rule.string)(undefined); // ✅ Rule.optional(Rule.string)("hello"); // ✅
Advanced Usage
Composing Validations
const userValidation = new Validate({ user: Rule.shape({ name: Rule.string("John"), age: Rule.number(25, { min: 18 }), email: Rule.email("john@example.com"), preferences: Rule.shape({ newsletter: Rule.boolean(true), theme: Rule.string("dark"), }), }), });
Optional Fields
const validation = new Validate({ requiredField: Rule.string("hello"), optionalField: Rule.optional(Rule.string)(undefined), nullableField: Rule.nullable(Rule.number)(null), });
Error Handling
The validation result will be either:
// Success case { valid: true, data: { // Your validated data } } // Error case { valid: false, exception: { // Field-specific error messages } }
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Add Package
deno add jsr:@fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";
Import directly with a jsr specifier
import * as doz_validator from "jsr:@fanboykun/doz-validator";
Add Package
pnpm i jsr:@fanboykun/doz-validator
pnpm dlx jsr add @fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";
Add Package
yarn add jsr:@fanboykun/doz-validator
yarn dlx jsr add @fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";
Add Package
vlt install jsr:@fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";
Add Package
npx jsr add @fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";
Add Package
bunx jsr add @fanboykun/doz-validator
Import symbol
import * as doz_validator from "@fanboykun/doz-validator";