Skip to content

Instantly share code, notes, and snippets.

@micaiahwallace
Created January 28, 2022 03:15
Show Gist options
  • Save micaiahwallace/2b635341b90d193f28e7232919fcafcd to your computer and use it in GitHub Desktop.
Save micaiahwallace/2b635341b90d193f28e7232919fcafcd to your computer and use it in GitHub Desktop.
validateBlogPost.ts
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