From f63f503b718e453602d3f896d528ad3e1b5043b9 Mon Sep 17 00:00:00 2001 From: WofWca Date: Tue, 19 Mar 2024 15:40:53 +0400 Subject: [PATCH] add more validation for `server` currently many providers are failing --- validation/index.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/validation/index.js b/validation/index.js index d0c7645..e9ef9a7 100644 --- a/validation/index.js +++ b/validation/index.js @@ -9,10 +9,34 @@ function testServer(server){ if(!server.hostname || server.hostname.trim() == "") throw new Error("Hostname missing") if(!server.port) throw new Error("Port missing") - if(!server.socket || server.socket.trim() == "") throw new Error("Socket missing") - if(!server.type || server.type.trim() == "") throw new Error("Type missing") - - // todo username_pattern optional but one of right the values + if( + !server.socket || + server.socket.trim() == "" || + !["SSL", "STARTTLS", "PLAIN"].includes(server.socket.trim()) + ) throw new Error(`Invalid Socket "${server.socket}"`) + if( + !server.type || + server.type.trim() == "" || + !["IMAP", "SMTP"].includes(server.type.trim().toUpperCase()) + ) throw new Error(`Invalid type "${server.type}"`) + if( + server.username_pattern && + !["EMAIL", "EMAILLOCALPART"].includes( + server.username_pattern.trim().toUpperCase() + ) + ) throw new Error(`Invalid username_pattern "${server.username_pattern}"`) + + for (const key of Object.keys(server)) { + if (![ + "type", + "socket", + "hostname", + "port", + "username_pattern", + ].includes(key)) { + throw new Error(`Unexpected key "${key}"`) + } + } } function test(fileContent) {