A lightweight, type-safe validation library for TypeScript/JavaScript applications.
- 🚀 Type-safe validation schemas
- 💪 Built-in validators for common types
- 🔄 Nested object validation
- 📝 Custom validation rules
- 0️⃣ Zero dependencies
npm install @sunillakandri/data-validator
import { DataValidator } from "@sunillakandri/data-validator";
// Define schema
const schema = {
username: {
type: "string",
required: true,
minLength: 3,
},
email: {
type: "string",
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
},
};
// Create validator
const validator = new DataValidator(schema);
// Validate data
const result = validator.validate({
username: "john_doe",
email: "john@example.com",
});
required
: Field must be presenttype
: Type validation ('string', 'number', 'boolean', 'object', 'array')minLength
/maxLength
: String length limitsmin
/max
: Number value limitspattern
: RegExp pattern matchingcustom
: Custom validation functionnested
: Nested object schema
const schema = {
password: {
type: "string",
custom: (value) => {
if (!/[A-Z]/.test(value)) {
return "Need one uppercase letter";
}
if (!/[0-9]/.test(value)) {
return "Need one number";
}
},
},
};
MIT © Sunil Lakandri
Made by Sunil Lakandri