Skip to content

feat: add contentSchema #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2024
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: 2 additions & 2 deletions docs/demo/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -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 };
4 changes: 2 additions & 2 deletions packages/template/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -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 };
56 changes: 56 additions & 0 deletions packages/types/src/schemas/content.spec.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
13 changes: 13 additions & 0 deletions packages/types/src/schemas/content.ts
Original file line number Diff line number Diff line change
@@ -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<typeof contentSchema>;
1 change: 1 addition & 0 deletions packages/types/src/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -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';