diff --git a/docs/demo/src/content/config.ts b/docs/demo/src/content/config.ts index c28f993ae..8e595c053 100644 --- a/docs/demo/src/content/config.ts +++ b/docs/demo/src/content/config.ts @@ -1,9 +1,9 @@ -import { chapterSchema, lessonSchema, partSchema, tutorialSchema } from '@tutorialkit/types'; +import { contentSchema } from '@tutorialkit/types'; import { defineCollection } from 'astro:content'; const tutorial = defineCollection({ type: 'content', - schema: tutorialSchema.strict().or(partSchema.strict()).or(chapterSchema.strict()).or(lessonSchema.strict()), + schema: contentSchema, }); export const collections = { tutorial }; diff --git a/packages/template/src/content/config.ts b/packages/template/src/content/config.ts index c28f993ae..8e595c053 100644 --- a/packages/template/src/content/config.ts +++ b/packages/template/src/content/config.ts @@ -1,9 +1,9 @@ -import { chapterSchema, lessonSchema, partSchema, tutorialSchema } from '@tutorialkit/types'; +import { contentSchema } from '@tutorialkit/types'; import { defineCollection } from 'astro:content'; const tutorial = defineCollection({ type: 'content', - schema: tutorialSchema.strict().or(partSchema.strict()).or(chapterSchema.strict()).or(lessonSchema.strict()), + schema: contentSchema, }); export const collections = { tutorial }; diff --git a/packages/types/src/schemas/content.spec.ts b/packages/types/src/schemas/content.spec.ts new file mode 100644 index 000000000..8c723d50e --- /dev/null +++ b/packages/types/src/schemas/content.spec.ts @@ -0,0 +1,56 @@ +import { describe, it, expect } from 'vitest'; +import { contentSchema } from './content.js'; + +describe('contentSchema', () => { + it('should validate a valid part', () => { + expect(() => + contentSchema.parse({ + title: 'Part 1', + type: 'part', + slug: 'part-one', + }), + ).not.toThrow(); + }); + + it('should validate an invalid part', () => { + expect(() => + contentSchema.parse({ + type: 'part', + }), + ).toThrow(); + }); + + it('should validate a valid chapter', () => { + expect(() => + contentSchema.parse({ + type: 'chapter', + title: 'Chapter 1', + }), + ).not.toThrow(); + }); + + it('should validate an invalid chapter', () => { + expect(() => + contentSchema.parse({ + type: 'chapter', + }), + ).toThrow(); + }); + + it('should validate a valid lesson', () => { + expect(() => + contentSchema.parse({ + type: 'lesson', + title: 'Lesson 1', + }), + ).not.toThrow(); + }); + + it('should validate an invalid lesson', () => { + expect(() => + contentSchema.parse({ + type: 'lesson', + }), + ).toThrow(); + }); +}); diff --git a/packages/types/src/schemas/content.ts b/packages/types/src/schemas/content.ts new file mode 100644 index 000000000..e38ce4f73 --- /dev/null +++ b/packages/types/src/schemas/content.ts @@ -0,0 +1,13 @@ +import type { z } from 'zod'; +import { chapterSchema } from './chapter.js'; +import { lessonSchema } from './lesson.js'; +import { partSchema } from './part.js'; +import { tutorialSchema } from './tutorial.js'; + +export const contentSchema = tutorialSchema + .strict() + .or(partSchema.strict()) + .or(chapterSchema.strict()) + .or(lessonSchema.strict()); + +export type ContentSchema = z.infer; diff --git a/packages/types/src/schemas/index.ts b/packages/types/src/schemas/index.ts index 8e5ee9ff6..2ecb9a39e 100644 --- a/packages/types/src/schemas/index.ts +++ b/packages/types/src/schemas/index.ts @@ -1,5 +1,6 @@ export * from './chapter.js'; export * from './common.js'; +export * from './content.js'; export * from './lesson.js'; export * from './part.js'; export * from './tutorial.js';