Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielaRiesgo/5aeeb4cbd42d2e116904c1ebdc854a9c to your computer and use it in GitHub Desktop.
Save danielaRiesgo/5aeeb4cbd42d2e116904c1ebdc854a9c to your computer and use it in GitHub Desktop.
enum PasswordError: Error {
case tooShort
case tooLong
case noCapitalLetter
}
let password = MutableProperty("")
let validated: ValidatingProperty<String, PasswordError> = ValidatingProperty(password, {
if $0.count < 5 {
return .invalid(.tooShort)
}
if $0.count > 10 {
return .invalid(.tooLong)
}
if $0.filter({ character in String(character).uppercased() == String(character) }).first != .none {
return .valid
}
return .coerced($0.capitalized, .noCapitalLetter)
})
validated.producer.startWithValues {
print("Producer Value -", $0)
print("Value - ", validated.value)
}
validated.result.producer.startWithValues {
print("Result -", $0)
}
password.value = "hi"
password.value = "HELLO"
password.value = "too loooooong"
password.value = "hello"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment