All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swift5.Validation.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
// Validation.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//

import Foundation

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct StringRule {
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var minLength: Int?
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var maxLength: Int?
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var pattern: String?
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct NumericRule {
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var minimum: T?
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var exclusiveMinimum = false
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var maximum: T?
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var exclusiveMaximum = false
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var multipleOf: T?
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum StringValidationErrorKind: Error {
    case minLength, maxLength, pattern
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum NumericValidationErrorKind: Error {
    case minimum, maximum, multipleOf
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct ValidationError: Error {
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} fileprivate(set) var kinds: Set
}

{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct Validator {
    /// Validate a string against a rule.
    /// - Parameter string: The String you wish to validate.
    /// - Parameter rule: The StringRule you wish to use for validation.
    /// - Returns: A validated string.
    /// - Throws: `ValidationError` if the string is invalid against the rule,
    ///           `NSError` if the rule.pattern is invalid.
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate(_ string: String, against rule: StringRule) throws -> String {
        var error = ValidationError(kinds: [])
        if let minLength = rule.minLength, !(minLength <= string.count) {
            error.kinds.insert(.minLength)
        }
        if let maxLength = rule.maxLength, !(string.count <= maxLength) {
            error.kinds.insert(.maxLength)
        }
        if let pattern = rule.pattern {
            let matches = try NSRegularExpression(pattern: pattern, options: .caseInsensitive)
                .matches(in: string, range: .init(location: 0, length: string.utf16.count))
            if matches.isEmpty {
                error.kinds.insert(.pattern)
            }
        }
        guard error.kinds.isEmpty else {
            throw error
        }
        return string
    }

    /// Validate a integer against a rule.
    /// - Parameter numeric: The integer you wish to validate.
    /// - Parameter rule: The NumericRule you wish to use for validation.
    /// - Returns: A validated integer.
    /// - Throws: `ValidationError` if the numeric is invalid against the rule.
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate(_ numeric: T, against rule: NumericRule) throws -> T {
        var error = ValidationError(kinds: [])
        if let minium = rule.minimum {
            if !rule.exclusiveMinimum, minium > numeric {
                error.kinds.insert(.minimum)
            }
            if rule.exclusiveMinimum, minium >= numeric {
                error.kinds.insert(.minimum)
            }
        }
        if let maximum = rule.maximum {
            if !rule.exclusiveMaximum, numeric > maximum {
                error.kinds.insert(.maximum)
            }
            if rule.exclusiveMaximum, numeric >= maximum {
                error.kinds.insert(.maximum)
            }
        }
        if let multipleOf = rule.multipleOf, !numeric.isMultiple(of: multipleOf) {
            error.kinds.insert(.multipleOf)
        }
        guard error.kinds.isEmpty else {
            throw error
        }
        return numeric
    }

    /// Validate a fractional number against a rule.
    /// - Parameter numeric: The fractional number you wish to validate.
    /// - Parameter rule: The NumericRule you wish to use for validation.
    /// - Returns: A validated fractional number.
    /// - Throws: `ValidationError` if the numeric is invalid against the rule.
    {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate(_ numeric: T, against rule: NumericRule) throws -> T {
        var error = ValidationError(kinds: [])
        if let minium = rule.minimum {
            if !rule.exclusiveMinimum, minium > numeric {
                error.kinds.insert(.minimum)
            }
            if rule.exclusiveMinimum, minium >= numeric {
                error.kinds.insert(.minimum)
            }
        }
        if let maximum = rule.maximum {
            if !rule.exclusiveMaximum, numeric > maximum {
                error.kinds.insert(.maximum)
            }
            if rule.exclusiveMaximum, numeric >= maximum {
                error.kinds.insert(.maximum)
            }
        }
        if let multipleOf = rule.multipleOf, numeric.remainder(dividingBy: multipleOf) != 0 {
            error.kinds.insert(.multipleOf)
        }
        guard error.kinds.isEmpty else {
            throw error
        }
        return numeric
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy