-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(v2): fix too strict markdown frontmatter validation (#4654)
* start work * use orta.vscode-jest * node 14 * add some better infra to validate markdown frontmatter * better docs frontmatter validation * fix Yaml / Joi validation issues * fix Yaml / Joi validation issues Co-authored-by: slorber <lorber.sebastien@gmail.com>
- Loading branch information
1 parent
c04e613
commit e11597a
Showing
8 changed files
with
238 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
{ | ||
"name": "Docusaurus Dev Container", | ||
"image": "mcr.microsoft.com/vscode/devcontainers/typescript-node:0-10-buster", | ||
"image": "mcr.microsoft.com/vscode/devcontainers/typescript-node:14-buster", | ||
"settings": { | ||
"terminal.integrated.shell.linux": "/bin/bash" | ||
}, | ||
"extensions": ["dbaeumer.vscode-eslint"], | ||
"extensions": ["dbaeumer.vscode-eslint", "orta.vscode-jest"], | ||
"forwardPorts": [3000], | ||
"postCreateCommand": "yarn install" | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/docusaurus-plugin-content-blog/src/__tests__/blogFrontMatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import { | ||
BlogPostFrontMatter, | ||
validateBlogPostFrontMatter, | ||
} from '../blogFrontMatter'; | ||
|
||
describe('validateBlogPostFrontMatter', () => { | ||
test('accept empty object', () => { | ||
const frontMatter = {}; | ||
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
test('accept valid values', () => { | ||
const frontMatter: BlogPostFrontMatter = { | ||
id: 'blog', | ||
title: 'title', | ||
description: 'description', | ||
date: 'date', | ||
slug: 'slug', | ||
draft: true, | ||
tags: ['hello', {label: 'tagLabel', permalink: '/tagPermalink'}], | ||
}; | ||
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 | ||
test('accept empty title', () => { | ||
const frontMatter: BlogPostFrontMatter = {title: ''}; | ||
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 | ||
test('accept empty description', () => { | ||
const frontMatter: BlogPostFrontMatter = {description: ''}; | ||
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
// See https://github.com/facebook/docusaurus/issues/4642 | ||
test('convert tags as numbers', () => { | ||
const frontMatter: BlogPostFrontMatter = { | ||
tags: [ | ||
// @ts-expect-error: number for test | ||
42, | ||
{ | ||
// @ts-expect-error: number for test | ||
label: 84, | ||
permalink: '/tagPermalink', | ||
}, | ||
], | ||
}; | ||
expect(validateBlogPostFrontMatter(frontMatter)).toEqual({ | ||
tags: [ | ||
'42', | ||
{ | ||
label: '84', | ||
permalink: '/tagPermalink', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/docusaurus-plugin-content-docs/src/__tests__/docFrontMatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {DocFrontMatter, validateDocFrontMatter} from '../docFrontMatter'; | ||
|
||
describe('validateDocFrontMatter', () => { | ||
test('accept empty object', () => { | ||
const frontMatter: DocFrontMatter = {}; | ||
expect(validateDocFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
test('accept valid values', () => { | ||
const frontMatter: DocFrontMatter = { | ||
id: 'blog', | ||
title: 'title', | ||
description: 'description', | ||
slug: 'slug', | ||
}; | ||
expect(validateDocFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 | ||
test('accept empty title', () => { | ||
const frontMatter: DocFrontMatter = {title: ''}; | ||
expect(validateDocFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
|
||
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 | ||
test('accept empty description', () => { | ||
const frontMatter: DocFrontMatter = {description: ''}; | ||
expect(validateDocFrontMatter(frontMatter)).toEqual(frontMatter); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/docusaurus-utils-validation/src/__tests__/validationUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import Joi from '../Joi'; | ||
import {JoiFrontMatter, validateFrontMatter} from '../validationUtils'; | ||
|
||
describe('validateFrontMatter', () => { | ||
test('should accept good values', () => { | ||
const schema = Joi.object<{test: string}>({ | ||
test: Joi.string(), | ||
}); | ||
const frontMatter = { | ||
test: 'hello', | ||
}; | ||
expect(validateFrontMatter(frontMatter, schema)).toEqual(frontMatter); | ||
}); | ||
|
||
test('should reject bad values', () => { | ||
const consoleError = jest.spyOn(console, 'error').mockImplementation(); | ||
const schema = Joi.object<{test: string}>({ | ||
test: Joi.string(), | ||
}); | ||
const frontMatter = { | ||
test: true, | ||
}; | ||
expect(() => | ||
validateFrontMatter(frontMatter, schema), | ||
).toThrowErrorMatchingInlineSnapshot(`"\\"test\\" must be a string"`); | ||
expect(consoleError).toHaveBeenCalledWith( | ||
expect.stringContaining('FrontMatter contains invalid values: '), | ||
); | ||
}); | ||
|
||
// Fix Yaml trying to convert strings to numbers automatically | ||
// We only want to deal with a single type in the final frontmatter (not string | number) | ||
test('should convert number values to string when string schema', () => { | ||
const schema = Joi.object<{test: string}>({ | ||
test: JoiFrontMatter.string(), | ||
}); | ||
const frontMatter = { | ||
test: 42, | ||
}; | ||
expect(validateFrontMatter(frontMatter, schema)).toEqual({test: '42'}); | ||
}); | ||
|
||
// Helps to fix Yaml trying to convert strings to dates automatically | ||
// We only want to deal with a single type in the final frontmatter (not string | Date) | ||
test('should convert date values when string schema', () => { | ||
const schema = Joi.object<{test: string}>({ | ||
test: JoiFrontMatter.string(), | ||
}); | ||
const date = new Date(); | ||
const frontMatter = { | ||
test: date, | ||
}; | ||
expect(validateFrontMatter(frontMatter, schema)).toEqual({ | ||
test: date.toString(), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters