Skip to content

Commit bc0fde2

Browse files
authored
feat: add contentSchema (#156)
1 parent ce4d078 commit bc0fde2

File tree

5 files changed

+74
-4
lines changed

5 files changed

+74
-4
lines changed

docs/demo/src/content/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { chapterSchema, lessonSchema, partSchema, tutorialSchema } from '@tutorialkit/types';
1+
import { contentSchema } from '@tutorialkit/types';
22
import { defineCollection } from 'astro:content';
33

44
const tutorial = defineCollection({
55
type: 'content',
6-
schema: tutorialSchema.strict().or(partSchema.strict()).or(chapterSchema.strict()).or(lessonSchema.strict()),
6+
schema: contentSchema,
77
});
88

99
export const collections = { tutorial };
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { chapterSchema, lessonSchema, partSchema, tutorialSchema } from '@tutorialkit/types';
1+
import { contentSchema } from '@tutorialkit/types';
22
import { defineCollection } from 'astro:content';
33

44
const tutorial = defineCollection({
55
type: 'content',
6-
schema: tutorialSchema.strict().or(partSchema.strict()).or(chapterSchema.strict()).or(lessonSchema.strict()),
6+
schema: contentSchema,
77
});
88

99
export const collections = { tutorial };
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, it, expect } from 'vitest';
2+
import { contentSchema } from './content.js';
3+
4+
describe('contentSchema', () => {
5+
it('should validate a valid part', () => {
6+
expect(() =>
7+
contentSchema.parse({
8+
title: 'Part 1',
9+
type: 'part',
10+
slug: 'part-one',
11+
}),
12+
).not.toThrow();
13+
});
14+
15+
it('should validate an invalid part', () => {
16+
expect(() =>
17+
contentSchema.parse({
18+
type: 'part',
19+
}),
20+
).toThrow();
21+
});
22+
23+
it('should validate a valid chapter', () => {
24+
expect(() =>
25+
contentSchema.parse({
26+
type: 'chapter',
27+
title: 'Chapter 1',
28+
}),
29+
).not.toThrow();
30+
});
31+
32+
it('should validate an invalid chapter', () => {
33+
expect(() =>
34+
contentSchema.parse({
35+
type: 'chapter',
36+
}),
37+
).toThrow();
38+
});
39+
40+
it('should validate a valid lesson', () => {
41+
expect(() =>
42+
contentSchema.parse({
43+
type: 'lesson',
44+
title: 'Lesson 1',
45+
}),
46+
).not.toThrow();
47+
});
48+
49+
it('should validate an invalid lesson', () => {
50+
expect(() =>
51+
contentSchema.parse({
52+
type: 'lesson',
53+
}),
54+
).toThrow();
55+
});
56+
});

packages/types/src/schemas/content.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { z } from 'zod';
2+
import { chapterSchema } from './chapter.js';
3+
import { lessonSchema } from './lesson.js';
4+
import { partSchema } from './part.js';
5+
import { tutorialSchema } from './tutorial.js';
6+
7+
export const contentSchema = tutorialSchema
8+
.strict()
9+
.or(partSchema.strict())
10+
.or(chapterSchema.strict())
11+
.or(lessonSchema.strict());
12+
13+
export type ContentSchema = z.infer<typeof contentSchema>;

packages/types/src/schemas/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './chapter.js';
22
export * from './common.js';
3+
export * from './content.js';
34
export * from './lesson.js';
45
export * from './part.js';
56
export * from './tutorial.js';

0 commit comments

Comments
 (0)