From a37e67b520dc35dbf40313c77490a97446de2f74 Mon Sep 17 00:00:00 2001 From: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> Date: Mon, 15 May 2023 07:10:32 +0100 Subject: [PATCH] RSS: Fix string validation of pubDate (#7066) --- .changeset/eleven-hotels-roll.md | 5 +++++ packages/astro-rss/src/schema.ts | 2 +- packages/astro-rss/test/rss.test.js | 13 +++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .changeset/eleven-hotels-roll.md diff --git a/.changeset/eleven-hotels-roll.md b/.changeset/eleven-hotels-roll.md new file mode 100644 index 0000000000..dc4eb3df8e --- /dev/null +++ b/.changeset/eleven-hotels-roll.md @@ -0,0 +1,5 @@ +--- +'@astrojs/rss': patch +--- + +Fix pubDate schema tranformation diff --git a/packages/astro-rss/src/schema.ts b/packages/astro-rss/src/schema.ts index 829a4da1e4..ede4e764cf 100644 --- a/packages/astro-rss/src/schema.ts +++ b/packages/astro-rss/src/schema.ts @@ -2,7 +2,7 @@ import { z } from 'astro/zod'; export const rssSchema = z.object({ title: z.string(), - pubDate: z.union([z.string(), z.number(), z.date()]).transform((value) => new Date(value)), + pubDate: z.union([z.string(), z.number(), z.date()]).transform((value) => new Date(value)).refine((value) => !isNaN(value.getTime())), description: z.string().optional(), customData: z.string().optional(), draft: z.boolean().optional(), diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 2a7106c076..238408d44c 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -1,4 +1,5 @@ import rss from '../dist/index.js'; +import { rssSchema } from '../dist/schema.js'; import chai from 'chai'; import chaiPromises from 'chai-as-promised'; import chaiXml from 'chai-xml'; @@ -195,4 +196,16 @@ describe('rss', () => { chai.expect(body).xml.to.equal(validXmlResult); }); + + it('should fail when an invalid date string is provided', async () => { + const res = rssSchema.safeParse({ + title: phpFeedItem.title, + pubDate: 'invalid date', + description: phpFeedItem.description, + link: phpFeedItem.link, + }) + + chai.expect(res.success).to.be.false; + chai.expect(res.error.issues[0].path[0]).to.equal('pubDate'); + }); });