Files

39 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import Foundation
/// Eingabe-Prüfung + Bereinigung für Profile (reine Funktionen, testbar).
/// Fängt die häufigsten „Verbindung falsch"-Ursachen ab: Leerzeichen,
/// user@host im Host-Feld, Protokoll-Präfix, leerer Benutzername.
enum ProfileValidation {
static func cleaned(_ profile: ConnectionProfile) -> ConnectionProfile {
var p = profile
p.name = p.name.trimmingCharacters(in: .whitespacesAndNewlines)
p.host = p.host.trimmingCharacters(in: .whitespacesAndNewlines)
p.username = p.username.trimmingCharacters(in: .whitespacesAndNewlines)
p.group = p.group.trimmingCharacters(in: .whitespacesAndNewlines)
p.resolution = p.resolution.trimmingCharacters(in: .whitespacesAndNewlines)
p.extraArgs = p.extraArgs.trimmingCharacters(in: .whitespacesAndNewlines)
return p
}
static func warnings(for profile: ConnectionProfile) -> [String] {
var w: [String] = []
let h = profile.host.trimmingCharacters(in: .whitespacesAndNewlines)
if h.contains("@") {
w.append("Host enthält „@“ — den Benutzernamen ins Feld darunter eintragen, hier nur Hostname/IP.")
}
if h.contains("://") {
w.append("Host enthält „://“ — nur Hostname/IP, kein Protokoll (das steht oben).")
}
if h.contains(where: { $0.isWhitespace }) {
w.append("Host enthält Leerzeichen.")
}
if profile.username.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
w.append("Benutzername fehlt.")
}
if !(1...65535).contains(profile.port) {
w.append("Port ungültig (1–65535).")
}
return w
}
}