Skip to content

Commit

Permalink
Validate post length and url
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-sherman committed Nov 5, 2024
1 parent 5c5d24c commit fe4ccc1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/frontpage/lib/data/atproto/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
import { z } from "zod";
import { DataLayerError } from "../error";
import { DID, getPdsUrl } from "./did";
import { MAX_POST_TITLE_LENGTH, MAX_POST_URL_LENGTH } from "../db/constants";

export const PostCollection = "fyi.unravel.frontpage.post";

export const PostRecord = z.object({
title: z.string(),
url: z.string(),
title: z.string().max(MAX_POST_TITLE_LENGTH),
url: z.string().url().max(MAX_POST_URL_LENGTH),
createdAt: z.string(),
});

Expand All @@ -25,7 +26,12 @@ type PostInput = {

export async function createPost({ title, url }: PostInput) {
const record = { title, url, createdAt: new Date().toISOString() };
PostRecord.parse(record);
const parseResult = PostRecord.safeParse(record);
if (!parseResult.success) {
throw new DataLayerError("Invalid post record", {
cause: parseResult.error,
});
}

const result = await atprotoCreateRecord({
record,
Expand Down

0 comments on commit fe4ccc1

Please sign in to comment.