validation.ts•1.29 kB
/**
* Validates if a string is a valid Figma file key.
* File keys are typically alphanumeric strings.
*/
export const isValidFileKey = (fileKey: string): boolean => {
if (!fileKey || typeof fileKey !== "string") {
return false;
}
return /^[a-zA-Z0-9]{20,30}$/.test(fileKey);
};
/**
* Validates if a string is a valid Figma node ID.
* Node IDs can contain numbers, letters, colons, and hyphens.
*/
export const isValidNodeId = (nodeId: string): boolean => {
if (!nodeId || typeof nodeId !== "string") {
return false;
}
return /^[a-zA-Z0-9:;\-]+$/.test(nodeId);
};
/**
* Validation result type
*/
export interface ValidationResult {
isValid: boolean;
error?: string;
}
/**
* Validates environment variables.
*/
export const validateApiToken = (token: string): ValidationResult => {
if (!token) {
return { isValid: false, error: "API token is required" };
}
if (typeof token !== "string") {
return { isValid: false, error: "API token must be a string" };
}
if (token.length < 40 || token.length > 50) {
return { isValid: false, error: "Invalid API token format" };
}
if (!/^[a-zA-Z0-9\-_]+$/.test(token)) {
return { isValid: false, error: "API token contains invalid characters" };
}
return { isValid: true };
};