Initial commit: add .gitignore and README
This commit is contained in:
49
packages/shared-utils/src/validation.ts
Normal file
49
packages/shared-utils/src/validation.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Validation utility functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if value is empty
|
||||
*/
|
||||
export function isEmpty(value: unknown): boolean {
|
||||
if (value === null || value === undefined) return true;
|
||||
if (typeof value === 'string') return value.trim().length === 0;
|
||||
if (Array.isArray(value)) return value.length === 0;
|
||||
if (typeof value === 'object') return Object.keys(value).length === 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if value is not empty
|
||||
*/
|
||||
export function isNotEmpty(value: unknown): boolean {
|
||||
return !isEmpty(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if email is valid
|
||||
*/
|
||||
export function isValidEmail(email: string): boolean {
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
return emailRegex.test(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if URL is valid
|
||||
*/
|
||||
export function isValidUrl(url: string): boolean {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if string is numeric
|
||||
*/
|
||||
export function isNumeric(str: string): boolean {
|
||||
return !isNaN(Number(str)) && !isNaN(parseFloat(str));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user