Skip to content

Instantly share code, notes, and snippets.

@farzadshbfn
Last active September 14, 2018 14:47
Show Gist options
  • Save farzadshbfn/9fe89e79a12f657ed88725f3b61c935b to your computer and use it in GitHub Desktop.
Save farzadshbfn/9fe89e79a12f657ed88725f3b61c935b to your computer and use it in GitHub Desktop.
struct CellNumber: Codable {
let digits: String
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawDigits = try container.decode(String.self)
// validate rawDigits by some Regex maybe? and throw appropriate errors?
// or extract CountryCode and AreaCode and save them in another way?
digits = rawDigits
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(digits)
}
}
struct Email: Codable {
let address: String
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawAddress = try container.decode(String.self)
// validate rawAddress by some Regex maybe? and throw appropriate errors?
address = rawAddress
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(address)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment