|
| 1 | +// |
| 2 | +// Validator |
| 3 | +// Copyright © 2025 Space Code. All rights reserved. |
| 4 | +// |
| 5 | + |
| 6 | +import ValidatorCore |
| 7 | + |
| 8 | +// 1. LengthValidationRule - validates string length |
| 9 | +let lengthRule = LengthValidationRule(min: 3, max: 20, error: "Length must be 3-20 characters") |
| 10 | + |
| 11 | +let shortString = "ab" |
| 12 | +let validString = "Hello" |
| 13 | +let longString = "This is a very long string that exceeds the limit" |
| 14 | + |
| 15 | +print("LengthValidationRule:") |
| 16 | +print("'\(shortString)' is valid: \(lengthRule.validate(input: shortString))") // false |
| 17 | +print("'\(validString)' is valid: \(lengthRule.validate(input: validString))") // true |
| 18 | +print("'\(longString)' is valid: \(lengthRule.validate(input: longString))") // false |
| 19 | +print() |
| 20 | + |
| 21 | +// 2. NonEmptyValidationRule - checks if string is not empty |
| 22 | +let nonEmptyRule = NonEmptyValidationRule(error: "Field is required") |
| 23 | + |
| 24 | +let emptyString = "" |
| 25 | +let whitespaceString = " " |
| 26 | +let filledString = "Content" |
| 27 | + |
| 28 | +print("NonEmptyValidationRule:") |
| 29 | +print("'\(emptyString)' is valid: \(nonEmptyRule.validate(input: emptyString))") // false |
| 30 | +print("'\(whitespaceString)' is valid: \(nonEmptyRule.validate(input: whitespaceString))") // false |
| 31 | +print("'\(filledString)' is valid: \(nonEmptyRule.validate(input: filledString))") // true |
| 32 | +print() |
| 33 | + |
| 34 | +// 3. PrefixValidationRule - validates string prefix |
| 35 | +let prefixRule = PrefixValidationRule(prefix: "https://", error: "URL must start with https://") |
| 36 | + |
| 37 | +let httpURL = "http://example.com" |
| 38 | +let httpsURL = "https://example.com" |
| 39 | +let noProtocol = "example.com" |
| 40 | + |
| 41 | +print("PrefixValidationRule:") |
| 42 | +print("'\(httpURL)' is valid: \(prefixRule.validate(input: httpURL))") // false |
| 43 | +print("'\(httpsURL)' is valid: \(prefixRule.validate(input: httpsURL))") // true |
| 44 | +print("'\(noProtocol)' is valid: \(prefixRule.validate(input: noProtocol))") // false |
| 45 | +print() |
| 46 | + |
| 47 | +// 4. SuffixValidationRule - validates string suffix |
| 48 | +let suffixRule = SuffixValidationRule(suffix: ".com", error: "Domain must end with .com") |
| 49 | + |
| 50 | +let comDomain = "example.com" |
| 51 | +let orgDomain = "example.org" |
| 52 | +let noDomain = "example" |
| 53 | + |
| 54 | +print("SuffixValidationRule:") |
| 55 | +print("'\(comDomain)' is valid: \(suffixRule.validate(input: comDomain))") // true |
| 56 | +print("'\(orgDomain)' is valid: \(suffixRule.validate(input: orgDomain))") // false |
| 57 | +print("'\(noDomain)' is valid: \(suffixRule.validate(input: noDomain))") // false |
| 58 | +print() |
| 59 | + |
| 60 | +// 5. RegexValidationRule - validates using regular expression |
| 61 | +let phoneRule = RegexValidationRule(pattern: "^\\d{3}-\\d{4}$", error: "Invalid phone format") |
| 62 | + |
| 63 | +let validPhone = "123-4567" |
| 64 | +let invalidPhone1 = "1234567" |
| 65 | +let invalidPhone2 = "123-456" |
| 66 | + |
| 67 | +print("RegexValidationRule:") |
| 68 | +print("'\(validPhone)' is valid: \(phoneRule.validate(input: validPhone))") // true |
| 69 | +print("'\(invalidPhone1)' is valid: \(phoneRule.validate(input: invalidPhone1))") // false |
| 70 | +print("'\(invalidPhone2)' is valid: \(phoneRule.validate(input: invalidPhone2))") // false |
| 71 | +print() |
| 72 | + |
| 73 | +// 6. URLValidationRule - validates URL format |
| 74 | +let urlRule = URLValidationRule(error: "Please enter a valid URL") |
| 75 | + |
| 76 | +let validURL = "https://www.apple.com" |
| 77 | +let invalidURL = "not a url" |
| 78 | +let localURL = "file:///path/to/file" |
| 79 | + |
| 80 | +print("URLValidationRule:") |
| 81 | +print("'\(validURL)' is valid: \(urlRule.validate(input: validURL))") // true |
| 82 | +print("'\(invalidURL)' is valid: \(urlRule.validate(input: invalidURL))") // false |
| 83 | +print("'\(localURL)' is valid: \(urlRule.validate(input: localURL))") // true |
| 84 | +print() |
| 85 | + |
| 86 | +// 7. CreditCardValidationRule - validates credit card number (Luhn algorithm) |
| 87 | +let cardRule = CreditCardValidationRule(error: "Invalid card number") |
| 88 | + |
| 89 | +let validCard = "4532015112830366" // Valid Visa test number |
| 90 | +let invalidCard = "1234567890123456" |
| 91 | +let shortCard = "4532" |
| 92 | + |
| 93 | +print("CreditCardValidationRule:") |
| 94 | +print("'\(validCard)' is valid: \(cardRule.validate(input: validCard))") // true |
| 95 | +print("'\(invalidCard)' is valid: \(cardRule.validate(input: invalidCard))") // false |
| 96 | +print("'\(shortCard)' is valid: \(cardRule.validate(input: shortCard))") // false |
| 97 | +print() |
| 98 | + |
| 99 | +// 8. EmailValidationRule - validates email format |
| 100 | +let emailRule = EmailValidationRule(error: "Please enter a valid email") |
| 101 | + |
| 102 | +let validEmail = "user@example.com" |
| 103 | +let invalidEmail1 = "user@" |
| 104 | +let invalidEmail2 = "user.example.com" |
| 105 | + |
| 106 | +print("EmailValidationRule:") |
| 107 | +print("'\(validEmail)' is valid: \(emailRule.validate(input: validEmail))") // true |
| 108 | +print("'\(invalidEmail1)' is valid: \(emailRule.validate(input: invalidEmail1))") // false |
| 109 | +print("'\(invalidEmail2)' is valid: \(emailRule.validate(input: invalidEmail2))") // false |
| 110 | +print() |
| 111 | + |
| 112 | +// 9. CharactersValidationRule - validates allowed characters |
| 113 | +let lettersRule = CharactersValidationRule(characterSet: .letters, error: "Invalid characters") |
| 114 | + |
| 115 | +let onlyLetters = "HelloWorld" |
| 116 | +let withNumbers = "Hello123" |
| 117 | +let withSpaces = "Hello World" |
| 118 | + |
| 119 | +print("CharactersValidationRule:") |
| 120 | +print("'\(onlyLetters)' is valid: \(lettersRule.validate(input: onlyLetters))") // true |
| 121 | +print("'\(withNumbers)' is valid: \(lettersRule.validate(input: withNumbers))") // false |
| 122 | +print("'\(withSpaces)' is valid: \(lettersRule.validate(input: withSpaces))") // false |
| 123 | +print() |
| 124 | + |
| 125 | +// 10. NilValidationRule - validates that value is nil |
| 126 | +let nilRule = NilValidationRule<String>(error: "Value must be nil") |
| 127 | + |
| 128 | +let nilValue: String? = nil |
| 129 | +let nonNilValue: String? = "Something" |
| 130 | + |
| 131 | +print("NilValidationRule:") |
| 132 | +print("nil value is valid: \(nilRule.validate(input: nilValue))") // true |
| 133 | +print("non-nil value is valid: \(nilRule.validate(input: nonNilValue))") // false |
| 134 | +print() |
| 135 | + |
| 136 | +// MARK: - Combining Rules |
| 137 | + |
| 138 | +print("=== Combining Multiple Rules ===") |
| 139 | + |
| 140 | +// Example: password validation with multiple rules |
| 141 | +let passwordLengthRule = LengthValidationRule(min: 8, max: 32, error: "Password must be 8-32 characters") |
| 142 | +let passwordNotEmptyRule = NonEmptyValidationRule(error: "Password is required") |
| 143 | + |
| 144 | +let passwords = ["", "123", "ValidPass123", "VeryLongPasswordThatExceedsMaximumLength"] |
| 145 | + |
| 146 | +for password in passwords { |
| 147 | + let isLengthValid = passwordLengthRule.validate(input: password) |
| 148 | + let isNotEmpty = passwordNotEmptyRule.validate(input: password) |
| 149 | + let isValid = isLengthValid && isNotEmpty |
| 150 | + |
| 151 | + print("Password '\(password)': \(isValid ? "✅" : "❌")") |
| 152 | +} |
0 commit comments