Skip to content

Commit

Permalink
feat: add contentSchema (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jul 23, 2024
1 parent ce4d078 commit bc0fde2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
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';

0 comments on commit bc0fde2

Please sign in to comment.