feat: 新增 news 内容集合与 Zod schema(含单测)
This commit is contained in:
Generated
+349
-1249
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -14,6 +14,6 @@
|
||||
"zod": "^3.25.76"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vitest": "^4.1.10"
|
||||
"vitest": "^3.2.7"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { defineCollection } from 'astro:content';
|
||||
import { glob } from 'astro/loaders';
|
||||
import { newsSchema } from './content/schemas';
|
||||
|
||||
const news = defineCollection({
|
||||
loader: glob({ pattern: '**/*.md', base: './src/content/news' }),
|
||||
schema: newsSchema,
|
||||
});
|
||||
|
||||
export const collections = { news };
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
title: 官网基座启动
|
||||
date: 2026-07-23
|
||||
category: 公告
|
||||
---
|
||||
|
||||
这是 Phase 0 技术验证的第一条新闻。内容编辑链路跑通后,客户可在 Sveltia CMS 自助新增。
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const newsSchema = z.object({
|
||||
title: z.string(),
|
||||
date: z.coerce.date(),
|
||||
category: z.string().default('未分类'),
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { newsSchema } from '../src/content/schemas';
|
||||
|
||||
describe('newsSchema', () => {
|
||||
it('accepts valid frontmatter and coerces date', () => {
|
||||
const parsed = newsSchema.parse({
|
||||
title: '官网基座启动',
|
||||
date: '2026-07-23',
|
||||
category: '公告',
|
||||
});
|
||||
expect(parsed.title).toBe('官网基座启动');
|
||||
expect(parsed.date).toEqual(new Date('2026-07-23'));
|
||||
expect(parsed.category).toBe('公告');
|
||||
});
|
||||
|
||||
it('defaults category to 未分类', () => {
|
||||
const parsed = newsSchema.parse({ title: '测试', date: '2026-07-23' });
|
||||
expect(parsed.category).toBe('未分类');
|
||||
});
|
||||
|
||||
it('rejects missing title', () => {
|
||||
expect(() => newsSchema.parse({ date: '2026-07-23' })).toThrow();
|
||||
});
|
||||
|
||||
it('rejects missing date', () => {
|
||||
expect(() => newsSchema.parse({ title: '测试' })).toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user