29 lines
702 B
JavaScript
29 lines
702 B
JavaScript
export const COMPANY_USER_ROLES = ['admin', 'user'];
|
|
|
|
/**
|
|
* Validates a company user role.
|
|
* @param {unknown} role
|
|
* @returns {'admin'|'user'}
|
|
* @throws {Error} 'Invalid role'
|
|
*/
|
|
export function assertRole(role) {
|
|
if (!COMPANY_USER_ROLES.includes(role)) {
|
|
throw new Error('Invalid role');
|
|
}
|
|
return role;
|
|
}
|
|
|
|
/**
|
|
* Normalizes and validates an email address.
|
|
* @param {unknown} email
|
|
* @returns {string} lower-cased, trimmed email
|
|
* @throws {Error} 'Invalid email'
|
|
*/
|
|
export function normalizeEmail(email) {
|
|
const value = typeof email === 'string' ? email.trim().toLowerCase() : '';
|
|
if (!value || !value.includes('@')) {
|
|
throw new Error('Invalid email');
|
|
}
|
|
return value;
|
|
}
|