@@ -10,12 +10,12 @@ import Foundation
10
10
11
11
12
12
public enum ValidationResult {
13
- case Valid
13
+ case valid
14
14
case invalid( [ String ] )
15
15
16
16
public var valid : Bool {
17
17
switch self {
18
- case . Valid :
18
+ case . valid :
19
19
return true
20
20
case . invalid:
21
21
return false
@@ -24,7 +24,7 @@ public enum ValidationResult {
24
24
25
25
public var errors : [ String ] ? {
26
26
switch self {
27
- case . Valid :
27
+ case . valid :
28
28
return nil
29
29
case . invalid( let errors) :
30
30
return errors
@@ -50,12 +50,12 @@ func flatten(_ results:[ValidationResult]) -> ValidationResult {
50
50
return . invalid( errors)
51
51
}
52
52
53
- return . Valid
53
+ return . valid
54
54
}
55
55
56
56
/// Creates a Validator which always returns an valid result
57
57
func validValidation( _ value: Any ) -> ValidationResult {
58
- return . Valid
58
+ return . valid
59
59
}
60
60
61
61
/// Creates a Validator which always returns an invalid result with the given error
@@ -74,36 +74,36 @@ func validateType(_ type: String) -> (_ value: Any) -> ValidationResult {
74
74
case " integer " :
75
75
if let number = value as? NSNumber {
76
76
if !CFNumberIsFloatType( number) && CFGetTypeID ( number) != CFBooleanGetTypeID ( ) {
77
- return . Valid
77
+ return . valid
78
78
}
79
79
}
80
80
case " number " :
81
81
if let number = value as? NSNumber {
82
82
if CFGetTypeID ( number) != CFBooleanGetTypeID ( ) {
83
- return . Valid
83
+ return . valid
84
84
}
85
85
}
86
86
case " string " :
87
87
if value is String {
88
- return . Valid
88
+ return . valid
89
89
}
90
90
case " object " :
91
91
if value is NSDictionary {
92
- return . Valid
92
+ return . valid
93
93
}
94
94
case " array " :
95
95
if value is NSArray {
96
- return . Valid
96
+ return . valid
97
97
}
98
98
case " boolean " :
99
99
if let number = value as? NSNumber {
100
100
if CFGetTypeID ( number) == CFBooleanGetTypeID ( ) {
101
- return . Valid
101
+ return . valid
102
102
}
103
103
}
104
104
case " null " :
105
105
if value is NSNull {
106
- return . Valid
106
+ return . valid
107
107
}
108
108
default :
109
109
break
@@ -136,7 +136,7 @@ func anyOf(_ validators:[Validator], error:String? = nil) -> (_ value: Any) -> V
136
136
for validator in validators {
137
137
let result = validator ( value)
138
138
if result. valid {
139
- return . Valid
139
+ return . valid
140
140
}
141
141
}
142
142
@@ -154,7 +154,7 @@ func oneOf(_ validators: [Validator]) -> (_ value: Any) -> ValidationResult {
154
154
let validValidators = results. filter { $0. valid } . count
155
155
156
156
if validValidators == 1 {
157
- return . Valid
157
+ return . valid
158
158
}
159
159
160
160
return . invalid( [ " \( validValidators) validates instead `oneOf`. " ] )
@@ -168,7 +168,7 @@ func not(_ validator: @escaping Validator) -> (_ value: Any) -> ValidationResult
168
168
return . invalid( [ " ' \( value) ' does not match 'not' validation. " ] )
169
169
}
170
170
171
- return . Valid
171
+ return . valid
172
172
}
173
173
}
174
174
@@ -181,7 +181,7 @@ func allOf(_ validators: [Validator]) -> (_ value: Any) -> ValidationResult {
181
181
func validateEnum( _ values: [ Any ] ) -> ( _ value: Any ) -> ValidationResult {
182
182
return { value in
183
183
if ( values as! [ NSObject ] ) . contains ( value as! NSObject ) {
184
- return . Valid
184
+ return . valid
185
185
}
186
186
187
187
return . invalid( [ " ' \( value) ' is not a valid enumeration value of ' \( values) ' " ] )
@@ -198,7 +198,7 @@ func validateLength(_ comparitor: @escaping ((Int, Int) -> (Bool)), length: Int,
198
198
}
199
199
}
200
200
201
- return . Valid
201
+ return . valid
202
202
}
203
203
}
204
204
@@ -216,7 +216,7 @@ func validatePattern(_ pattern: String) -> (_ value: Any) -> ValidationResult {
216
216
}
217
217
}
218
218
219
- return . Valid
219
+ return . valid
220
220
}
221
221
}
222
222
@@ -233,7 +233,7 @@ func validateMultipleOf(_ number: Double) -> (_ value: Any) -> ValidationResult
233
233
}
234
234
}
235
235
236
- return . Valid
236
+ return . valid
237
237
}
238
238
}
239
239
@@ -251,7 +251,7 @@ func validateNumericLength(_ length: Double, comparitor: @escaping ((Double, Dou
251
251
}
252
252
}
253
253
254
- return . Valid
254
+ return . valid
255
255
}
256
256
}
257
257
@@ -265,7 +265,7 @@ func validateArrayLength(_ rhs: Int, comparitor: @escaping ((Int, Int) -> Bool),
265
265
}
266
266
}
267
267
268
- return . Valid
268
+ return . valid
269
269
}
270
270
}
271
271
@@ -286,13 +286,13 @@ func validateUniqueItems(_ value: Any) -> ValidationResult {
286
286
let delta = ( hasTrueAndOne ? 1 : 0 ) + ( hasFalseAndZero ? 1 : 0 )
287
287
288
288
if ( NSSet ( array: value) . count + delta) == value. count {
289
- return . Valid
289
+ return . valid
290
290
}
291
291
292
292
return . invalid( [ " \( value) does not have unique items " ] )
293
293
}
294
294
295
- return . Valid
295
+ return . valid
296
296
}
297
297
298
298
// MARK: Object
@@ -305,21 +305,21 @@ func validatePropertiesLength(_ length: Int, comparitor: @escaping ((Int, Int) -
305
305
}
306
306
}
307
307
308
- return . Valid
308
+ return . valid
309
309
}
310
310
}
311
311
312
312
func validateRequired( _ required: [ String ] ) -> ( _ value: Any ) -> ValidationResult {
313
313
return { value in
314
314
if let value = value as? [ String : Any ] {
315
315
if ( required. filter { r in !value. keys. contains ( r) } . count == 0 ) {
316
- return . Valid
316
+ return . valid
317
317
}
318
318
319
319
return . invalid( [ " Required properties are missing ' \( required) ' " ] )
320
320
}
321
321
322
- return . Valid
322
+ return . valid
323
323
}
324
324
}
325
325
@@ -363,7 +363,7 @@ func validateProperties(_ properties: [String:Validator]?, patternProperties: [S
363
363
return flatten ( results)
364
364
}
365
365
366
- return . Valid
366
+ return . valid
367
367
}
368
368
}
369
369
@@ -401,25 +401,25 @@ func validateIPv4(_ value:Any) -> ValidationResult {
401
401
if let ipv4 = value as? String {
402
402
if let expression = try ? NSRegularExpression ( pattern: " ^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) \\ .(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ " , options: NSRegularExpression . Options ( rawValue: 0 ) ) {
403
403
if expression. matches ( in: ipv4, options: NSRegularExpression . MatchingOptions ( rawValue: 0 ) , range: NSMakeRange ( 0 , ipv4. characters. count) ) . count == 1 {
404
- return . Valid
404
+ return . valid
405
405
}
406
406
}
407
407
408
408
return . invalid( [ " ' \( ipv4) ' is not valid IPv4 address. " ] )
409
409
}
410
410
411
- return . Valid
411
+ return . valid
412
412
}
413
413
414
414
func validateIPv6( _ value: Any ) -> ValidationResult {
415
415
if let ipv6 = value as? String {
416
416
var buf = UnsafeMutablePointer< Int8> . allocate( capacity: Int ( INET6_ADDRSTRLEN) )
417
417
if inet_pton ( AF_INET6, ipv6, & buf) == 1 {
418
- return . Valid
418
+ return . valid
419
419
}
420
420
421
421
return . invalid( [ " ' \( ipv6) ' is not valid IPv6 address. " ] )
422
422
}
423
423
424
- return . Valid
424
+ return . valid
425
425
}
0 commit comments