-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLengthValidationRule.swift
More file actions
55 lines (44 loc) · 1.62 KB
/
LengthValidationRule.swift
File metadata and controls
55 lines (44 loc) · 1.62 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
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Validator
// Copyright © 2023 Space Code. All rights reserved.
//
import Foundation
/// Validates that a string length is within the specified range.
///
/// This rule checks whether the input string's length is greater than or equal to `min`
/// and less than or equal to `max`. If the string length falls outside this range, validation fails.
///
/// # Example:
/// ```swift
/// let rule = LengthValidationRule(min: 3, max: 10, error: "Length out of range")
/// rule.validate(input: "Hello") // true
/// rule.validate(input: "Hi") // false
/// ```
public struct LengthValidationRule: IValidationRule {
// MARK: Types
public typealias Input = String
// MARK: Properties
/// The minimum allowed length of the string. Defaults to `0`.
public let min: Int
/// The maximum allowed length of the string. Defaults to `Int.max`.
public let max: Int
/// The validation error returned if the input does not satisfy the length constraints.
public let error: IValidationError
// MARK: Initialization
/// Initializes a length validation rule.
///
/// - Parameters:
/// - min: The minimum allowed string length. Defaults to 0.
/// - max: The maximum allowed string length. Defaults to `Int.max`.
/// - error: The validation error returned if input is invalid.
public init(min: Int = .zero, max: Int = .max, error: IValidationError) {
self.min = min
self.max = max
self.error = error
}
// MARK: IValidationRule
public func validate(input: String) -> Bool {
let length = input.count
return length >= min && length <= max
}
}