Skip to content

Commit 641fcd4

Browse files
BrianL3Brian Ledbetter
andauthored
feature/json-validation (#157)
* JSON validation implemented so we can have required properties on model objects. Co-authored-by: Brian Ledbetter <[email protected]>
1 parent aa0e015 commit 641fcd4

39 files changed

+455
-422
lines changed

src/containers/PeopleContainer/PeopleContainer.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ export interface PeopleContainerProps extends PeoplePageData {
88
}
99

1010
function renderLinkCard(person: Person) {
11-
if (person.id) {
12-
return (
13-
<div>
14-
<Link to={`/people/${person.id}`}>{person.name}</Link>
15-
</div>
16-
);
17-
} else {
18-
return null;
19-
}
11+
return (
12+
<div>
13+
<Link to={`/people/${person.id}`}>{person.name}</Link>
14+
</div>
15+
);
2016
}
2117

2218
const PersonContainer = ({ currentPeople, allPeople }: PeopleContainerProps) => {

src/containers/PersonContainer/PersonContainer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const PersonContainer = ({ person, votes, roles, mattersSponsored }: PersonConta
2626
<div key={person.id}>
2727
{isMobile && (
2828
<PersonCard
29-
personName={person.name!}
29+
personName={person.name}
3030
personPictureSrc={""}
31-
personIsActive={person.is_active!}
31+
personIsActive={person.is_active}
3232
seatName={currentRole.seat?.name || "No name"}
3333
seatElectoralArea={currentRole.seat?.electoral_area || "No Electoral Area"}
3434
seatPictureSrc={currentRole.seat?.image?.uri}

src/models/Body.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import { ResponseData } from "../networking/NetworkResponse";
22
import { Model } from "./Model";
33
import firestoreTimestampToDate from "../utils/firestoreTimestampToDate";
4-
54
export default class Body implements Model {
6-
id?: string;
5+
id: string;
76
description?: string;
87
end_datetime?: Date;
98
external_source_id?: string;
10-
is_active?: boolean;
11-
name?: string;
12-
start_datetime?: Date;
9+
is_active: boolean;
10+
name: string;
11+
start_datetime: Date;
1312

1413
constructor(jsonData: ResponseData) {
15-
if (jsonData["id"]) {
16-
this.id = jsonData["id"];
17-
}
14+
this.id = jsonData["id"];
15+
this.is_active = jsonData["is_active"];
16+
this.start_datetime = firestoreTimestampToDate(jsonData["start_datetime"]);
17+
this.name = jsonData["name"];
18+
1819
if (jsonData["description"]) {
1920
this.description = jsonData["description"];
2021
}
@@ -24,14 +25,5 @@ export default class Body implements Model {
2425
if (jsonData["external_source_id"]) {
2526
this.external_source_id = jsonData["external_source_id"];
2627
}
27-
if (jsonData["is_active"]) {
28-
this.is_active = jsonData["is_active"];
29-
}
30-
if (jsonData["start_datetime"]) {
31-
this.start_datetime = firestoreTimestampToDate(jsonData["start_datetime"]);
32-
}
33-
if (jsonData["name"]) {
34-
this.name = jsonData["name"];
35-
}
3628
}
3729
}

src/models/Event.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { Model } from "./Model";
66
import { DocumentReference } from "@firebase/firestore";
77

88
class Event implements Model {
9-
id?: string;
9+
id: string;
1010
agenda_uri?: string;
11-
body_ref?: string;
11+
body_ref: string;
1212
body?: Body;
13-
event_datetime?: Date;
13+
event_datetime: Date;
1414
external_source_id?: string;
1515
hover_thumbnail_ref?: string;
1616
hover_thumbnail?: File;
@@ -19,9 +19,9 @@ class Event implements Model {
1919
static_thumbnail?: File;
2020

2121
constructor(jsonData: ResponseData) {
22-
if (jsonData["id"]) {
23-
this.id = jsonData["id"];
24-
}
22+
this.id = jsonData["id"];
23+
this.event_datetime = firestoreTimestampToDate(jsonData["event_datetime"]);
24+
this.body_ref = jsonData["body_ref"].id;
2525

2626
if (jsonData["agenda_uri"]) {
2727
this.agenda_uri = jsonData["agenda_uri"];
@@ -31,16 +31,11 @@ class Event implements Model {
3131
this.minutes_uri = jsonData["minutes_uri"];
3232
}
3333

34-
if (jsonData["body_ref"]) {
35-
if (jsonData["body_ref"] instanceof DocumentReference) {
36-
this.body_ref = jsonData["body_ref"].id;
37-
} else if (typeof jsonData["body_ref"] === "object") {
38-
this.body = new Body(jsonData["body_ref"]);
39-
}
40-
}
41-
42-
if (jsonData["event_datetime"]) {
43-
this.event_datetime = firestoreTimestampToDate(jsonData["event_datetime"]);
34+
if (
35+
typeof jsonData["body_ref"] === "object" &&
36+
!(jsonData["body_ref"] instanceof DocumentReference)
37+
) {
38+
this.body = new Body(jsonData["body_ref"]);
4439
}
4540

4641
if (jsonData["external_source_id"]) {

src/models/EventMinutesItem.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,45 @@ import { ResponseData } from "../networking/NetworkResponse";
22
import Event from "./Event";
33
import MinutesItem from "./MinutesItem";
44
import { Model } from "./Model";
5-
import { DocumentReference } from "@firebase/firestore";
5+
import { DocumentReference } from "firebase/firestore";
66

77
export default class EventMinutesItem implements Model {
8-
id?: string;
8+
id: string;
99
decision?: string;
10-
event_ref?: string;
10+
event_ref: string;
1111
event?: Event;
1212
external_source_id?: string;
13-
index?: number;
14-
minutes_item_ref?: string;
13+
index: number;
14+
minutes_item_ref: string;
1515
minutes_item?: MinutesItem;
1616

1717
constructor(jsonData: ResponseData) {
18-
if (jsonData["id"]) {
19-
this.id = jsonData["id"];
20-
}
18+
this.id = jsonData["id"];
2119

2220
if (jsonData["decision"]) {
2321
this.decision = jsonData["decision"];
2422
}
2523

26-
if (jsonData["event_ref"]) {
27-
if (jsonData["event_ref"] instanceof DocumentReference) {
28-
this.event_ref = jsonData["event_ref"].id;
29-
} else if (typeof jsonData["event_ref"] === "object") {
30-
this.event = new Event(jsonData["event_ref"]);
31-
}
32-
}
24+
this.event_ref = jsonData["event_ref"].id;
3325

26+
if (
27+
typeof jsonData["event_ref"] === "object" &&
28+
!(jsonData["event_ref"] instanceof DocumentReference)
29+
) {
30+
this.event = new Event(jsonData["event_ref"]);
31+
}
3432
if (jsonData["external_source_id"]) {
3533
this.external_source_id = jsonData["external_source_id"];
3634
}
3735

38-
if (jsonData["index"]) {
39-
this.index = jsonData["index"];
40-
}
36+
this.index = jsonData["index"];
4137

42-
if (jsonData["minutes_item_ref"]) {
43-
if (jsonData["minutes_item_ref"] instanceof DocumentReference) {
44-
this.minutes_item_ref = jsonData["minutes_item_ref"].id;
45-
} else if (typeof jsonData["minutes_item_ref"] === "object") {
46-
this.minutes_item = new MinutesItem(jsonData["minutes_item_ref"]);
47-
}
38+
this.minutes_item_ref = jsonData["minutes_item_ref"].id;
39+
if (
40+
typeof jsonData["minutes_item_ref"] === "object" &&
41+
!(jsonData["minutes_item_ref"] instanceof DocumentReference)
42+
) {
43+
this.minutes_item = new MinutesItem(jsonData["minutes_item_ref"]);
4844
}
4945
}
5046
}

src/models/EventMinutesItemFile.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
import { ResponseData } from "../networking/NetworkResponse";
22
import EventMinutesItem from "./EventMinutesItem";
33
import { Model } from "./Model";
4-
import { DocumentReference } from "@firebase/firestore";
5-
4+
import { DocumentReference } from "firebase/firestore";
65
export default class EventMinutesItemFile implements Model {
7-
id?: string;
8-
event_minutes_item_ref?: string;
6+
id: string;
7+
event_minutes_item_ref: string;
98
event_minutes_item?: EventMinutesItem;
109
external_source_id?: string;
11-
name?: string;
12-
uri?: string;
10+
name: string;
11+
uri: string;
1312

1413
constructor(jsonData: ResponseData) {
15-
if (jsonData["id"]) {
16-
this.id = jsonData["id"];
17-
}
14+
this.id = jsonData["id"];
15+
16+
this.event_minutes_item_ref = jsonData["event_minutes_item_ref"].id;
1817

19-
if (jsonData["event_minutes_item_ref"]) {
20-
if (jsonData["event_minutes_item_ref"] instanceof DocumentReference) {
21-
this.event_minutes_item_ref = jsonData["event_minutes_item_ref"].id;
22-
} else if (typeof jsonData["event_minutes_item_ref"] === "object") {
23-
this.event_minutes_item = new EventMinutesItem(jsonData["event_minutes_item_ref"]);
24-
}
18+
if (
19+
typeof jsonData["event_minutes_item_ref"] === "object" &&
20+
!(jsonData["event_minutes_item_ref"] instanceof DocumentReference)
21+
) {
22+
this.event_minutes_item = new EventMinutesItem(jsonData["event_minutes_item_ref"]);
2523
}
2624

2725
if (jsonData["external_source_id"]) {
2826
this.external_source_id = jsonData["external_source_id"];
2927
}
3028

31-
if (jsonData["name"]) {
32-
this.name = jsonData["name"];
33-
}
29+
this.name = jsonData["name"];
3430

35-
if (jsonData["uri"]) {
36-
this.uri = jsonData["uri"];
37-
}
31+
this.uri = jsonData["uri"];
3832
}
3933
}

src/models/File.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,18 @@ import { ResponseData } from "../networking/NetworkResponse";
22
import { Model } from "./Model";
33

44
class File implements Model {
5-
id?: string;
6-
uri?: string;
7-
name?: string;
5+
id: string;
6+
uri: string;
7+
name: string;
88
description?: string;
99
media_type?: string;
1010

1111
constructor(jsonData: ResponseData) {
12-
if (jsonData["id"]) {
13-
this.id = jsonData["id"];
14-
}
12+
this.id = jsonData["id"];
1513

16-
if (jsonData["uri"]) {
17-
this.uri = jsonData["uri"];
18-
}
19-
if (jsonData["name"]) {
20-
this.name = jsonData["name"];
21-
}
14+
this.uri = jsonData["uri"];
15+
16+
this.name = jsonData["name"];
2217

2318
if (jsonData["description"]) {
2419
this.description = jsonData["description"];

src/models/IndexedEventGram.ts

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,31 @@
11
import { ResponseData } from "../networking/NetworkResponse";
22
import Event from "./Event";
33
import { Model } from "./Model";
4-
import { DocumentReference } from "@firebase/firestore";
5-
4+
import { DocumentReference } from "firebase/firestore";
65
export default class IndexedEventGram implements Model {
7-
id?: string;
8-
context_span?: string;
9-
datetime_weighted_value?: number;
10-
event_ref?: string;
6+
id: string;
7+
context_span: string;
8+
datetime_weighted_value: number;
9+
event_ref: string;
1110
event?: Event;
12-
stemmed_gram?: string;
13-
unstemmed_gram?: string;
14-
value?: number;
11+
stemmed_gram: string;
12+
unstemmed_gram: string;
13+
value: number;
1514

1615
constructor(jsonData: ResponseData) {
17-
if (jsonData["id"]) {
18-
this.id = jsonData["id"];
19-
}
20-
21-
if (jsonData["context_span"]) {
22-
this.context_span = jsonData["context_span"];
23-
}
24-
25-
if (jsonData["datetime_weighted_value"]) {
26-
this.datetime_weighted_value = jsonData["datetime_weighted_value"];
27-
}
28-
29-
if (jsonData["event_ref"]) {
30-
if (jsonData["event_ref"] instanceof DocumentReference) {
31-
this.event_ref = jsonData["event_ref"].id;
32-
} else if (typeof jsonData["event_ref"] === "object") {
33-
this.event = new Event(jsonData["event_ref"]);
34-
}
35-
}
36-
37-
if (jsonData["stemmed_gram"]) {
38-
this.stemmed_gram = jsonData["stemmed_gram"];
39-
}
40-
41-
if (jsonData["unstemmed_gram"]) {
42-
this.unstemmed_gram = jsonData["unstemmed_gram"];
43-
}
44-
45-
if (jsonData["value"]) {
46-
this.value = jsonData["value"];
16+
this.id = jsonData["id"];
17+
this.context_span = jsonData["context_span"];
18+
this.datetime_weighted_value = jsonData["datetime_weighted_value"];
19+
this.event_ref = jsonData["event_ref"].id;
20+
this.stemmed_gram = jsonData["stemmed_gram"];
21+
this.unstemmed_gram = jsonData["unstemmed_gram"];
22+
this.value = jsonData["value"];
23+
24+
if (
25+
typeof jsonData["event_ref"] === "object" &&
26+
!(jsonData["event_ref"] instanceof DocumentReference)
27+
) {
28+
this.event = new Event(jsonData["event_ref"]);
4729
}
4830
}
4931
}

src/models/Matter.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
import { ResponseData } from "../networking/NetworkResponse";
22
import { Model } from "./Model";
3-
43
export default class Matter implements Model {
5-
id?: string;
4+
id: string;
65
external_source_id?: string;
7-
matter_type?: string;
8-
name?: string;
9-
title?: string;
6+
matter_type: string;
7+
name: string;
8+
title: string;
109

1110
constructor(jsonData: ResponseData) {
12-
if (jsonData["id"]) {
13-
this.id = jsonData["id"];
14-
}
11+
this.id = jsonData["id"];
12+
this.matter_type = jsonData["matter_type"];
13+
this.name = jsonData["name"];
14+
this.title = jsonData["title"];
1515

1616
if (jsonData["external_source_id"]) {
1717
this.external_source_id = jsonData["external_source_id"];
1818
}
19-
20-
if (jsonData["matter_type"]) {
21-
this.matter_type = jsonData["matter_type"];
22-
}
23-
24-
if (jsonData["name"]) {
25-
this.name = jsonData["name"];
26-
}
27-
28-
if (jsonData["title"]) {
29-
this.title = jsonData["title"];
30-
}
3119
}
3220
}

0 commit comments

Comments
 (0)