diff --git a/modules/openapi-generator/src/main/resources/swift5/Validation.mustache b/modules/openapi-generator/src/main/resources/swift5/Validation.mustache index 8457a891fdca..70e3abfb5057 100644 --- a/modules/openapi-generator/src/main/resources/swift5/Validation.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/Validation.mustache @@ -20,6 +20,12 @@ import Foundation {{#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 } @@ -28,6 +34,10 @@ import Foundation 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 } @@ -123,4 +133,29 @@ import Foundation } 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 + } } diff --git a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache index 58105900d606..71025d946997 100644 --- a/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/modelObject.mustache @@ -16,6 +16,9 @@ {{#isNumeric}} static let {{{name}}}Rule = NumericRule<{{{dataType}}}>(minimum: {{#minimum}}{{{.}}}{{/minimum}}{{^minimum}}nil{{/minimum}}, exclusiveMinimum: {{#exclusiveMinimum}}true{{/exclusiveMinimum}}{{^exclusiveMinimum}}false{{/exclusiveMinimum}}, maximum: {{#maximum}}{{{.}}}{{/maximum}}{{^maximum}}nil{{/maximum}}, exclusiveMaximum: {{#exclusiveMaximum}}true{{/exclusiveMaximum}}{{^exclusiveMaximum}}false{{/exclusiveMaximum}}, multipleOf: {{#multipleOf}}{{{.}}}{{/multipleOf}}{{^multipleOf}}nil{{/multipleOf}}) {{/isNumeric}} +{{#isArray}} + static let {{{name}}}Rule = ArrayRule(minItems: {{#minItems}}{{{.}}}{{/minItems}}{{^minItems}}nil{{/minItems}}, maxItems: {{#maxItems}}{{{.}}}{{/maxItems}}{{^maxItems}}nil{{/maxItems}}, uniqueItems: {{#uniqueItems}}true{{/uniqueItems}}{{^uniqueItems}}false{{/uniqueItems}}) +{{/isArray}} {{/hasValidation}} {{/validatable}} {{/allVars}} diff --git a/modules/openapi-generator/src/test/resources/3_0/validation.yaml b/modules/openapi-generator/src/test/resources/3_0/validation.yaml index 559a895a9453..13b519516299 100644 --- a/modules/openapi-generator/src/test/resources/3_0/validation.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/validation.yaml @@ -24,3 +24,10 @@ components: maximum: 100 exclusiveMaximum: true multipleOf: 5 + ids: + type: array + items: + type: integer + minItems: 1 + maxItems: 10 + uniqueItems: false \ No newline at end of file diff --git a/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/anycodableLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/asyncAwaitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/combineLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Pet.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Pet.swift index abd848e5836f..394619c072c6 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Pet.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Models/Pet.swift @@ -17,6 +17,7 @@ public struct Pet: Codable, JSONEncodable, Hashable { case pending = "pending" case sold = "sold" } + static let photoUrlsRule = ArrayRule(minItems: nil, maxItems: nil, uniqueItems: true) public var id: Int64? public var category: Category? public var name: String diff --git a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/default/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/deprecated/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/frozenEnums/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Validation.swift index 912ba84c9090..ced1069dd454 100644 --- a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ internal struct NumericRule { internal var multipleOf: T? } +internal struct ArrayRule { + internal var minItems: Int? + internal var maxItems: Int? + internal var uniqueItems: Bool +} + internal enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ internal enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +internal enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + internal struct ValidationError: Error { internal fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ internal struct Validator { } 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. + internal 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 + } } diff --git a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/oneOf/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/promisekitLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/readonlyProperties/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/resultLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/rxswiftLibrary/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Validation.swift b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Validation.swift +++ b/samples/client/petstore/swift5/urlsessionLibrary/Sources/PetstoreClient/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models/Banana.swift b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models/Banana.swift index a58981710b1e..5eb857cb6a95 100644 --- a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models/Banana.swift +++ b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Models/Banana.swift @@ -13,14 +13,18 @@ import AnyCodable public struct Banana: Codable, JSONEncodable, Hashable { static let countRule = NumericRule(minimum: 10, exclusiveMinimum: true, maximum: 100, exclusiveMaximum: true, multipleOf: 5) + static let idsRule = ArrayRule(minItems: 1, maxItems: 10, uniqueItems: false) public var count: Int? + public var ids: [Int]? - public init(count: Int? = nil) { + public init(count: Int? = nil, ids: [Int]? = nil) { self.count = count + self.ids = ids } public enum CodingKeys: String, CodingKey, CaseIterable { case count + case ids } // Encodable protocol methods @@ -28,6 +32,7 @@ public struct Banana: Codable, JSONEncodable, Hashable { public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) try container.encodeIfPresent(count, forKey: .count) + try container.encodeIfPresent(ids, forKey: .ids) } } diff --git a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/validation/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/validation/docs/Banana.md b/samples/client/petstore/swift5/validation/docs/Banana.md index 3c90b8d7d989..56da27c9049f 100644 --- a/samples/client/petstore/swift5/validation/docs/Banana.md +++ b/samples/client/petstore/swift5/validation/docs/Banana.md @@ -4,6 +4,7 @@ Name | Type | Description | Notes ------------ | ------------- | ------------- | ------------- **count** | **Int** | | [optional] +**ids** | **[Int]** | | [optional] [[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md) diff --git a/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Models/Pet.swift b/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Models/Pet.swift index 90481c91eced..73c5b9d0b652 100644 --- a/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Models/Pet.swift +++ b/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Models/Pet.swift @@ -18,6 +18,7 @@ public final class Pet: Content, Hashable { case pending = "pending" case sold = "sold" } + static let photoUrlsRule = ArrayRule(minItems: nil, maxItems: nil, uniqueItems: true) public var id: Int64? public var category: Category? public var name: String diff --git a/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Validation.swift b/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Validation.swift +++ b/samples/client/petstore/swift5/vaporLibrary/Sources/PetstoreClient/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } } diff --git a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Validation.swift b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Validation.swift index b520bd7e5a75..6a0d4c9da1ab 100644 --- a/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Validation.swift +++ b/samples/client/petstore/swift5/x-swift-hashable/PetstoreClient/Classes/OpenAPIs/Validation.swift @@ -20,6 +20,12 @@ public struct NumericRule { public var multipleOf: T? } +public struct ArrayRule { + public var minItems: Int? + public var maxItems: Int? + public var uniqueItems: Bool +} + public enum StringValidationErrorKind: Error { case minLength, maxLength, pattern } @@ -28,6 +34,10 @@ public enum NumericValidationErrorKind: Error { case minimum, maximum, multipleOf } +public enum ArrayValidationErrorKind: Error { + case minItems, maxItems, uniqueItems +} + public struct ValidationError: Error { public fileprivate(set) var kinds: Set } @@ -123,4 +133,29 @@ public struct Validator { } 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. + public 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 + } }