Skip to content

Instantly share code, notes, and snippets.

@Nirajpaul2
Created December 1, 2024 09:41
Show Gist options
  • Select an option

  • Save Nirajpaul2/971174518b2c971d90d835823ab994c0 to your computer and use it in GitHub Desktop.

Select an option

Save Nirajpaul2/971174518b2c971d90d835823ab994c0 to your computer and use it in GitHub Desktop.
Validation
// Step 1: Define the Validation Strategy Protocol
// Each validation strategy will implement this protocol.
protocol ValidationStrategy {
func validate(_ input: String) -> Bool
}
// Step 2: Concrete Validation Strategies
// Email validation strategy
class EmailValidation: ValidationStrategy {
func validate(_ input: String) -> Bool {
let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex)
return emailTest.evaluate(with: input)
}
}
// Phone number validation strategy
class PhoneNumberValidation: ValidationStrategy {
func validate(_ input: String) -> Bool {
let phoneRegex = "^[0-9]{10}$" // Simple regex for a 10-digit phone number
let phoneTest = NSPredicate(format: "SELF MATCHES %@", phoneRegex)
return phoneTest.evaluate(with: input)
}
}
// Username validation strategy
class UsernameValidation: ValidationStrategy {
func validate(_ input: String) -> Bool {
// Username should be 3-15 characters long, alphanumeric
let usernameRegex = "^[a-zA-Z0-9]{3,15}$"
let usernameTest = NSPredicate(format: "SELF MATCHES %@", usernameRegex)
return usernameTest.evaluate(with: input)
}
}
// Step 3: Context Class
// This class holds the current validation strategy and delegates the validation.
class InputValidator {
private var validationStrategy: ValidationStrategy? // Current strategy
// Method to set the validation strategy dynamically
func setValidationStrategy(_ strategy: ValidationStrategy) {
self.validationStrategy = strategy
}
// Method to validate the input using the current strategy
func validateInput(_ input: String) -> Bool {
guard let strategy = validationStrategy else {
fatalError("Validation strategy is not set") // Ensure a strategy is set before validating
}
return strategy.validate(input)
}
}
// Step 4: Demonstration of Usage
// Main function to test the validation system
func main() {
// Create the context (InputValidator)
let validator = InputValidator()
// Test case 1: Email validation
validator.setValidationStrategy(EmailValidation()) // Set strategy to EmailValidation
let email = "[email protected]"
print("Is '\(email)' a valid email? \(validator.validateInput(email))") // Should print: true
// Test case 2: Phone number validation
validator.setValidationStrategy(PhoneNumberValidation()) // Set strategy to PhoneNumberValidation
let phoneNumber = "1234567890"
print("Is '\(phoneNumber)' a valid phone number? \(validator.validateInput(phoneNumber))") // Should print: true
// Test case 3: Username validation
validator.setValidationStrategy(UsernameValidation()) // Set strategy to UsernameValidation
let username = "user123"
print("Is '\(username)' a valid username? \(validator.validateInput(username))") // Should print: true
// Test case 4: Invalid email
let invalidEmail = "invalid-email"
validator.setValidationStrategy(EmailValidation()) // Reuse EmailValidation strategy
print("Is '\(invalidEmail)' a valid email? \(validator.validateInput(invalidEmail))") // Should print: false
}
// Execute the main function
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment