Skip to content

Commit

Permalink
RSS: Fix string validation of pubDate (#7066)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOtterlord committed May 15, 2023
1 parent d68e736 commit a37e67b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eleven-hotels-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---

Fix pubDate schema tranformation
2 changes: 1 addition & 1 deletion packages/astro-rss/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
13 changes: 13 additions & 0 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
});
});

0 comments on commit a37e67b

Please sign in to comment.