swift5.Validation.mustache Maven / Gradle / Ivy
// 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}} struct ArrayRule {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var minItems: Int?
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var maxItems: Int?
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} var uniqueItems: Bool
}
{{#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}} enum ArrayValidationErrorKind: Error {
case minItems, maxItems, uniqueItems
}
{{#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
}
/// Validate a array against a rule.
/// - Parameter array: The Array you wish to validate.
/// - Parameter rule: The ArrayRule you wish to use for validation.
/// - Returns: A validated array.
/// - Throws: `ValidationError` if the string is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate(_ array: Array, against rule: ArrayRule) throws -> Array {
var error = ValidationError(kinds: [])
if let minItems = rule.minItems, !(minItems <= array.count) {
error.kinds.insert(.minItems)
}
if let maxItems = rule.maxItems, !(array.count <= maxItems) {
error.kinds.insert(.maxItems)
}
if rule.uniqueItems {
let unique = Set(array)
if unique.count != array.count {
error.kinds.insert(.uniqueItems)
}
}
guard error.kinds.isEmpty else {
throw error
}
return array
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy