mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
fix(rss): apply refinement at the point of parsing (#9797)
This commit is contained in:
parent
c1e02688e7
commit
457e8b6422
4 changed files with 47 additions and 29 deletions
5
.changeset/angry-dryers-hang.md
Normal file
5
.changeset/angry-dryers-hang.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"@astrojs/rss": patch
|
||||
---
|
||||
|
||||
Restores `rssSchema` to a zod object
|
|
@ -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;
|
||||
|
|
|
@ -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(),
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue