-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionalValidation.swift
62 lines (54 loc) · 1.9 KB
/
OptionalValidation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// OptionalValidation.swift
//
// Created by N A Shashank on 1/23/18.
// Copyright © 2018 N A Shashank. All rights reserved.
//
import UIKit
struct ValidationError:Error {
var description = String()
}
protocol OptionalValidation {
func validate() -> ValidationError?
}
extension OptionalValidation {
func validate() -> ValidationError? {
// first fetch all instance values for this class
let mirror = Mirror(reflecting: self)
for child in mirror.children
{
let subMirror = Mirror(reflecting:child.value)
guard subMirror.displayStyle == Mirror.DisplayStyle.optional else{
continue
}
guard subMirror.children.count == 0 else{
continue
}
return ValidationError(description: "Please fill all the required fields.")
}
return nil
}
// recursive model check uncomment if required
// func validate(object:Any) -> ValidationError? {
// let mirror = Mirror(reflecting: object)
// for child in mirror.children {
// let subMirror = Mirror(reflecting: child.value)
// guard subMirror.children.count == 0 else{
// guard let _ = validate(object: child.value) else {
// continue
// }
// return ValidationError(description: "Please fill all the required fields.")
// }
// guard subMirror.displayStyle == Mirror.DisplayStyle.optional else{
// //arrHolder.append(child.value)
// continue
// }
// guard subMirror.children.count == 0 else{
// //arrHolder.append(child.value)
// continue
// }
// return ValidationError(description: "Please fill all the required fields.")
// }
// return nil
// }
}