Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rss): apply refinement at the point of parsing #9797

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-dryers-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/rss": patch
---

Restores `rssSchema` to a zod object
7 changes: 6 additions & 1 deletion packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ export function pagesGlobToRssItems(items: GlobResult): Promise<ValidatedRSSFeed
`[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`
);
}
const parsedResult = rssSchema.safeParse({ ...frontmatter, link: url }, { errorMap });
const parsedResult = rssSchema
.refine((val) => 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;
Expand Down
51 changes: 23 additions & 28 deletions packages/astro-rss/src/schema.ts
Original file line number Diff line number Diff line change
@@ -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(),
});
13 changes: 13 additions & 0 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
});
});