@th3hero/request-validator
Version:
🚀 Lightweight, fast & flexible request validation library for Node.js/Express/Next.js with TypeScript support. Features 20+ validation rules, database integration, file upload validation, and zero external dependencies. Perfect for API validation, form v
52 lines • 1.68 kB
JavaScript
;
/**
* Custom validation utilities to replace Sequelize's validator
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDate = exports.isURL = exports.isEmail = void 0;
/**
* Validates if a string is a valid email address
* @param value - The string to validate
* @returns boolean - True if valid email, false otherwise
*/
const isEmail = (value) => {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return emailRegex.test(value);
};
exports.isEmail = isEmail;
/**
* Validates if a string is a valid URL
* @param value - The string to validate
* @returns boolean - True if valid URL, false otherwise
*/
const isURL = (value) => {
try {
new URL(value);
return true;
}
catch {
return false;
}
};
exports.isURL = isURL;
/**
* Validates if a string is a valid date
* @param value - The string to validate
* @param format - Optional date format (default: YYYY-MM-DD)
* @returns boolean - True if valid date, false otherwise
*/
const isDate = (value, format = 'YYYY-MM-DD') => {
// Basic date validation for YYYY-MM-DD format
if (format === 'YYYY-MM-DD') {
const dateRegex = /^\d{4}-\d{2}-\d{2}$/;
if (!dateRegex.test(value))
return false;
const [year, month, day] = value.split('-').map(Number);
const date = new Date(year, month - 1, day);
return date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day;
}
// For other formats, we can add more validation logic
return !isNaN(Date.parse(value));
};
exports.isDate = isDate;
//# sourceMappingURL=validators.js.map