A powerful, type-safe validation library for TypeScript/JavaScript with zero dependencies.
- 🎯 Type-safe validation
- 🚀 Zero dependencies
- 💪 Extensive validation rules
- 🔄 Composable validation
- 📝 Clear error messages
- 🌟 TypeScript support
deno add jsr:@fanboykun/doz-validator
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);
}
Validates that the input is a non-empty string.
Rule.string("hello"); // ✅
Rule.string(""); // ❌ "must be string"
Validates numbers with optional min/max constraints.
Rule.number(25, { min: 0, max: 100 }); // ✅
Rule.number(-1, { min: 0 }); // ❌ "must be greater than 0"
Validates boolean values.
Rule.boolean(true); // ✅
Rule.boolean("true"); // ❌ "must be boolean"
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"
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"
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"
Validates that the input is an object.
Rule.object({ key: "value" }); // ✅
Rule.object(null); // ❌ "must be object"
Validates object properties against specified validators.
Rule.shape(
{ name: "John", age: 25 },
{ name: Rule.string, age: Rule.number },
); // ✅
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"
Validates email addresses.
Rule.email("test@example.com"); // ✅
Rule.email("invalid-email"); // ❌ "must be valid email address"
Validates dates.
Rule.date("2023-12-25"); // ✅
Rule.date("invalid-date"); // ❌ "must be valid date"
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"
Validates passwords with customizable requirements.
Rule.password("Test123!", {
minLength: 8,
requireUppercase: true,
requireNumbers: true,
requireSpecialChars: true,
}); // ✅
Validates UUIDv4 strings.
Rule.uuidv4("123e4567-e89b-4d3c-8456-426614174000"); // ✅
Rule.uuidv4("invalid-uuid"); // ❌ "must be a valid UUIDv4"
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"
Makes a validator accept null values.
Rule.nullable(Rule.string)(null); // ✅
Rule.nullable(Rule.string)("hello"); // ✅
Makes a validator accept undefined values.
Rule.optional(Rule.string)(undefined); // ✅
Rule.optional(Rule.string)("hello"); // ✅
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"),
}),
}),
});
const validation = new Validate({
requiredField: Rule.string("hello"),
optionalField: Rule.optional(Rule.string)(undefined),
nullableField: Rule.nullable(Rule.number)(null),
});
The validation result will be either:
// Success case
{
valid: true,
data: {
// Your validated data
}
}
// Error case
{
valid: false,
exception: {
// Field-specific error messages
}
}
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.