Validator

public struct Validator<T>: CustomStringConvertible

A discrete Validator. Usually created by calling ValidatorType.validator().

All validation operators (&&, ||, !, etc) work on Validators.

try validations.add(\.firstName, .count(5...) && .alphanumeric)

Adding static properties to this type will enable leading-dot syntax when composing validators.

extension Validator {
    static var myValidation: Validator<T> { return MyValidator().validator() }
}
  • Readable name explaining what this Validator does. Must be suitable for placing after is and `is not.

    is alphanumeric
    is not alphanumeric
    

    Declaration

    Swift

    public var readable: String
  • See CustomStringConvertible.

    Declaration

    Swift

    public var description: String
  • Creates a new Validation.

    Declaration

    Swift

    public init(_ readable: String, _ closure: @escaping (T) throws -> Void)

    Parameters

    readable

    Readable name, suitable for placing after is and is not.

    validate

    Validates the supplied ValidationData, throwing an error if it is not valid.

  • Validates the supplied ValidationData, throwing an error if it is not valid.

    Throws

    ValidationError if the data is not valid, or another error if something fails.

    Declaration

    Swift

    public func validate(_ data: T) throws

    Parameters

    data

    ValidationData to validate.

  • Validates that all characters in a String are ASCII (bytes 0..<128).

    try validations.add(\.name, .ascii)
    

    Declaration

    Swift

    public static var ascii: Validator<String>
  • Validates that all characters in a String are alphanumeric (a-z,A-Z,0-9).

    try validations.add(\.name, .alphanumeric)
    

    Declaration

    Swift

    public static var alphanumeric: Validator<String>
  • Validates that all characters in a String are in the supplied CharacterSet.

    try validations.add(\.name, .characterSet(.alphanumerics + .whitespaces))
    

    Declaration

    Swift

    public static func characterSet(_ characterSet: CharacterSet) -> Validator<String>
  • Validates that the data’s count is within the supplied ClosedRange.

    try validations.add(\.name, .count(5...10))
    

    Declaration

    Swift

    public static func count(_ range: ClosedRange<Int>) -> Validator<T>
  • Validates that the data’s count is less than the supplied upper bound using PartialRangeThrough.

    try validations.add(\.name, .count(...10))
    

    Declaration

    Swift

    public static func count(_ range: PartialRangeThrough<Int>) -> Validator<T>
  • Validates that the data’s count is less than the supplied lower bound using PartialRangeFrom.

    try validations.add(\.name, .count(5...))
    

    Declaration

    Swift

    public static func count(_ range: PartialRangeFrom<Int>) -> Validator<T>
  • Validates that the data’s count is within the supplied Range.

    try validations.add(\.name, .count(5..<10))
    

    Declaration

    Swift

    public static func count(_ range: Range<Int>) -> Validator<T>
  • Validates whether a String is a valid email address.

    try validations.add(\.email, .email)
    

    Declaration

    Swift

    public static var email: Validator<T>
  • Validates whether an item is contained in the supplied array.

    try validations.add(\.name, .in("foo", "bar"))
    

    Declaration

    Swift

    public static func `in`(_ array: T...) -> Validator<T>
  • Validates whether an item is contained in the supplied array.

    try validations.add(\.name, .in(["foo", "bar"]))
    

    Declaration

    Swift

    public static func `in`(_ array: [T]) -> Validator<T>
  • nil

    Validates that the data is nil. Combine with the not-operator ! to validate that the data is not nil.

    try validations.add(\.email, .email && !.nil)
    

    Declaration

    Swift

    public static var `nil`: Validator<T.WrappedType?>
  • Validates that the data is within the supplied ClosedRange.

    try validations.add(\.age, .range(5...10))
    

    Declaration

    Swift

    public static func range(_ range: ClosedRange<T>) -> Validator<T>
  • Validates that the data is less than the supplied upper bound using PartialRangeThrough.

    try validations.add(\.age, .range(...10))
    

    Declaration

    Swift

    public static func range(_ range: PartialRangeThrough<T>) -> Validator<T>
  • Validates that the data is less than the supplied lower bound using PartialRangeFrom.

    try validations.add(\.age, .range(5...))
    

    Declaration

    Swift

    public static func range(_ range: PartialRangeFrom<T>) -> Validator<T>
  • Validates that the data is within the supplied Range.

    try validations.add(\.age, .range(5..<10))
    

    Declaration

    Swift

    public static func range(_ range: Range<T>) -> Validator<T>