gadget/gadget-code/src/lib/validators.ts
2026-04-28 09:20:37 -04:00

43 lines
1.2 KiB
TypeScript

// src/lib/validators.ts
// Copyright (C) 2026 Robert Colbert <rob.colbert@openplatform.us>
// All Rights Reserved
/**
* Validates if a given string is a valid Firebase user ID.
*
* A valid Firebase user ID must meet the following criteria:
* 1. Must be between 6 and 50 characters long (inclusive)
* 2. Can only contain alphanumeric characters, underscore (_), dot (.),
* hyphen (-), plus (+), and slash (/) characters
* 3. Cannot start or end with a special character
*
* @param {string} userId - The string to validate as a Firebase user ID
* @returns {boolean} True if the string is a valid Firebase user ID, false
* otherwise
*/
export function isValidFirebaseUserId(userId: string): boolean {
// Check length requirements
const minLength = 6;
const maxLength = 50;
if (
typeof userId !== "string" ||
userId.length < minLength ||
userId.length > maxLength
) {
return false;
}
// Define allowed special characters
const specialChars = ["_", ".", "-", "+", "/"];
// Regular expression to match the Firebase user ID pattern
const regex = new RegExp(
`^[A-Za-z0-9]+(${specialChars
.map((c) => `\\${c}`)
.join("|")}[A-Za-z0-9]*)*$`
);
return regex.test(userId);
}