@satyajit_me/input-validator
v1.0.1
Published
Validate emails, URLs, phone numbers, credit cards with chainable rules
Downloads
161
Maintainers
Readme
Input Validator
Validate emails, URLs, phone numbers, credit cards with chainable rules.
Installation
npm install input-validatorUsage
import { isEmail, isUrl, validate } from 'input-validator';Quick Validators
isEmail('[email protected]') // true
isUrl('https://example.com') // true
isPhone('+1 555 123-4567') // true
isCreditCard('4111111111111111') // true
isUuid('550e8400-...') // true
isIp('192.168.1.1') // true
isHexColor('#ff5733') // true
isDateString('2024-01-15') // true
isSlug('hello-world') // true
isJson('{"key": "value"}') // true
isEmpty('') // true
isAlphanumeric('abc123') // true
isAlpha('abc') // true
isNumeric('123') // true
isInteger(42) // true
isInRange(5, { min: 1, max: 10 }) // true
isLength('hi', { min: 1, max: 5 }) // true
isOneOf('a', ['a', 'b', 'c']) // true
matches('abc', /^[a-z]+$/) // trueChainable Validation
const result = validate(userInput)
.required('Email is required')
.email('Invalid email format')
.minLength(5, 'Too short');
if (result.isValid()) {
// proceed
} else {
console.log(result.getErrors()); // All error messages
console.log(result.getFirstError()); // First error only
}Chain Methods
| Method | Description |
|--------|-------------|
| .required(msg) | Field must not be empty |
| .email(msg) | Must be valid email |
| .url(msg) | Must be valid URL |
| .phone(msg) | Must be valid phone |
| .minLength(n, msg) | Minimum length |
| .maxLength(n, msg) | Maximum length |
| .pattern(regex, msg) | Match regex |
| .custom(fn, msg) | Custom validation function |
Options
// URL with protocol required
isUrl('example.com', { requireProtocol: true }) // false
// Length range
isLength('hello', { min: 3, max: 10 }) // true
// Number range
isInRange(5, { min: 1, max: 10 }) // trueLicense
MIT
