Skip to content

Commit

Permalink
Fixed initial validation; updated readme; added convenience operators
Browse files Browse the repository at this point in the history
  • Loading branch information
sanzaru committed Oct 16, 2024
1 parent 63377fe commit a1328c0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ Button { } label: {
| .minLength(Int) | If set, the value must be longer than the provided length. | ```.minLength(10)``` |
| .maxLength(Int) | If set, the value must be shorter than the provided length. | ```.maxLength(10)``` |
| .pattern(String) | If set, the value must match the provided regular expression. | ```.pattern("[a-zA-Z]")``` |
| .pattern(RegEx) | If set, the value must match the provided regular expression. | ```.pattern("[a-zA-Z]")``` |
| .email | If set, the value must match an internal regular expression for email addresses. | ```.pattern("[a-zA-Z]/)``` |
| .email | If set, the value must match an internal regular expression for email addresses. | ```.email``` |
| .phone | If set, the value must match an internal regular expression for phone numbers. | ```.phone``` |


## Example
Expand Down
6 changes: 5 additions & 1 deletion Sources/Formify/FormifyField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct FormifyField {
public private(set) var pattern: Regex<Substring>?

public var isValid: Bool {
errors.isEmpty
isRequired && value.isEmpty ? false : errors.isEmpty
}

public init(_ initialValue: String = "", operators: [FormifyOperator] = []) {
Expand All @@ -45,6 +45,10 @@ public struct FormifyField {
maxLength = length
case .pattern(let regex):
pattern = try? Regex(regex)
case .email:
pattern = try? Regex("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}")
case .phonenumber:
pattern = try? Regex("\\+?([0-9]{1,3})?[-.\\s]?(\\(?[0-9]{1,4}\\)?)?[-.\\s]?[0-9]{1,4}[-.\\s]?[0-9]{1,4}[-.\\s]?[0-9]{0,9}")
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/Formify/FormifyOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public enum FormifyOperator {
case pattern(String)
case minLength(Int)
case maxLength(Int)
case email
case phonenumber
}

0 comments on commit a1328c0

Please sign in to comment.