ChWebsiteApp/CHDataManagement/Model/Content+Validation.swift
2025-01-09 13:27:38 +01:00

47 lines
1.4 KiB
Swift

import Foundation
extension Content {
private static let disallowedCharactersInIds = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-")).inverted
private static let disallowedCharactersInFileIds = CharacterSet.alphanumerics.union(CharacterSet(charactersIn: "-.")).inverted
func isNewIdForTag(_ id: String) -> Bool {
tagOverview?.id != id && !tags.contains { $0.id == id }
}
func isNewIdForPage(_ id: String) -> Bool {
!pages.contains { $0.id == id }
}
func isNewIdForPost(_ id: String) -> Bool {
!posts.contains { $0.id == id }
}
func isNewIdForFile(_ id: String) -> Bool {
!files.contains { $0.id == id }
}
func isValidIdForTagOrPageOrPost(_ id: String) -> Bool {
!id.isEmpty &&
id.rangeOfCharacter(from: Content.disallowedCharactersInIds) == nil
}
func isValidIdForFile(_ id: String) -> Bool {
!id.isEmpty &&
id.rangeOfCharacter(from: Content.disallowedCharactersInFileIds) == nil
}
func containsPage(withUrlComponent urlComponent: String) -> Bool {
pages.contains {
$0.german.urlString == urlComponent ||
$0.english.urlString == urlComponent
}
}
func containsTag(withUrlComponent urlComponent: String) -> Bool {
(tagOverview?.contains(urlComponent: urlComponent) ?? false) ||
tags.contains { $0.contains(urlComponent: urlComponent) }
}
}