Created
January 28, 2022 03:15
-
-
Save micaiahwallace/2b635341b90d193f28e7232919fcafcd to your computer and use it in GitHub Desktop.
validateBlogPost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BlogPost } from '../types' | |
export const getValidatedBlogPost = (post: any): BlogPost | undefined => { | |
if (typeof post !== 'object') return undefined | |
if (typeof post.title !== 'string') return undefined | |
if (typeof post.time !== 'string') return undefined | |
if (typeof post.summary !== 'string') return undefined | |
return { | |
title: post.title, | |
time: post.time, | |
summary: post.summary, | |
} | |
} | |
export const getValidatedBlogPosts = (posts: any): BlogPost[] => { | |
if (!Array.isArray(posts)) return [] | |
return posts | |
.map((post) => getValidatedBlogPost(post)) | |
.filter(Boolean) as BlogPost[] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment