// input validation for server URLs and credentials // ensure value is always an array const toArray = (value) => Array.isArray(value) ? value : value ? [value] : []; // build validation response object const buildValidation = (valid, value, error) => ({ valid, ...(valid ? { value } : { error }), }); // check if id is present and valid type function validateId(value, fieldName = "ID") { const isValid = value && (typeof value === "string" || typeof value === "number"); return buildValidation(isValid, value, `${fieldName} is required`); } // validate server URL is valid HTTP(S) format function validateServerUrl(url) { if (!url || typeof url !== "string") { return buildValidation(false, null, STRINGS.SERVER_URL_REQUIRED); } const trimmed = url.trim(); if (!trimmed) { return buildValidation(false, null, STRINGS.SERVER_URL_EMPTY); } try { const urlObj = new URL(trimmed); const isValid = ["http:", "https:"].includes(urlObj.protocol); return buildValidation( isValid, trimmed, isValid ? null : "Server URL must use HTTP or HTTPS", ); } catch { return buildValidation(false, null, "Invalid URL format"); } } // validate username and password meet requirements function validateCredentials(username, password) { const errors = []; if (!username || typeof username !== "string" || !username.trim()) { errors.push("Username is required"); } else if (username.length > 255) { errors.push("Username is too long (max 255 characters)"); } if (!password || typeof password !== "string" || !password) { errors.push("Password is required"); } else if (password.length > 1000) { errors.push("Password is too long"); } if (errors.length > 0) { return buildValidation(false, null, errors.join("; ")); } return buildValidation(true, { username: username.trim(), password }); }