Skip to content

[swift5]Add array validation rule #19242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ components:
maximum: 100
exclusiveMaximum: true
multipleOf: 5
ids:
type: array
items:
type: integer
minItems: 1
maxItems: 10
uniqueItems: false
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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>
}
Expand Down Expand Up @@ -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
}
}
Loading
Loading