Skip to content

Commit

Permalink
test: zod union type
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jan 5, 2023
1 parent 88776a8 commit 7721e5f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
29 changes: 29 additions & 0 deletions packages/astro/test/content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ describe('Content Collections', () => {
const slugs = json.withSlugConfig.map((item) => item.slug);
expect(slugs).to.deep.equal(['fancy-one.md', 'excellent-three.md', 'interesting-two.md']);
});

it('Returns `with union schema` collection', async () => {
expect(json).to.haveOwnProperty('withUnionSchema');
expect(Array.isArray(json.withUnionSchema)).to.equal(true);

const post = json.withUnionSchema.find((item) => item.id === 'post.md');
expect(post).to.not.be.undefined;
expect(post.data).to.deep.equal({
type: 'post',
title: 'My Post',
description: 'This is my post',
});
const newsletter = json.withUnionSchema.find((item) => item.id === 'newsletter.md');
expect(newsletter).to.not.be.undefined;
expect(newsletter.data).to.deep.equal({
type: 'newsletter',
subject: 'My Newsletter',
});
});
});

describe('Entry', () => {
Expand Down Expand Up @@ -130,6 +149,16 @@ describe('Content Collections', () => {
expect(json).to.haveOwnProperty('twoWithSlugConfig');
expect(json.twoWithSlugConfig.slug).to.equal('interesting-two.md');
});

it('Returns `with union schema` collection entry', async () => {
expect(json).to.haveOwnProperty('postWithUnionSchema');
expect(json.postWithUnionSchema.id).to.equal('post.md');
expect(json.postWithUnionSchema.data).to.deep.equal({
type: 'post',
title: 'My Post',
description: 'This is my post',
});
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { z, defineCollection } from 'astro:content';

const withSlugConfig = defineCollection({
slug({ id, data }) {
console.log({id, data})
return `${data.prefix}-${id}`;
},
schema: z.object({
prefix: z.string(),
})
}),
});

const withSchemaConfig = defineCollection({
Expand All @@ -18,7 +19,22 @@ const withSchemaConfig = defineCollection({
})
});

const withUnionSchema = defineCollection({
schema: z.discriminatedUnion('type', [
z.object({
type: z.literal('post'),
title: z.string(),
description: z.string(),
}),
z.object({
type: z.literal('newsletter'),
subject: z.string(),
}),
]),
});

export const collections = {
'with-slug-config': withSlugConfig,
'with-schema-config': withSchemaConfig,
'with-union-schema': withUnionSchema,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
type: newsletter
subject: My Newsletter
---

# It's a newsletter!
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
type: post
title: My Post
description: This is my post
---

# It's a post!
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export async function get() {
const withoutConfig = stripAllRenderFn(await getCollection('without-config'));
const withSchemaConfig = stripAllRenderFn(await getCollection('with-schema-config'));
const withSlugConfig = stripAllRenderFn(await getCollection('with-slug-config'));
const withUnionSchema = stripAllRenderFn(await getCollection('with-union-schema'));

return {
body: devalue.stringify({withoutConfig, withSchemaConfig, withSlugConfig}),
body: devalue.stringify({withoutConfig, withSchemaConfig, withSlugConfig, withUnionSchema}),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export async function get() {
const columbiaWithoutConfig = stripRenderFn(await getEntry('without-config', 'columbia.md'));
const oneWithSchemaConfig = stripRenderFn(await getEntry('with-schema-config', 'one.md'));
const twoWithSlugConfig = stripRenderFn(await getEntry('with-slug-config', 'two.md'));
const postWithUnionSchema = stripRenderFn(await getEntry('with-union-schema', 'post.md'));

return {
body: devalue.stringify({columbiaWithoutConfig, oneWithSchemaConfig, twoWithSlugConfig}),
body: devalue.stringify({columbiaWithoutConfig, oneWithSchemaConfig, twoWithSlugConfig, postWithUnionSchema}),
}
}

0 comments on commit 7721e5f

Please sign in to comment.