Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Pro subscription. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it’s simply the best investment you can make in your development career.

Sanitize & Validate Data Pro

Code pasted in during this episode:

struct WhitespaceValidation: Validation {
  struct Options: OptionSet {
    let rawValue: Int

    static let trailing = Options(rawValue: 1 << 0)
    static let leading = Options(rawValue: 1 << 1)

    static let all: Options = [.leading, .trailing]
  typealias Value = String
  let options: Options

  func validate(_ value: String) -> Bool {
    return !value.isEmpty

  func sanitize(_ value: String) -> String {
    var value = value
    if self.options.contains(.leading) {
      while value.first?.isWhitespace == true {
    if self.options.contains(.trailing) {
      while value.last?.isWhitespace == true {
    return value

  static let trailing = WhitespaceValidation(options: .leading)
  static let leading = WhitespaceValidation(options: .trailing)
  static let all = WhitespaceValidation(options: .all)

Learn how to create a Property Wrapper to sanitize and validate data before sending it to a backend service or saving it on device.