Validated

public class Validated<E, A> : ValidatedOf<E, A>

Validated is a data type to represent valid and invalid values. It is similar to Either, but with error accumulation in the invalid case.

  • Constructs a valid value.

    Declaration

    Swift

    public static func valid(_ value: A) -> Validated<E, A>

    Parameters

    value

    Valid value to be wrapped in this validated.

    Return Value

    A Validated value wrapping the parameter.

  • Constructs an invalid value.

    Declaration

    Swift

    public static func invalid(_ value: E) -> Validated<E, A>

    Parameters

    value

    Invalid value to be wrapped in this validated.

    Return Value

    A Validated value wrapping the parameter.

  • Constructs a Validated from a Try value.

    Declaration

    Swift

    public static func fromTry(_ t: Try<A>) -> Validated<Error, A>

    Parameters

    t

    A Try value.

    Return Value

    A Validated that contains an invalid error or a valid value, obtained from the Try value.

  • Constructs a Validated from an Option value.

    Declaration

    Swift

    public static func fromOption(_ m: Option<A>, ifNone: @escaping () -> E) -> Validated<E, A>

    Parameters

    m

    An Option value.

    ifNone

    A closure providing a value for the invalid case if the option is not present.

    Return Value

    A Validated containing a valid value from the option, or an invalid wrapping the default value from the closure.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: ValidatedOf<E, A>) -> Validated<E, A>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to Validated.

  • Applies the provided closures based on the content of this Validated value.

    Declaration

    Swift

    public func fold<C>(_ fe: (E) -> C, _ fa: (A) -> C) -> C

    Parameters

    fe

    Closure to apply if the contained value is invalid.

    fa

    Closure to apply if the contained value is valid.

    Return Value

    Result of applying the corresponding closure to the contained value.

  • Checks if this value is valid.

    Declaration

    Swift

    public var isValid: Bool { get }
  • Checks if this value is invalid.

    Declaration

    Swift

    public var isInvalid: Bool { get }
  • Checks if the valid value in this Validated matches a predicate.

    Declaration

    Swift

    public func exists(_ predicate: (A) -> Bool) -> Bool

    Parameters

    predicate

    Predicate to match the valid values.

    Return Value

    false if the contained value is invalid or does not match the predicate; true otherwise.

  • Converts this value to an Either value.

    Declaration

    Swift

    public func toEither() -> Either<E, A>

    Return Value

    An Either.left if this value is invalid, or an Either.right if this value is valid.

  • Converts this value to an Option value.

    Declaration

    Swift

    public func toOption() -> Option<A>

    Return Value

    An Option.none if this value is invalid, or an Option.some if this value is valid.

  • Converts this value to an Array value.

    Declaration

    Swift

    public func toArray() -> [A]

    Return Value

    An empty array if this value is invalid, or a singleton array if this value is valid.

  • Wraps the invalid values of this type into a NonEmptyArray.

    Declaration

    Swift

    public func toValidatedNEA() -> Validated<NEA<E>, A>

    Return Value

    A value that is equivalent to the original one but wraps the invalid value in a NonEmptyArray.

  • Applies a function in the Either context to this value.

    It uses the isomorphism between Either and Validated, mapping left to invalid and right to valid.

    Declaration

    Swift

    public func withEither<EE, B>(_ f: (Either<E, A>) -> Either<EE, B>) -> Validated<EE, B> where EE : Semigroup

    Parameters

    f

    A closure in the Either context.

    Return Value

    Transformation of this validated value with the provided closure.

  • Swaps the valid and invalid types.

    Declaration

    Swift

    public func swap() -> Validated<A, E>

    Return Value

    A valid value if it was invalid, and vice versa.

  • Obtains the valid value or a default value for the invalid case.

    Declaration

    Swift

    public func getOrElse(_ defaultValue: A) -> A

    Parameters

    defaultValue

    Default value for the invalid case.

    Return Value

    Valid value or default value otherwise.

  • Obtains the valid value or maps the invalid value.

    Declaration

    Swift

    public func valueOr(_ f: (E) -> A) -> A

    Parameters

    f

    Mapping function for invalid values.

    Return Value

    The valid value or the mapped invalid value.

  • Obtains this validated if is valid, or a default value if not.

    Declaration

    Swift

    public func orElse(_ defaultValue: Validated<E, A>) -> Validated<E, A>

    Parameters

    defaultValue

    Value to return if this value is invalid.

    Return Value

    This value if it is valid, or the default one otherwise.

  • Undocumented

    Declaration

    Swift

    static func toPEither<E2, B>() -> PIso<Validated<E, A>, Validated<E2, B>, Either<E, A>, Either<E2, B>>
  • Undocumented

    Declaration

    Swift

    static func toEither() -> Iso<Validated<E, A>, Either<E, A>>
  • Undocumented

    Declaration

    Swift

    static func toPTry<B>() -> PIso<Validated<Error, A>, Validated<Error, B>, Try<A>, Try<B>>
  • Undocumented

    Declaration

    Swift

    static func toTry() -> Iso<Validated<Error, A>, Try<A>>
  • Undocumented

    Declaration

    Swift

    func toResult() -> Result<A, E>
  • Declaration

    Swift

    public var description: String { get }