From 457e8b6422704ba23347c766a8bb9c101c2aba0b Mon Sep 17 00:00:00 2001 From: William Killerud Date: Wed, 24 Jan 2024 06:37:06 +0100 Subject: [PATCH] fix(rss): apply refinement at the point of parsing (#9797) --- .changeset/angry-dryers-hang.md | 5 +++ packages/astro-rss/src/index.ts | 7 +++- packages/astro-rss/src/schema.ts | 51 +++++++++++++---------------- packages/astro-rss/test/rss.test.js | 13 ++++++++ 4 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 .changeset/angry-dryers-hang.md diff --git a/.changeset/angry-dryers-hang.md b/.changeset/angry-dryers-hang.md new file mode 100644 index 000000000000..7c1e4c3f44eb --- /dev/null +++ b/.changeset/angry-dryers-hang.md @@ -0,0 +1,5 @@ +--- +"@astrojs/rss": patch +--- + +Restores `rssSchema` to a zod object diff --git a/packages/astro-rss/src/index.ts b/packages/astro-rss/src/index.ts index b866365ab342..b3e0253d2ce7 100644 --- a/packages/astro-rss/src/index.ts +++ b/packages/astro-rss/src/index.ts @@ -137,7 +137,12 @@ export function pagesGlobToRssItems(items: GlobResult): Promise val.title || val.description, { + message: 'At least title or description must be provided.', + path: ['title', 'description'], + }) + .safeParse({ ...frontmatter, link: url }, { errorMap }); if (parsedResult.success) { return parsedResult.data; diff --git a/packages/astro-rss/src/schema.ts b/packages/astro-rss/src/schema.ts index a3604bdebfdb..79d2e5987636 100644 --- a/packages/astro-rss/src/schema.ts +++ b/packages/astro-rss/src/schema.ts @@ -1,30 +1,25 @@ import { z } from 'astro/zod'; -export const rssSchema = z - .object({ - title: z.string().optional(), - description: z.string().optional(), - pubDate: z - .union([z.string(), z.number(), z.date()]) - .optional() - .transform((value) => (value === undefined ? value : new Date(value))) - .refine((value) => (value === undefined ? value : !isNaN(value.getTime()))), - customData: z.string().optional(), - categories: z.array(z.string()).optional(), - author: z.string().optional(), - commentsUrl: z.string().optional(), - source: z.object({ url: z.string().url(), title: z.string() }).optional(), - enclosure: z - .object({ - url: z.string(), - length: z.number().positive().int().finite(), - type: z.string(), - }) - .optional(), - link: z.string().optional(), - content: z.string().optional(), - }) - .refine((val) => val.title || val.description, { - message: 'At least title or description must be provided.', - path: ['title', 'description'], - }); +export const rssSchema = z.object({ + title: z.string().optional(), + description: z.string().optional(), + pubDate: z + .union([z.string(), z.number(), z.date()]) + .optional() + .transform((value) => (value === undefined ? value : new Date(value))) + .refine((value) => (value === undefined ? value : !isNaN(value.getTime()))), + customData: z.string().optional(), + categories: z.array(z.string()).optional(), + author: z.string().optional(), + commentsUrl: z.string().optional(), + source: z.object({ url: z.string().url(), title: z.string() }).optional(), + enclosure: z + .object({ + url: z.string(), + length: z.number().positive().int().finite(), + type: z.string(), + }) + .optional(), + link: z.string().optional(), + content: z.string().optional(), +}); diff --git a/packages/astro-rss/test/rss.test.js b/packages/astro-rss/test/rss.test.js index 3f37e88a5699..833abf91c9b7 100644 --- a/packages/astro-rss/test/rss.test.js +++ b/packages/astro-rss/test/rss.test.js @@ -1,6 +1,7 @@ import chai from 'chai'; import chaiPromises from 'chai-as-promised'; import chaiXml from 'chai-xml'; +import { z } from 'astro/zod'; import rss, { getRssString } from '../dist/index.js'; import { rssSchema } from '../dist/schema.js'; import { @@ -214,4 +215,16 @@ describe('getRssString', () => { chai.expect(res.success).to.be.false; chai.expect(res.error.issues[0].path[0]).to.equal('pubDate'); }); + + it('should be extendable', () => { + let error = null; + try { + rssSchema.extend({ + category: z.string().optional(), + }); + } catch (e) { + error = e.message; + } + chai.expect(error).to.be.null; + }); });