Skip to content

Instantly share code, notes, and snippets.

View RPallas92's full-sized avatar

Ricardo Pallas RPallas92

View GitHub Profile
//The fmap function is only applied on Success values.
let success: Validation<String, Int> = Validation.Success(1)
success.fmap{ $0 + 1 }
// ==> Validation.Success(2)
let failure: Validation<String, Int> = Validation.Failure("error")
failure.fmap{$0 + 1}
// ==> Validation.Failure("error")
//Validate min length
func minLength(_ value:String, min:Int, fieldName:String) -> Validation<[String], String>{
if(value.characters.count < min){
return Validation.Failure(["\(fieldName) must have more than \(min) characters"])
} else {
return Validation.Success(value)
}
}
//Validate match a regular expression
//Validate min length
func minLength(_ value:String, min:Int, fieldName:String) -> Validation<[String], String>{
if(value.characters.count < min){
return Validation.Failure(["\(fieldName) must have more than \(min) characters"])
} else {
return Validation.Success(value)
}
}
//Validate match a regular expression
func validatePassword(username: String, password:String) -> [String]{
var errors:[String] = []
if password.characters.count < 8 {
errors.append("Password must have more than 8 characters.")
}
if (password.range(of:"[\\W]", options: .regularExpression) == nil){
errors.append("Password must contain a special character.")
//Check if the password is long enough
func isPasswordLongEnough(_ password:String) -> Validation<[String], String> {
if password.characters.count < 8 {
return Validation.Failure(["Password must have more than 8 characters."])
} else {
return Validation.Success(password)
}
}

Business Models

Advertising

Models Examples
Display ads Yahoo!
Search ads Google