Skip to content

Commit

Permalink
fix(rss): apply refinement at the point of parsing
Browse files Browse the repository at this point in the history
Allows extending, while keeping the validation.
  • Loading branch information
wkillerud committed Jan 23, 2024
1 parent 5994a90 commit 2aa8941
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
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;
});
});

0 comments on commit 2aa8941

Please sign in to comment.