For fun, I added additional code to check for valid address information in the Order class using regular expressions (see Paul's article at: https://www.hackingwithswift.com/articles/108/how-to-use-regular-expressions-in-swift).
These are both US-centric, one for checking for a 5 digit Zip Code (not Zip+4!):
//confirm zip code is 5 digits long
let zipRange = NSRange(location: 0, length: zip.utf16.count)
let zipRegex = try! NSRegularExpression(pattern: "[0-9]{5}")
if zipRegex.firstMatch(in: zip, options: [], range: zipRange) == nil {
return false
}
...the second is to perform a simple-minded address check: one or more digits, followed by one or more spaces, followed by at least one number or letter:
//confirm address starts with numbers followed by something that looks like a street name
let streetAddressRange = NSRange(location: 0, length: streetAddress.utf16.count)
let streetAddressRegex = try! NSRegularExpression(pattern: "[0-9]+[ ]+[a-zA-Z0-9]+")
if streetAddressRegex.firstMatch(in: streetAddress, options: [], range: streetAddressRange) == nil {
return false
}