Skip to content

Commit 0ac0406

Browse files
committed
ensure malformed entries don't break entire parsing
1 parent 3ffde40 commit 0ac0406

File tree

6 files changed

+441
-421
lines changed

6 files changed

+441
-421
lines changed

src/components/Projects.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as z from "zod/mini";
2-
3-
import { Project, ProjectSchema } from "../types";
1+
import { Project, ProjectSchema, gracefulParse } from "../types";
42
import projectJSON from "../content/text/projects.json";
53

64
const ProjectComponent = ({ projectData }: { projectData: Project }) => {
@@ -28,7 +26,7 @@ const ProjectComponent = ({ projectData }: { projectData: Project }) => {
2826
};
2927

3028
export const Projects = () => {
31-
const projects: Project[] = z.array(ProjectSchema).parse(projectJSON);
29+
const projects: Project[] = gracefulParse(ProjectSchema, projectJSON);
3230

3331
return (
3432
<div className="projects-grid">

src/components/Publications.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as z from "zod/mini";
2-
3-
import { Publication, PublicationSchema } from "../types";
1+
import { Publication, PublicationSchema, gracefulParse } from "../types";
42

53
// this imports the json from `content` such that we can use it as a variable
64
import publicationsJSON from "../content/text/publications.json";
@@ -45,7 +43,7 @@ const PublicationComponent = ({ publicationData }: { publicationData: Publicatio
4543
// a simple unorderd list of publications which links to the paper with minor styling
4644
export const Publications = () => {
4745
// zod is library for parsing json data according to schema (think type / class in other languages). We parse data
48-
const publications: Publication[] = z.array(PublicationSchema).parse(publicationsJSON);
46+
const publications: Publication[] = gracefulParse(PublicationSchema, publicationsJSON);
4947

5048
// 'map' takes each element in array and applies function to it, in our case this function returns a <li> element with publication component inside
5149
return (

src/components/Team.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as z from "zod/mini";
21
import { useState } from "react";
32

4-
import { Person, PersonSchema } from "../types";
3+
import { Person, PersonSchema, gracefulParse } from "../types";
54

65
import teamJSON from "../content/text/team.json";
76

@@ -63,7 +62,7 @@ const PersonComponent = ({ personData }: { personData: Person }) => {
6362
};
6463

6564
export const Team = () => {
66-
const people: Person[] = z.array(PersonSchema).parse(teamJSON);
65+
const people: Person[] = gracefulParse(PersonSchema, teamJSON);
6766

6867
// filter takes an array & returns new array with elements that match the boolean expression
6968
// in the first section, this is filtering for current members

src/components/Videos.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as z from "zod/mini";
2-
3-
import { Video, VideoSchema } from "../types";
1+
import { Video, VideoSchema, gracefulParse } from "../types";
42

53
import videoJSON from "../content/text/videos.json";
64

@@ -16,7 +14,7 @@ const VideoComponent = ({ videoData }: { videoData: Video }) => {
1614
};
1715

1816
export const Videos = () => {
19-
const videos: Video[] = z.array(VideoSchema).parse(videoJSON);
17+
const videos: Video[] = gracefulParse(VideoSchema, videoJSON);
2018
return (
2119
<div className="projects-grid">
2220
{videos.map((v, i) => (

0 commit comments

Comments
 (0)