Publisher

@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher
  • A Combine operator that validates upstream output and sends valid non-nil objects downstream.

         _ = Just("Foo-bar")
             .validate(!.empty && .count(3...))
             .sink { print($0) }
         // "Foo-bar"
    

    Declaration

    Swift

    public func compactValidate<T>(_ validator: Validator<T>) -> CompactValidatePublisher<Self> where T == Self.Output

    Parameters

    validator

    The validator to use to validate the upstream output.

    Return Value

    A new CompactValidatePublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output and sends valid non-nil objects downstream.

         _ = Just("Foo-bar")
             .validate { !.empty && .count(3...) }
             .sink { print($0) }
         // "Foo-bar"
    

    Declaration

    Swift

    public func compactValidate<T>(_ validator: @escaping () -> Validator<T>) -> CompactValidatePublisher<Self> where T == Output

    Parameters

    validator

    A closure that returns validator to use to validate the upstream output.

    Return Value

    A new CompactValidatePublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output that is Validatable and sends valid non-nil objects downstream.

    See also

    Validatable
         _ = Just(MyValidatable("foo-bar"))
             .validate()
             .sink { print($0.name) }
         // "foo-bar"
    

    Declaration

    Swift

    public func compactValidate<T>() -> CompactValidatePublisher<Self> where T : Validatable, T == Self.Output

    Return Value

    A new CompactValidatePublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output and sends valid non-nil objects downstream. This allows you to create a custom Validator on the fly.

    See also

    Validator

         _ = Just("foo-bar")
             .compactValidate(name: "only foo-bar") { string in
                 guard string.lowercased() == "foo-bar" else {
                     return nil
                 }
                 return string
             }
             .sink { print($0) }
    
         // "foo-bar"
    

    Declaration

    Swift

    public func compactValidate<T>(name: String, _ closure: @escaping (T) -> T?) -> CompactValidatePublisher<Self> where T == Output

    Parameters

    name

    The name for the custom validator.

    closure

    Closure that takes an instance and returns the instance if valid or nil if not valid.

  • A Combine operator that validates upstream output and sends valid objects or a BasicValidationError downstream.

         _ = Just("")
             .tryValidate(!.empty && .count(3...))
             .replaceError(with: "Failed")
             .sink { print($0) }
         // "Failed"
    

    Declaration

    Swift

    public func tryValidate<T>(_ validator: Validator<T>) -> TryValidationPublisher<Self> where T == Self.Output

    Parameters

    validator

    The validator to use to validate the upstream output.

    Return Value

    A new TryValidationPublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output and sends valid objects or a BasicValidationError downstream.

         _ = Just("")
             .tryValidate { !.empty && .count(3...) }
             .replaceError(with: "Failed")
             .sink { print($0) }
         // "Failed"
    

    Declaration

    Swift

    public func tryValidate<T>(_ validator: @escaping () -> Validator<T>) -> TryValidationPublisher<Self> where T == Output

    Parameters

    validator

    A closure that returns the validator to use to validate the upstream output.

    Return Value

    A new TryValidationPublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output that is Validatable and sends valid objects or a BasicValidationError downstream.

         _ = Just(MyValidatable())
             .tryValidate()
             .replaceError(with: MyValidatable("Failed"))
             .sink { print($0.name) }
         // "Failed"
    

    Declaration

    Swift

    public func tryValidate<T>() -> TryValidationPublisher<Self> where T : Validatable, T == Self.Output

    Return Value

    A new TryValidationPublisher that sends valid non-nil objects donwstream.

  • A Combine operator that creates a custom Validator to validate upstream output and sends valid objects or an Error downstream.

         _ = Just("")
             .tryValidate(name: "only foo-bar") { string in
                 guard string.lowercased() == "foo-bar" else {
                     throw MyError()
                  }
             }
             .replaceError(with: "Failed")
             .sink { print($0) }
         // "Failed"
    

    Declaration

    Swift

    public func tryValidate<T>(name: String, _ closure: @escaping (T) throws -> ()) -> TryValidationPublisher<Self> where T == Output

    Parameters

    name

    The name for the custom validator

    closure

    A closure used to validate the output from the upstream publisher. This closure should throw an error on invalid objects or return Void for valid objects.

    Return Value

    A new TryValidationPublisher that sends valid non-nil objects donwstream.

  • A Combine operator that validates upstream output and sends valid or nil objects downstream.

         _ = Just("")
             .validate(!.empty && .count(3...))
             .replaceNil(with: "Failed")
             .sink { print($0) }
         // "Failed"
    

    Declaration

    Swift

    public func validate<T>(_ validator: Validator<T>) -> ValidationPublisher<Self> where T == Self.Output

    Parameters

    validator

    The validator to use to validate the upstream output.

    Return Value

    A new ValidationPublisher that sends valid objects or nil donwstream.

  • A Combine operator that validates upstream output and sends valid or nil objects downstream.

         _ = Just("")
             .validate { !.empty && .count(3...) }
             .replaceNil(with: "Failed")
             .sink { print($0) }
         // "Failed"
    

    Declaration

    Swift

    public func validate<T>(_ validator: @escaping () -> Validator<T>) -> ValidationPublisher<Self> where T == Output

    Parameters

    validator

    A closure that returns a validator to use to validate the upstream output.

    Return Value

    A new ValidationPublisher that sends valid objects or nil donwstream.

  • A Combine operator that validates upstream output that is Validatable and sends valid or nil objects downstream.

    See also

    Validatable

         _ = Just(MyValidatable())
             .validate()
             .replaceNil(with: MyValidatable("Failed"))
             .sink { print($0.name) }
         // "Failed"
    

    Declaration

    Swift

    public func validate<T>() -> ValidationPublisher<Self> where T : Validatable, T == Self.Output

    Return Value

    A new ValidationPublisher that sends valid objects or nil donwstream.

  • A Combine operator that creates a custom validator to validate upstream output and sends valid or nil objects downstream.

    Seealso

    Validator

     _ = Just("")
         .validate(name: "only foo-bar") { string in
             guard string.lowercased() == "foo-bar" else {
                 return nil
             }
             return string
         }
         .replaceNil(with: "Failed")
         .sink { print($0.name) }
     // "Failed"
    

    Declaration

    Swift

    public func validate<T>(name: String, _ closure: @escaping (T) -> T?) -> ValidationPublisher<Self> where T == Output

    Parameters

    name

    The name for the custom validator.

    closure

    Closure that takes an instance and returns the instance if valid or nil if not valid.