Skip to content

Commit 154b587

Browse files
committed
Added five possible JSON responses to exercise various paths of validation.
1 parent 00a1500 commit 154b587

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

JSON responses.txt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
A:
2+
{
3+
"description":"7/11 Fake St.",
4+
"price":3.50,
5+
"sellsDiesel": true,
6+
"address": {
7+
"street":"123 Fake Street",
8+
"city":"Newark",
9+
"state":"NJ",
10+
"zip":"07105"
11+
},
12+
"historicalPrices":[
13+
{
14+
"date":"2013-02-18T15:43:24-05:00",
15+
"price":4.60
16+
}
17+
]
18+
}
19+
20+
21+
B:
22+
{
23+
"description":"7/11 Fake St.",
24+
"price":"$3.50",
25+
"sellsDiesel": "yes",
26+
"address": {
27+
"street":"123 Fake Street",
28+
"city":"Newark",
29+
"state":"NJ",
30+
"zip":7105
31+
},
32+
"historicalPrices":[
33+
{
34+
"date":"2013-02-18T15:43:24-05:00",
35+
"price":"$4.60"
36+
}
37+
]
38+
}
39+
40+
41+
C:
42+
{
43+
"description":"7/11 Fake St.",
44+
"price":4,
45+
"sellsDiesel": 1,
46+
"address": {
47+
"street":"123 Fake Street",
48+
"city":"Newark",
49+
"state":"NJ",
50+
"zip":7105
51+
},
52+
"historicalPrices":[
53+
{
54+
"date":1361202204,
55+
"price":4.6
56+
}
57+
]
58+
}
59+
60+
61+
D:
62+
{
63+
"description":"7/11 Fake St.",
64+
"price":3.5,
65+
"sellsDiesel": true,
66+
"address": {
67+
"street":"123 Fake Street",
68+
"city":"Newark",
69+
"state":"NJ",
70+
"zip":"07105"
71+
},
72+
"historicalPrices":{
73+
"date":"2013-02-18T15:43:24-05:00",
74+
"price":4.60
75+
}
76+
}
77+
78+
79+
E:
80+
{
81+
"description":null,
82+
"price":null,
83+
"sellsDiesel": null,
84+
"address":"error looking up address",
85+
"historicalPrices":"error fetching prices"
86+
}

KVC Validation Pattern/KVC Validation Pattern/CTCViewController.m

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef NS_ENUM(NSUInteger, CTVViewControllerSection) {
1313
CTVViewControllerSectionHistoricalPrices = 1
1414
};
1515

16+
1617
@implementation CTCViewController
1718

1819
- (void)viewDidLoad {
@@ -110,4 +111,127 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
110111
return cell;
111112
}
112113

114+
115+
#pragma mark - JSON responses
116+
117+
- (NSString*)jsonStringForResponse:(NSUInteger)responseIndex {
118+
switch (responseIndex) {
119+
case 0:
120+
default:
121+
/* Response A: happy path
122+
123+
{
124+
"description":"7/11 Fake St.",
125+
"price":3.50,
126+
"sellsDiesel": true,
127+
"address": {
128+
"street":"123 Fake Street",
129+
"city":"Newark",
130+
"state":"NJ",
131+
"zip":"07105"
132+
},
133+
"historicalPrices":[
134+
{
135+
"date":"2013-02-18T15:43:24-05:00",
136+
"price":4.60
137+
}
138+
]
139+
}
140+
141+
*/
142+
143+
return @"{ \"description\":\"7/11 Fake St.\", \"price\":3.50, \"sellsDiesel\": true, \"address\": { \"street\":\"123 Fake Street\", \"city\":\"Newark\", \"state\":\"NJ\", \"zip\":\"07105\" }, \"historicalPrices\":[ { \"date\":\"2013-02-18T15:43:24-05:00\", \"price\":4.60 } ] }";
144+
145+
case 1:
146+
/* Response B: price & sellsDiesel are strings, zip is an integer
147+
148+
{
149+
"description":"7/11 Fake St.",
150+
"price":"$3.50",
151+
"sellsDiesel": "yes",
152+
"address": {
153+
"street":"123 Fake Street",
154+
"city":"Newark",
155+
"state":"NJ",
156+
"zip":7105
157+
},
158+
"historicalPrices":[
159+
{
160+
"date":"2013-02-18T15:43:24-05:00",
161+
"price":"$4.60"
162+
}
163+
]
164+
}
165+
166+
*/
167+
168+
return @"{ \"description\":\"7/11 Fake St.\", \"price\":\"$3.50\", \"sellsDiesel\": \"yes\", \"address\": { \"street\":\"123 Fake Street\", \"city\":\"Newark\", \"state\":\"NJ\", \"zip\":7105 }, \"historicalPrices\":[ { \"date\":\"2013-02-18T15:43:24-05:00\", \"price\":\"$4.60\" } ] }";
169+
170+
171+
case 2:
172+
/* Response C: price & sellsDiesel are integers, historicalPrices/price is a decimal, date is a UNIX timestamp
173+
174+
{
175+
"description":"7/11 Fake St.",
176+
"price":4,
177+
"sellsDiesel": 1,
178+
"address": {
179+
"street":"123 Fake Street",
180+
"city":"Newark",
181+
"state":"NJ",
182+
"zip":7105
183+
},
184+
"historicalPrices":[
185+
{
186+
"date":1361202204,
187+
"price":4.6
188+
}
189+
]
190+
}
191+
192+
*/
193+
194+
return @"{ \"description\":\"7/11 Fake St.\", \"price\":4, \"sellsDiesel\": 1, \"address\": { \"street\":\"123 Fake Street\", \"city\":\"Newark\", \"state\":\"NJ\", \"zip\":7105 }, \"historicalPrices\":[ { \"date\":1361202204, \"price\":4.6 } ] }";
195+
196+
197+
case 3:
198+
/* Response D: historicalPrices is a dictionary instead of an array
199+
200+
{
201+
"description":"7/11 Fake St.",
202+
"price":3.50,
203+
"sellsDiesel": true,
204+
"address": {
205+
"street":"123 Fake Street",
206+
"city":"Newark",
207+
"state":"NJ",
208+
"zip":"07105"
209+
},
210+
"historicalPrices":{
211+
"date":"2013-02-18T15:43:24-05:00",
212+
"price":4.60
213+
}
214+
}
215+
216+
*/
217+
218+
return @"{ \"description\":\"7/11 Fake St.\", \"price\":3.50, \"sellsDiesel\": true, \"address\": { \"street\":\"123 Fake Street\", \"city\":\"Newark\", \"state\":\"NJ\", \"zip\":\"07105\" }, \"historicalPrices\":{ \"date\":\"2013-02-18T15:43:24-05:00\", \"price\":4.60 } }";
219+
220+
case 4:
221+
/* Response E: description, price and sellsDiesel are null; address and historicalPrices are strings
222+
223+
{
224+
"description":null,
225+
"price":null,
226+
"sellsDiesel": null,
227+
"address":"error looking up address",
228+
"historicalPrices":"error fetching prices"
229+
}
230+
231+
*/
232+
233+
return @"{ \"description\":null, \"price\":null, \"sellsDiesel\": null, \"address\":\"error looking up address\", \"historicalPrices\":\"error fetching prices\" }";
234+
}
235+
}
236+
113237
@end

0 commit comments

Comments
 (0)