Skip to content
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
4 changes: 3 additions & 1 deletion README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ const xml = toAtom({ options, items }, { baseUrl: 'https://example.com' })

## リント

`validateInput` が検証するのは、ドキュメントを壊すものだけです (タイトルがない、Atom の author がない、など)。`lintInput` はさらに踏み込み、仕様上は省略可能でも、実際のリーダーやディレクトリが期待している項目を指摘します。たとえばフィードの `description` / `language` / `image` がない、記事に `id`、`author`、日付がない、`podcast` を設定したのにポッドキャストのメタデータが不完全、などです。例外は投げず、警告の `string[]` を返します (問題がなければ空)。`serveFeed` はこれを呼ばないので、テストや CI 向けの機能で、配信の経路では使いません。
`validateInput` が検証するのは、ドキュメントを壊すものだけです (タイトルがない、Atom の author がない、など)。`lintInput` はさらに踏み込み、仕様上は省略可能でも、実際のリーダーやディレクトリが期待している項目を指摘します。たとえばフィードの `description` / `language` / `image` がない、記事に `id`、`author`、日付がない、`podcast` を設定したのにポッドキャストのメタデータが不完全、などです。よくあるミスも検出します。記事の `id` の重複、未来の日付 (多くはタイムゾーンの取り違え)、`content` / `description` に残った相対 URL (リーダーが解決できません)、`type` や `length` がない enclosure、長すぎる、または HTML が混ざったタイトル、100 件を超えるのに `paging` がないフィード、などです。例外は投げず、警告の `string[]` を返します (問題がなければ空)。`serveFeed` はこれを呼ばないので、テストや CI 向けの機能で、配信の経路では使いません。

```ts
import { lintInput } from 'hono-feed'
Expand All @@ -751,6 +751,8 @@ const warnings = lintInput({ options, items }, 'rss')
// e.g. expect(lintInput({ options, items }, 'rss')).toEqual([]) in a CI check
```

すべてのルールに ID (`LintRuleId`) があり、許容するルールだけ無効にできます。たとえば `lintInput(input, 'rss', { ignore: ['feed-language', 'item-count'] })` のように指定します。`future-date` ルールは現在時刻と比較するので、テストを決定的にしたい場合は `now` を渡してください。

## コントリビュート

コントリビュートを歓迎します。次のような形で参加できます。
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ const xml = toAtom({ options, items }, { baseUrl: 'https://example.com' })

## Linting

`validateInput` only enforces what breaks the document (a missing title, a missing Atom author, …). `lintInput` goes further: it flags fields that are optional per spec but expected by real readers and directories — a missing feed `description`/`language`/`image`, an item with no `id`, `author`, or date, incomplete Podcast metadata once `podcast` is set, and so on. It never throws and returns a plain `string[]` of warnings (empty when clean), and `serveFeed` never calls it — it's for tests and CI, not the serve path:
`validateInput` only enforces what breaks the document (a missing title, a missing Atom author, …). `lintInput` goes further: it flags fields that are optional per spec but expected by real readers and directories — a missing feed `description`/`language`/`image`, an item with no `id`, `author`, or date, incomplete Podcast metadata once `podcast` is set, and so on. It also catches common mistakes: duplicate item ids, a date in the future (usually a timezone mix-up), relative URLs left in `content`/`description` (readers can't resolve them), an enclosure with no `type` or `length`, an overlong or HTML-laden title, and a feed of more than 100 items with no `paging`. It never throws and returns a plain `string[]` of warnings (empty when clean), and `serveFeed` never calls it — it's for tests and CI, not the serve path:

```ts
import { lintInput } from 'hono-feed'
Expand All @@ -751,6 +751,8 @@ const warnings = lintInput({ options, items }, 'rss')
// e.g. expect(lintInput({ options, items }, 'rss')).toEqual([]) in a CI check
```

Every rule has an id (`LintRuleId`), so you can silence the ones you accept: `lintInput(input, 'rss', { ignore: ['feed-language', 'item-count'] })`. The `future-date` rule compares against the current time; pass `now` for a deterministic test.

## Contributing

Contributions Welcome! You can contribute in the following ways.
Expand Down
11 changes: 9 additions & 2 deletions base/README.base.md
Original file line number Diff line number Diff line change
Expand Up @@ -1093,12 +1093,12 @@ const xml = toAtom({ options, items }, { baseUrl: 'https://example.com' })
:::kiritan{locale=en}
## Linting

`validateInput` only enforces what breaks the document (a missing title, a missing Atom author, …). `lintInput` goes further: it flags fields that are optional per spec but expected by real readers and directories — a missing feed `description`/`language`/`image`, an item with no `id`, `author`, or date, incomplete Podcast metadata once `podcast` is set, and so on. It never throws and returns a plain `string[]` of warnings (empty when clean), and `serveFeed` never calls it — it's for tests and CI, not the serve path:
`validateInput` only enforces what breaks the document (a missing title, a missing Atom author, …). `lintInput` goes further: it flags fields that are optional per spec but expected by real readers and directories — a missing feed `description`/`language`/`image`, an item with no `id`, `author`, or date, incomplete Podcast metadata once `podcast` is set, and so on. It also catches common mistakes: duplicate item ids, a date in the future (usually a timezone mix-up), relative URLs left in `content`/`description` (readers can't resolve them), an enclosure with no `type` or `length`, an overlong or HTML-laden title, and a feed of more than 100 items with no `paging`. It never throws and returns a plain `string[]` of warnings (empty when clean), and `serveFeed` never calls it — it's for tests and CI, not the serve path:
:::
:::kiritan{locale=ja}
## リント

`validateInput` が検証するのは、ドキュメントを壊すものだけです (タイトルがない、Atom の author がない、など)。`lintInput` はさらに踏み込み、仕様上は省略可能でも、実際のリーダーやディレクトリが期待している項目を指摘します。たとえばフィードの `description` / `language` / `image` がない、記事に `id`、`author`、日付がない、`podcast` を設定したのにポッドキャストのメタデータが不完全、などです。例外は投げず、警告の `string[]` を返します (問題がなければ空)。`serveFeed` はこれを呼ばないので、テストや CI 向けの機能で、配信の経路では使いません。
`validateInput` が検証するのは、ドキュメントを壊すものだけです (タイトルがない、Atom の author がない、など)。`lintInput` はさらに踏み込み、仕様上は省略可能でも、実際のリーダーやディレクトリが期待している項目を指摘します。たとえばフィードの `description` / `language` / `image` がない、記事に `id`、`author`、日付がない、`podcast` を設定したのにポッドキャストのメタデータが不完全、などです。よくあるミスも検出します。記事の `id` の重複、未来の日付 (多くはタイムゾーンの取り違え)、`content` / `description` に残った相対 URL (リーダーが解決できません)、`type` や `length` がない enclosure、長すぎる、または HTML が混ざったタイトル、100 件を超えるのに `paging` がないフィード、などです。例外は投げず、警告の `string[]` を返します (問題がなければ空)。`serveFeed` はこれを呼ばないので、テストや CI 向けの機能で、配信の経路では使いません。
:::

```ts
Expand All @@ -1108,6 +1108,13 @@ const warnings = lintInput({ options, items }, 'rss')
// e.g. expect(lintInput({ options, items }, 'rss')).toEqual([]) in a CI check
```

:::kiritan{locale=en}
Every rule has an id (`LintRuleId`), so you can silence the ones you accept: `lintInput(input, 'rss', { ignore: ['feed-language', 'item-count'] })`. The `future-date` rule compares against the current time; pass `now` for a deterministic test.
:::
:::kiritan{locale=ja}
すべてのルールに ID (`LintRuleId`) があり、許容するルールだけ無効にできます。たとえば `lintInput(input, 'rss', { ignore: ['feed-language', 'item-count'] })` のように指定します。`future-date` ルールは現在時刻と比較するので、テストを決定的にしたい場合は `now` を渡してください。
:::

:::kiritan{locale=en}
## Contributing

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { Feed } from './feed'
export { toAtom, toJSONFeed, toRSS } from './formats'
export type { LintOptions, LintRuleId } from './lint'
export { lintInput } from './lint'
export type { FeedMiddlewareEnv, ServeFeedFn } from './middleware'
export { feed as feedMiddleware } from './middleware'
Expand Down
94 changes: 94 additions & 0 deletions src/lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,97 @@ describe('lintInput', () => {
expect(() => lintInput(empty, 'atom')).not.toThrow()
})
})

describe('lintInput rules', () => {
const withItem = (patch: Partial<FeedInput['items'][number]>): FeedInput => ({
...clean,
items: [{ ...clean.items[0], ...patch }],
})
const now = new Date('2026-07-01T00:00:00Z')

it('flags items that share an id (or fall back to the same link)', () => {
const dup: FeedInput = { ...clean, items: [clean.items[0], { ...clean.items[0] }] }
expect(lintInput(dup, 'rss')).toEqual([expect.stringMatching(/item\[1\].*item\[0\]/)])
const byLink: FeedInput = {
...clean,
items: [
{ ...clean.items[0], id: undefined },
{ ...clean.items[0], id: undefined },
],
}
expect(lintInput(byLink, 'rss', { ignore: ['item-id'] })).toEqual([
expect.stringMatching(/same id\/link/),
])
})

it('flags a date more than a day in the future, but not a small skew', () => {
const future = withItem({ published: new Date('2026-07-03T00:00:00Z') })
expect(lintInput(future, 'rss', { now })).toEqual([
expect.stringMatching(/"published" is in the future/),
])
expect(
lintInput(withItem({ updated: new Date('2026-07-01T05:00:00Z') }), 'rss', { now }),
).toEqual([])
})

it('flags relative URLs in content and description, not absolute, fragment or mailto ones', () => {
const relative = withItem({ content: '<img src="/a.png"><a href="post">x</a>' })
expect(lintInput(relative, 'atom')).toEqual([
expect.stringMatching(/"content" has 2 relative URL\(s\), e\.g\. "\/a\.png"/),
])
expect(lintInput(withItem({ description: "<a href='x/y'>" }), 'rss')).toEqual([
expect.stringMatching(/"description"/),
])
const fine = withItem({
content:
'<a href="https://a.test/">a</a><a href="//a.test">b</a><a href="#top">c</a><a href="mailto:a@b.c">d</a>',
})
expect(lintInput(fine, 'atom')).toEqual([])
})

it('flags an enclosure with no type or a zero length', () => {
const noLength = withItem({ enclosure: { url: 'https://a.test/a.mp3', type: 'audio/mpeg' } })
expect(lintInput(noLength, 'rss')).toEqual([expect.stringMatching(/no "length"/)])
const noType = withItem({ enclosure: { url: 'https://a.test/a.mp3', type: '', length: 5 } })
expect(lintInput(noType, 'rss')).toEqual([expect.stringMatching(/no "type"/)])
const ok = withItem({
enclosure: { url: 'https://a.test/a.mp3', type: 'audio/mpeg', length: 5 },
})
expect(lintInput(ok, 'rss')).toEqual([])
})

it('flags an overlong title and a title containing HTML', () => {
expect(lintInput(withItem({ title: 'x'.repeat(151) }), 'rss')).toEqual([
expect.stringMatching(/151 characters/),
])
expect(lintInput(withItem({ title: 'a <b>bold</b> move' }), 'rss')).toEqual([
expect.stringMatching(/HTML markup/),
])
expect(lintInput(withItem({ title: '1 < 2 and 3 > 2' }), 'rss')).toEqual([])
})

it('suggests paging for a feed over 100 items, unless paging is set', () => {
const many: FeedInput = {
...clean,
items: Array.from({ length: 101 }, (_, i) => ({
...clean.items[0],
id: `https://example.com/${i}`,
link: `https://example.com/${i}`,
})),
}
expect(lintInput(many, 'rss')).toEqual([expect.stringMatching(/101 items.*paging/)])
const paged: FeedInput = {
...many,
options: { ...many.options, paging: { next: 'https://example.com/?p=2' } },
}
expect(lintInput(paged, 'rss')).toEqual([])
})

it('skips ignored rules, including the pre-existing ones', () => {
const bare: FeedInput = { options: { title: 't' }, items: [{ title: 'a' }] }
const all = lintInput(bare, 'rss')
const filtered = lintInput(bare, 'rss', { ignore: ['feed-description', 'item-author'] })
expect(all.length - filtered.length).toBe(2)
expect(filtered.join('\n')).not.toMatch(/"description"|"author"/)
})
})
Loading
Loading