[swift5]Add array validation rule (#19242)

* Add ArrayRule

* Run ./bin/utils/export_docs_generators.sh

* Add ArrayValidationErrorKind

* Add validation method

* Run ./bin/generate-samples.sh

* Add array rule property

* Rename minItem and maxItem => minItems and maxItems

* Fix uniqueItems template

* Tweaks

* Add sample property

* Run ./bin/generate-samples.sh
This commit is contained in:
Makoto Aoyama 2024-07-26 15:53:59 +09:00 committed by GitHub
parent 408706ef9d
commit fb023b192b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 684 additions and 1 deletions

View File

@ -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<T: Error & Hashable>: Error {
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -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}}

View File

@ -24,3 +24,10 @@ components:
maximum: 100
exclusiveMaximum: true
multipleOf: 5
ids:
type: array
items:
type: integer
minItems: 1
maxItems: 10
uniqueItems: false

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -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

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ internal struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
internal fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
internal static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -13,14 +13,18 @@ import AnyCodable
public struct Banana: Codable, JSONEncodable, Hashable {
static let countRule = NumericRule<Int>(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)
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -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)

View File

@ -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

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}

View File

@ -20,6 +20,12 @@ public struct NumericRule<T: Comparable & Numeric> {
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<T: Error & Hashable>: Error {
public fileprivate(set) var kinds: Set<T>
}
@ -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<ArrayValidationErrorKind>` if the string is invalid against the rule.
public static func validate(_ array: Array<AnyHashable>, against rule: ArrayRule) throws -> Array<AnyHashable> {
var error = ValidationError<ArrayValidationErrorKind>(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
}
}