Skip to content

Commit 47da88b

Browse files
committed
Optional list should not require the field to be set in the frontmatter #95
1 parent 70788b6 commit 47da88b

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

packages/@contentlayer/source-files/src/__test__/fetchData/makeCacheItemFromFilePath.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ t.test('makeCacheItemFromFilePath', async (t) => {
2727
}
2828
})
2929

30-
t.test('b.md: missing required field', async (t) => {
30+
t.test('b.md: missing required field: list of strings', async (t) => {
3131
const TestPost = defineDocumentType(() => ({
3232
name: 'TestPost',
3333
filePathPattern: `**/*.md`,
@@ -44,6 +44,23 @@ t.test('makeCacheItemFromFilePath', async (t) => {
4444
}
4545
})
4646

47+
t.test('b.md: missing optional field: list of strings', async (t) => {
48+
const TestPost = defineDocumentType(() => ({
49+
name: 'TestPost',
50+
filePathPattern: `**/*.md`,
51+
fields: {
52+
tags: { type: 'list', of: { type: 'string' }, required: false },
53+
},
54+
}))
55+
56+
const { result } = await runTest({ documentTypes: [TestPost], contentDirPath, relativeFilePath: 'b.md' })
57+
58+
t.equal(result._tag, 'Right')
59+
if (E.isRight(result)) {
60+
t.ok(result.right.document)
61+
}
62+
})
63+
4764
t.test('c.md: invalid frontmatter', async (t) => {
4865
const TestPost = defineDocumentType(() => ({
4966
name: 'TestPost',

packages/@contentlayer/source-files/src/fetchData/validateDocumentData.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,13 @@ const validateFieldData = ({
185185
O.Option<FetchDataError.IncompatibleFieldDataError | FetchDataError.ReferencedFileDoesNotExistError>
186186
> =>
187187
T.gen(function* ($) {
188+
const dataIsNil = rawFieldData === undefined || rawFieldData === null
189+
if (dataIsNil && fieldDef.isRequired === false) {
190+
return O.none
191+
}
192+
188193
switch (fieldDef.type) {
189194
case 'list':
190-
// TODO fix if optional
191195
return Array.isArray(rawFieldData)
192196
? O.none
193197
: O.some(
@@ -197,7 +201,7 @@ const validateFieldData = ({
197201
documentTypeName,
198202
}),
199203
)
200-
// TODO also check for lists
204+
// TODO also check for references in lists
201205
case 'reference':
202206
if (typeof rawFieldData === 'string') {
203207
const fullFilePath = filePathJoin(contentDirPath, rawFieldData)

packages/next-contentlayer/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { runContentlayerBuild, runContentlayerDev } from './plugin.js'
44

55
export type { NextConfig }
66

7-
type PluginOptions = {}
7+
export type PluginOptions = {}
88

99
let devServerStarted = false
1010

0 commit comments

Comments
 (0)