qstatus/qStatus/Utilities/InputValidation.swift
2026-03-03 19:37:18 +01:00

37 lines
1.4 KiB
Swift

import Foundation
enum InputValidator {
private static let mastodonCharacterLimit = 500
private static let maxImages = 4
private static let maxImageBytes = 15 * 1024 * 1024
static func validatePost(text: String, images: [ImageAttachment], accounts: [Account]) throws {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else {
throw PostingError(message: "Post cannot be empty.")
}
guard !accounts.isEmpty else {
throw PostingError(message: "Select at least one account.")
}
guard images.count <= maxImages else {
throw PostingError(message: "You can attach up to \(maxImages) images.")
}
for image in images {
guard !image.data.isEmpty else {
throw PostingError(message: "One of the selected images is empty or unreadable.")
}
guard image.data.count <= maxImageBytes else {
throw PostingError(message: "Image \(image.filename) is too large. Max size is 15 MB.")
}
}
if accounts.contains(where: { $0.serviceType == .mastodon }) && text.count > mastodonCharacterLimit {
throw PostingError(
message: "Mastodon posts are limited to \(mastodonCharacterLimit) characters."
)
}
}
}