-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathURLValidationRule.swift
More file actions
42 lines (32 loc) · 1.02 KB
/
URLValidationRule.swift
File metadata and controls
42 lines (32 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// Validator
// Copyright © 2023 Space Code. All rights reserved.
//
import Foundation
// Validates that a string represents a valid URL.
//
// # Example:
// ```swift
// let rule = URLValidationRule(error: "Invalid URL")
// rule.validate(input: "https://example.com") // true
// rule.validate(input: "not_a_url") // false
// ```
public struct URLValidationRule: IValidationRule {
// MARK: Types
public typealias Input = String
// MARK: Properties
/// The validation error returned if the input is not a valid URL.
public let error: IValidationError
// MARK: Initialization
/// Initializes a URL validation rule.
///
/// - Parameter error: The validation error returned if input fails validation.
public init(error: IValidationError) {
self.error = error
}
// MARK: IValidationRule
public func validate(input: String) -> Bool {
guard let url = URL(string: input) else { return false }
return url.isFileURL || (url.host != nil && url.scheme != nil)
}
}