Skip to content

Commit 457e8b6

Browse files
authored
fix(rss): apply refinement at the point of parsing (#9797)
1 parent c1e0268 commit 457e8b6

4 files changed

Lines changed: 47 additions & 29 deletions

File tree

.changeset/angry-dryers-hang.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/rss": patch
3+
---
4+
5+
Restores `rssSchema` to a zod object

packages/astro-rss/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,12 @@ export function pagesGlobToRssItems(items: GlobResult): Promise<ValidatedRSSFeed
137137
`[RSS] You can only glob entries within 'src/pages/' when passing import.meta.glob() directly. Consider mapping the result to an array of RSSFeedItems. See the RSS docs for usage examples: https://docs.astro.build/en/guides/rss/#2-list-of-rss-feed-objects`
138138
);
139139
}
140-
const parsedResult = rssSchema.safeParse({ ...frontmatter, link: url }, { errorMap });
140+
const parsedResult = rssSchema
141+
.refine((val) => val.title || val.description, {
142+
message: 'At least title or description must be provided.',
143+
path: ['title', 'description'],
144+
})
145+
.safeParse({ ...frontmatter, link: url }, { errorMap });
141146

142147
if (parsedResult.success) {
143148
return parsedResult.data;

packages/astro-rss/src/schema.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
import { z } from 'astro/zod';
22

3-
export const rssSchema = z
4-
.object({
5-
title: z.string().optional(),
6-
description: z.string().optional(),
7-
pubDate: z
8-
.union([z.string(), z.number(), z.date()])
9-
.optional()
10-
.transform((value) => (value === undefined ? value : new Date(value)))
11-
.refine((value) => (value === undefined ? value : !isNaN(value.getTime()))),
12-
customData: z.string().optional(),
13-
categories: z.array(z.string()).optional(),
14-
author: z.string().optional(),
15-
commentsUrl: z.string().optional(),
16-
source: z.object({ url: z.string().url(), title: z.string() }).optional(),
17-
enclosure: z
18-
.object({
19-
url: z.string(),
20-
length: z.number().positive().int().finite(),
21-
type: z.string(),
22-
})
23-
.optional(),
24-
link: z.string().optional(),
25-
content: z.string().optional(),
26-
})
27-
.refine((val) => val.title || val.description, {
28-
message: 'At least title or description must be provided.',
29-
path: ['title', 'description'],
30-
});
3+
export const rssSchema = z.object({
4+
title: z.string().optional(),
5+
description: z.string().optional(),
6+
pubDate: z
7+
.union([z.string(), z.number(), z.date()])
8+
.optional()
9+
.transform((value) => (value === undefined ? value : new Date(value)))
10+
.refine((value) => (value === undefined ? value : !isNaN(value.getTime()))),
11+
customData: z.string().optional(),
12+
categories: z.array(z.string()).optional(),
13+
author: z.string().optional(),
14+
commentsUrl: z.string().optional(),
15+
source: z.object({ url: z.string().url(), title: z.string() }).optional(),
16+
enclosure: z
17+
.object({
18+
url: z.string(),
19+
length: z.number().positive().int().finite(),
20+
type: z.string(),
21+
})
22+
.optional(),
23+
link: z.string().optional(),
24+
content: z.string().optional(),
25+
});

packages/astro-rss/test/rss.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chai from 'chai';
22
import chaiPromises from 'chai-as-promised';
33
import chaiXml from 'chai-xml';
4+
import { z } from 'astro/zod';
45
import rss, { getRssString } from '../dist/index.js';
56
import { rssSchema } from '../dist/schema.js';
67
import {
@@ -214,4 +215,16 @@ describe('getRssString', () => {
214215
chai.expect(res.success).to.be.false;
215216
chai.expect(res.error.issues[0].path[0]).to.equal('pubDate');
216217
});
218+
219+
it('should be extendable', () => {
220+
let error = null;
221+
try {
222+
rssSchema.extend({
223+
category: z.string().optional(),
224+
});
225+
} catch (e) {
226+
error = e.message;
227+
}
228+
chai.expect(error).to.be.null;
229+
});
217230
});

0 commit comments

Comments
 (0)