@abhivorn/validation-kit
v2.0.0
Published
A comprehensive React Validation Library
Readme
@abhivorn/validation-kit
A lightweight, pure-logic React validation library. No UI components, zero dependencies.
This library provides an extensive suite of pure validation functions that can be attached directly to native HTML inputs (like onBlur or onChange) or used in any custom validation logic.
Features
- Pure Logic: Focuses exclusively on validation rules. No UI components or CSS included.
- Event-Aware: Validators can take raw strings OR native React events (
React.FocusEvent,React.ChangeEvent), making them plug-and-play for HTML inputs. - Structured Results: Every validator returns
{ isValid: boolean, error: string, value: string }. - Extensive Suite: 40+ built-in validators for emails, passwords, regional documents (PAN, Aadhaar), payments (Credit Card, CVV), networks (IP, MAC), and files.
- i18n Support: Completely customizable error messages.
Installation
npm install @abhivorn/validation-kitQuick Start
You can pass our validators directly to native React event handlers. The library automatically extracts event.target.value.
import { useState } from 'react';
import { validate } from '@abhivorn/validation-kit';
function App() {
const [emailError, setEmailError] = useState('');
const handleEmailBlur = (e) => {
const result = validate.email(e);
if (!result.isValid) {
setEmailError(result.error);
} else {
setEmailError('');
}
};
return (
<form>
<div>
<label>Email Address</label>
<input
type="email"
onBlur={handleEmailBlur}
/>
{emailError && <span style={{ color: 'red' }}>{emailError}</span>}
</div>
<div>
<label>Phone Number</label>
{/* Or validate inline */}
<input
type="tel"
onBlur={(e) => {
const { isValid, error } = validate.phone(e);
console.log(isValid ? 'Valid!' : error);
}}
/>
</div>
</form>
);
}Available Validators
Basic & String
validate.email | validate.name | validate.username | validate.password | validate.phone | validate.url | validate.domain | validate.slug
Regional & Identity (India)
validate.pan | validate.aadhaar | validate.gstin | validate.passport | validate.drivingLicense | validate.upiId | validate.ifsc | validate.swiftBic | validate.pinZip
Network & Payment
validate.ipAddress | validate.macAddress | validate.creditCard | validate.cvv | validate.expiryDate | validate.bankAccount
Developer & Files
validate.hexColor | validate.rgb | validate.json | validate.xml | validate.latitudeLongitude | validate.coordinates | validate.uuid | validate.otp | validate.address
Higher-Order Validators (Require Config)
validate.regex(/^[A-Z]+$/, 'Must be uppercase')(value)
validate.custom((val) => val > 10, 'Must be greater than 10')(value)
// File Validators
validate.file({ maxSizeMB: 5, allowedTypes: ['image/png'] })(fileEvent)
validate.image(5)(fileEvent) // 5MB max
validate.pdfDocument(10)(fileEvent) // 10MB maxLocalization (i18n)
You can easily override the default error messages globally:
import { i18n } from '@abhivorn/validation-kit';
i18n.registerMessages('en', {
invalid_email: 'Please provide a valid corporate email.',
invalid_password: 'Your password is too weak.',
});