RSS: Fix string validation of pubDate (#7066)

This commit is contained in:
Reuben Tier 2023-05-15 07:10:32 +01:00 committed by GitHub
parent d68e736545
commit a37e67b520
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---
Fix pubDate schema tranformation

View file

@ -2,7 +2,7 @@ import { z } from 'astro/zod';
export const rssSchema = z.object({ export const rssSchema = z.object({
title: z.string(), 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(), description: z.string().optional(),
customData: z.string().optional(), customData: z.string().optional(),
draft: z.boolean().optional(), draft: z.boolean().optional(),

View file

@ -1,4 +1,5 @@
import rss from '../dist/index.js'; import rss from '../dist/index.js';
import { rssSchema } from '../dist/schema.js';
import chai from 'chai'; import chai from 'chai';
import chaiPromises from 'chai-as-promised'; import chaiPromises from 'chai-as-promised';
import chaiXml from 'chai-xml'; import chaiXml from 'chai-xml';
@ -195,4 +196,16 @@ describe('rss', () => {
chai.expect(body).xml.to.equal(validXmlResult); 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');
});
}); });