Validation

public struct Validation<Value>

A struct for encapsulating validations.

  • Undocumented

    Declaration

    Swift

    public typealias Validate = (_ value: Value) throws -> Void
  • Undocumented

    Declaration

    Swift

    public let validate: Validate

Initialization

Greater than

  • Undocumented

    Declaration

    Swift

    typealias CountGreaterThanErrorMessageBuilder = (_ minimum: Int) -> String
  • Validate a collection’s count is greater than a minimum amount.

    Declaration

    Swift

    static func count<T: Collection>(
        greaterThan minimum: Int,
        message: CountGreaterThanErrorMessageBuilder? = nil
    ) -> Validation<T>

    Parameters

    greaterThan

    The minimum amount.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

  • Validate an optional collection’s count is greater than a minimum amount.

    If the value is nil, it is considered invalid.

    Declaration

    Swift

    static func count<T: Collection>(
        greaterThan minimum: Int,
        message: CountGreaterThanErrorMessageBuilder? = nil
    ) -> Validation<Optional<T>>

    Parameters

    greaterThan

    The minimum amount.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

Less than

  • Undocumented

    Declaration

    Swift

    typealias CountLessThanErrorMessageBuilder = (_ maximum: Int) -> String
  • Validate a collection’s count is less than a maximum amount.

    Declaration

    Swift

    static func count<T: Collection>(
        lessThan maximum: Int,
        message: CountLessThanErrorMessageBuilder? = nil
    ) -> Validation<T>

    Parameters

    lessThan

    The maximum amount.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

  • Validate a collection’s count is less than a maximum amount.

    If the value is nil, it is considered invalid.

    Declaration

    Swift

    static func count<T: Collection>(
        lessThan maximum: Int,
        message: CountLessThanErrorMessageBuilder? = nil
    ) -> Validation<Optional<T>>

    Parameters

    lessThan

    The maximum amount.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

Equal to

  • Undocumented

    Declaration

    Swift

    typealias CountEqualToErrorMessageBuilder = (_ value: Int) -> String
  • Validate a collection’s count is equal to an amount.

    Declaration

    Swift

    static func count<T: Collection>(
        equalTo count: Int,
        message: CountEqualToErrorMessageBuilder? = nil
    ) -> Validation<T>

    Parameters

    equalTo

    The value to be equal to.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

  • Validate an optional collection’s count is equal to an amount.

    If the value is nil, it is considered invalid.

    Declaration

    Swift

    static func count<T: Collection>(
        equalTo count: Int,
        message: CountEqualToErrorMessageBuilder? = nil
    ) -> Validation<Optional<T>>

    Parameters

    equalTo

    The value to be equal to.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

In

  • Undocumented

    Declaration

    Swift

    typealias CountRangeErrorMessageBuilder = (_ range: ClosedRange<Int>) -> String
  • Validate a collection’s count is in a range.

    Declaration

    Swift

    static func count<T: Collection>(
        in range: ClosedRange<Int>,
        message: CountRangeErrorMessageBuilder? = nil
    ) -> Validation<T>

    Parameters

    equalTo

    The value to be equal to.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

  • Validate an optional collection’s count is in a range.

    If the value is nil, it is considered invalid.

    Declaration

    Swift

    static func count<T: Collection>(
        in range: ClosedRange<Int>,
        message: CountRangeErrorMessageBuilder? = nil
    ) -> Validation<Optional<T>>

    Parameters

    equalTo

    The value to be equal to.

    message

    Closure to build custom error message.

    Return Value

    A Validation instance.

  • Validate a string matches a regular expression.

    Declaration

    Swift

    static func format(
        _ pattern: String,
        options: NSRegularExpression.Options = [],
        message: String? = nil
    ) -> Validation<String>

    Parameters

    pattern

    The regular expression pattern.

    options

    The regular expression options that are applied to the expression during matching. See NSRegularExpression.Options for possible values.

    message

    Custom error message.

    Return Value

    A Validation instance.

  • Validate an optional string matches a regular expression.

    Declaration

    Swift

    static func format(
        _ pattern: String,
        options: NSRegularExpression.Options = [],
        message: String? = nil
    ) -> Validation<Optional<String>>

    Parameters

    pattern

    The regular expression pattern.

    options

    The regular expression options that are applied to the expression during matching. See NSRegularExpression.Options for possible values.

    message

    Custom error message.

    Return Value

    A Validation instance.

  • Validate an optional value is present.

    If the value is nil, it is considered invalid.

    Declaration

    Swift

    static func presence<T>(
        message: String? = nil
    ) -> Validation<Optional<T>>

    Parameters

    message

    A custom error message.

    Return Value

    A Validation instance.

  • Validate an optional string is present.

    If the value is nil, it is considered invalid.

    When allowEmpty is true the string "" is valid. When allowEmpty is false the string "" is not valid.

    Declaration

    Swift

    static func presence(
        allowEmpty: Bool = false,
        message: String? = nil
    ) -> Validation<Optional<String>>

    Parameters

    allowEmpty

    Indicate whether an empty string validates as present.

    message

    A custom error message.

    Return Value

    A Validation instance.

  • Validate a string is not empty.

    As the string is not optional, this presence validation checks the string is not empty.

    Declaration

    Swift

    static func presence(
        message: String? = nil
    ) -> Validation<String>

    Parameters

    message

    A custom error message.

    Return Value

    A Validation instance.