Skip to content

Commit 1bd19f4

Browse files
committed
Print schema errors in debug, and fail in prod.
1 parent 1c2f4d3 commit 1bd19f4

File tree

1 file changed

+73
-4
lines changed

1 file changed

+73
-4
lines changed

PatientScannerDemo/Models/HCert.swift

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,90 @@ import SwiftyJSON
1010
import JSONSchema
1111

1212
struct HCert {
13-
init(from cborData: Data) {
13+
init?(from cborData: Data) {
1414
let headerStr = CBOR.header(from: cborData)?.toString() ?? "{}"
1515
let bodyStr = CBOR.payload(from: cborData)?.toString() ?? "{}"
1616
header = JSON(parseJSON: headerStr)
17-
body = JSON(parseJSON: bodyStr)
17+
var body = JSON(parseJSON: bodyStr)
18+
if body["-259"].exists() {
19+
body = body["-259"]
20+
}
21+
if body["1"].exists() {
22+
body = body["1"]
23+
}
24+
// body = JSON(parseJSON: """
25+
// {
26+
// "vac" : [
27+
// {
28+
// "seq" : 1,
29+
// "lot" : "C22-862FF-001",
30+
// "dis" : "840539006",
31+
// "adm" : "Vaccination centre Vienna 23",
32+
// "vap" : "1119305005",
33+
// "mep" : "EU\\/1\\/20\\/1528",
34+
// "tot" : 2,
35+
// "aut" : "ORG-100030215",
36+
// "dat" : "2021-02-18",
37+
// "cou" : "AT"
38+
// },
39+
// {
40+
// "seq" : 2,
41+
// "lot" : "C22-H62FF-010",
42+
// "dis" : "840539006",
43+
// "adm" : "Vaccination centre Vienna 23",
44+
// "vap" : "1119305005",
45+
// "mep" : "EU\\/1\\/20\\/1528",
46+
// "tot" : 2,
47+
// "aut" : "ORG-100030215",
48+
// "dat" : "2021-03-12",
49+
// "cou" : "AT"
50+
// }
51+
// ],
52+
// "cert" : {
53+
// "id" : "01AT42196560275230427402470256520250042",
54+
// "is" : "Ministry of Health, Austria",
55+
// "vr" : "v1.0",
56+
// "vf" : "2021-04-04",
57+
// "vu" : "2021-10-04",
58+
// "co" : "AT"
59+
// },
60+
// "sub" : {
61+
// "gen" : "female",
62+
// "dob" : "1998-02-26",
63+
// "id" : [
64+
// {
65+
// "i" : "12345ABC-321",
66+
// "t" : "PPN"
67+
// }
68+
// ],
69+
// "gn" : "Gabriele",
70+
// "fn" : "Musterfrau"
71+
// }
72+
// }
73+
//""")
1874

1975
let schema = JSON(parseJSON: EU_DGC_SCHEMA).dictionaryObject!
2076
let bodyDict = body.dictionaryObject!
2177

22-
let validation = try? validate(bodyDict, schema: schema)
23-
if let errors = validation?.errors {
78+
guard
79+
let validation = try? validate(bodyDict, schema: schema)
80+
else {
81+
return nil
82+
}
83+
#if DEBUG
84+
if let errors = validation.errors {
2485
for err in errors {
2586
print(err.description)
2687
}
2788
}
89+
#else
90+
if !validation.valid {
91+
return nil
92+
}
93+
#endif
94+
self.body = body
95+
print(header)
96+
print(body)
2897
}
2998

3099
var header: JSON

0 commit comments

Comments
 (0)