fix(v2): improve BlogPostFrontMatter schema validation (#4759)

* fix(v2): improve BlogPostFrontMatter schema validation

* Edit doc

* Add deprecate warning message

* minor changes, disable warnings temporarily

* only disable warnings + fix frontmatter date type

Co-authored-by: Nam Hoang Le <nam.hoang.le@mgm-tp.com>
Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
Nam Hoang Le 2021-05-14 23:52:51 +07:00 committed by GitHub
parent b33ee8226d
commit e092910627
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 326 additions and 72 deletions

View file

@ -10,58 +10,255 @@ import {
validateBlogPostFrontMatter, validateBlogPostFrontMatter,
} from '../blogFrontMatter'; } from '../blogFrontMatter';
function testField(params: {
fieldName: keyof BlogPostFrontMatter;
validFrontMatters: BlogPostFrontMatter[];
convertibleFrontMatter?: [
ConvertableFrontMatter: Record<string, unknown>,
ConvertedFrontMatter: BlogPostFrontMatter,
][];
invalidFrontMatters?: [
InvalidFrontMatter: Record<string, unknown>,
ErrorMessage: string,
][];
}) {
describe(`"${params.fieldName}" field`, () => {
test('accept valid values', () => {
params.validFrontMatters.forEach((frontMatter) => {
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter);
});
});
test('convert valid values', () => {
params.convertibleFrontMatter?.forEach(
([convertibleFrontMatter, convertedFrontMatter]) => {
expect(validateBlogPostFrontMatter(convertibleFrontMatter)).toEqual(
convertedFrontMatter,
);
},
);
});
test('throw error for values', () => {
params.invalidFrontMatters?.forEach(([frontMatter, message]) => {
expect(() => validateBlogPostFrontMatter(frontMatter)).toThrow(message);
});
});
});
}
describe('validateBlogPostFrontMatter', () => { describe('validateBlogPostFrontMatter', () => {
test('accept empty object', () => { test('accept empty object', () => {
const frontMatter = {}; const frontMatter = {};
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter);
}); });
test('accept valid values', () => { test('accept unknown field', () => {
const frontMatter: BlogPostFrontMatter = { const frontMatter = {abc: '1'};
id: 'blog',
title: 'title',
description: 'description',
date: 'date',
slug: 'slug',
draft: true,
tags: ['hello', {label: 'tagLabel', permalink: '/tagPermalink'}],
};
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter);
}); });
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 testField({
test('accept empty title', () => { fieldName: 'description',
const frontMatter: BlogPostFrontMatter = {title: ''}; validFrontMatters: [
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); // See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
{description: ''},
{description: 'description'},
],
}); });
// See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398 testField({
test('accept empty description', () => { fieldName: 'title',
const frontMatter: BlogPostFrontMatter = {description: ''}; validFrontMatters: [
expect(validateBlogPostFrontMatter(frontMatter)).toEqual(frontMatter); // See https://github.com/facebook/docusaurus/issues/4591#issuecomment-822372398
{title: ''},
{title: 'title'},
],
}); });
// See https://github.com/facebook/docusaurus/issues/4642 testField({
test('convert tags as numbers', () => { fieldName: 'id',
const frontMatter: BlogPostFrontMatter = { validFrontMatters: [{id: '123'}, {id: 'id'}],
tags: [ invalidFrontMatters: [[{id: ''}, 'is not allowed to be empty']],
// @ts-expect-error: number for test });
42,
{ testField({
// @ts-expect-error: number for test fieldName: 'author',
label: 84, validFrontMatters: [{author: '123'}, {author: 'author'}],
permalink: '/tagPermalink', invalidFrontMatters: [[{author: ''}, 'is not allowed to be empty']],
}, });
testField({
fieldName: 'authorTitle',
validFrontMatters: [{authorTitle: '123'}, {authorTitle: 'authorTitle'}],
invalidFrontMatters: [[{authorTitle: ''}, 'is not allowed to be empty']],
});
testField({
fieldName: 'author_title',
validFrontMatters: [{author_title: '123'}, {author_title: 'author_title'}],
invalidFrontMatters: [[{author_title: ''}, 'is not allowed to be empty']],
});
testField({
fieldName: 'authorURL',
validFrontMatters: [{authorURL: 'https://docusaurus.io'}],
invalidFrontMatters: [
[{authorURL: ''}, 'is not allowed to be empty'],
[{authorURL: '@site/api/author'}, 'must be a valid uri'],
[{authorURL: '../../api/author'}, 'must be a valid uri'],
],
});
testField({
fieldName: 'author_url',
validFrontMatters: [{author_url: 'https://docusaurus.io'}],
invalidFrontMatters: [
[{author_url: ''}, 'is not allowed to be empty'],
[{author_url: '@site/api/author'}, 'must be a valid uri'],
[{author_url: '../../api/author'}, 'must be a valid uri'],
],
});
testField({
fieldName: 'authorImageURL',
validFrontMatters: [
{authorImageURL: 'https://docusaurus.io/asset/image.png'},
],
invalidFrontMatters: [
[{authorImageURL: ''}, 'is not allowed to be empty'],
[{authorImageURL: '@site/api/asset/image.png'}, 'must be a valid uri'],
[{authorImageURL: '../../api/asset/image.png'}, 'must be a valid uri'],
],
});
testField({
fieldName: 'author_image_url',
validFrontMatters: [
{author_image_url: 'https://docusaurus.io/asset/image.png'},
],
invalidFrontMatters: [
[{author_image_url: ''}, 'is not allowed to be empty'],
[{author_image_url: '@site/api/asset/image.png'}, 'must be a valid uri'],
[{author_image_url: '../../api/asset/image.png'}, 'must be a valid uri'],
],
});
testField({
fieldName: 'slug',
validFrontMatters: [
{slug: 'blog/'},
{slug: '/blog'},
{slug: '/blog/'},
{slug: './blog'},
{slug: '../blog'},
{slug: '../../blog'},
{slug: '/api/plugins/@docusaurus/plugin-debug'},
{slug: '@site/api/asset/image.png'},
],
invalidFrontMatters: [[{slug: ''}, 'is not allowed to be empty']],
});
testField({
fieldName: 'image',
validFrontMatters: [
{image: 'blog/'},
{image: '/blog'},
{image: '/blog/'},
{image: './blog'},
{image: '../blog'},
{image: '../../blog'},
{image: '/api/plugins/@docusaurus/plugin-debug'},
{image: '@site/api/asset/image.png'},
],
invalidFrontMatters: [
[{image: ''}, 'is not allowed to be empty'],
[{image: 'https://docusaurus.io'}, 'must be a valid relative uri'],
[
{image: 'https://docusaurus.io/blog/image.png'},
'must be a valid relative uri',
], ],
}; ],
expect(validateBlogPostFrontMatter(frontMatter)).toEqual({ });
tags: [
'42', testField({
{ fieldName: 'tags',
label: '84', validFrontMatters: [
permalink: '/tagPermalink', {tags: []},
}, {tags: ['hello']},
{tags: ['hello', 'world']},
{tags: ['hello', 'world']},
{tags: ['hello', {label: 'tagLabel', permalink: '/tagPermalink'}]},
],
invalidFrontMatters: [
[{tags: ''}, 'must be an array'],
[{tags: ['']}, 'is not allowed to be empty'],
],
// See https://github.com/facebook/docusaurus/issues/4642
convertibleFrontMatter: [
[{tags: [42]}, {tags: ['42']}],
[
{tags: [{label: 84, permalink: '/tagPermalink'}]},
{tags: [{label: '84', permalink: '/tagPermalink'}]},
], ],
}); ],
});
testField({
fieldName: 'keywords',
validFrontMatters: [
{keywords: ['hello']},
{keywords: ['hello', 'world']},
{keywords: ['hello', 'world']},
{keywords: ['hello']},
],
invalidFrontMatters: [
[{keywords: ''}, 'must be an array'],
[{keywords: ['']}, 'is not allowed to be empty'],
[{keywords: []}, 'does not contain 1 required value(s)'],
],
});
testField({
fieldName: 'draft',
validFrontMatters: [{draft: true}, {draft: false}],
convertibleFrontMatter: [
[{draft: 'true'}, {draft: true}],
[{draft: 'false'}, {draft: false}],
],
invalidFrontMatters: [
[{draft: 'yes'}, 'must be a boolean'],
[{draft: 'no'}, 'must be a boolean'],
],
});
testField({
fieldName: 'hide_table_of_contents',
validFrontMatters: [
{hide_table_of_contents: true},
{hide_table_of_contents: false},
],
convertibleFrontMatter: [
[{hide_table_of_contents: 'true'}, {hide_table_of_contents: true}],
[{hide_table_of_contents: 'false'}, {hide_table_of_contents: false}],
],
invalidFrontMatters: [
[{hide_table_of_contents: 'yes'}, 'must be a boolean'],
[{hide_table_of_contents: 'no'}, 'must be a boolean'],
],
});
testField({
fieldName: 'date',
validFrontMatters: [
// @ts-expect-error: number for test
{date: new Date('2020-01-01')},
{date: '2020-01-01'},
{date: '2020'},
],
invalidFrontMatters: [
[{date: 'abc'}, 'must be a valid date'],
[{date: ''}, 'must be a valid date'],
],
}); });
}); });

View file

@ -5,13 +5,14 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
/* eslint-disable camelcase */
import { import {
JoiFrontMatter as Joi, // Custom instance for frontmatter JoiFrontMatter as Joi, // Custom instance for frontmatter
validateFrontMatter, validateFrontMatter,
} from '@docusaurus/utils-validation'; } from '@docusaurus/utils-validation';
import {Tag} from './types'; import {Tag} from './types';
// TODO complete this frontmatter + add unit tests
export type BlogPostFrontMatter = { export type BlogPostFrontMatter = {
id?: string; id?: string;
title?: string; title?: string;
@ -19,7 +20,21 @@ export type BlogPostFrontMatter = {
tags?: (string | Tag)[]; tags?: (string | Tag)[];
slug?: string; slug?: string;
draft?: boolean; draft?: boolean;
date?: string; date?: Date;
author?: string;
author_title?: string;
author_url?: string;
author_image_url?: string;
image?: string;
keywords?: string[];
hide_table_of_contents?: boolean;
/** @deprecated */
authorTitle?: string;
authorURL?: string;
authorImageURL?: string;
}; };
// NOTE: we don't add any default value on purpose here // NOTE: we don't add any default value on purpose here
@ -39,10 +54,31 @@ const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
title: Joi.string().allow(''), title: Joi.string().allow(''),
description: Joi.string().allow(''), description: Joi.string().allow(''),
tags: Joi.array().items(BlogTagSchema), tags: Joi.array().items(BlogTagSchema),
slug: Joi.string(),
draft: Joi.boolean(), draft: Joi.boolean(),
date: Joi.string().allow(''), // TODO validate the date better! date: Joi.date().raw(),
}).unknown();
author: Joi.string(),
author_title: Joi.string(),
author_url: Joi.string().uri(),
author_image_url: Joi.string().uri(),
slug: Joi.string(),
image: Joi.string().uri({relativeOnly: true}),
keywords: Joi.array().items(Joi.string().required()),
hide_table_of_contents: Joi.boolean(),
// TODO re-enable warnings later, our v1 blog posts use those older frontmatter fields
authorURL: Joi.string().uri(),
// .warning('deprecate.error', { alternative: '"author_url"'}),
authorTitle: Joi.string(),
// .warning('deprecate.error', { alternative: '"author_title"'}),
authorImageURL: Joi.string().uri(),
// .warning('deprecate.error', { alternative: '"author_image_url"'}),
})
.unknown()
.messages({
'deprecate.error':
'{#label} blog frontMatter field is deprecated. Please use {#alternative} instead.',
});
export function validateBlogPostFrontMatter( export function validateBlogPostFrontMatter(
frontMatter: Record<string, unknown>, frontMatter: Record<string, unknown>,

View file

@ -46,7 +46,7 @@ export function getSourceToPermalink(
// YYYY-MM-DD-{name}.mdx? // YYYY-MM-DD-{name}.mdx?
// Prefer named capture, but older Node versions do not support it. // Prefer named capture, but older Node versions do not support it.
const FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/; const DATE_FILENAME_PATTERN = /^(\d{4}-\d{1,2}-\d{1,2})-?(.*?).mdx?$/;
function toUrl({date, link}: DateLink) { function toUrl({date, link}: DateLink) {
return `${date return `${date
@ -165,24 +165,24 @@ export async function generateBlogPosts(
); );
} }
let date; let date: Date | undefined;
// Extract date and title from filename. // Extract date and title from filename.
const match = blogFileName.match(FILENAME_PATTERN); const dateFilenameMatch = blogFileName.match(DATE_FILENAME_PATTERN);
let linkName = blogFileName.replace(/\.mdx?$/, ''); let linkName = blogFileName.replace(/\.mdx?$/, '');
if (match) { if (dateFilenameMatch) {
const [, dateString, name] = match; const [, dateString, name] = dateFilenameMatch;
date = new Date(dateString); date = new Date(dateString);
linkName = name; linkName = name;
} }
// Prefer user-defined date. // Prefer user-defined date.
if (frontMatter.date) { if (frontMatter.date) {
date = new Date(frontMatter.date); date = frontMatter.date;
} }
// Use file create time for blog. // Use file create time for blog.
date = date || (await fs.stat(source)).birthtime; date = date ?? (await fs.stat(source)).birthtime;
const formattedDate = new Intl.DateTimeFormat(i18n.currentLocale, { const formattedDate = new Intl.DateTimeFormat(i18n.currentLocale, {
day: 'numeric', day: 'numeric',
month: 'long', month: 'long',
@ -193,7 +193,8 @@ export async function generateBlogPosts(
const description = frontMatter.description ?? excerpt ?? ''; const description = frontMatter.description ?? excerpt ?? '';
const slug = const slug =
frontMatter.slug || (match ? toUrl({date, link: linkName}) : linkName); frontMatter.slug ||
(dateFilenameMatch ? toUrl({date, link: linkName}) : linkName);
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]); const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);

View file

@ -31,7 +31,7 @@ describe('validateFrontMatter', () => {
validateFrontMatter(frontMatter, schema), validateFrontMatter(frontMatter, schema),
).toThrowErrorMatchingInlineSnapshot(`"\\"test\\" must be a string"`); ).toThrowErrorMatchingInlineSnapshot(`"\\"test\\" must be a string"`);
expect(consoleError).toHaveBeenCalledWith( expect(consoleError).toHaveBeenCalledWith(
expect.stringContaining('FrontMatter contains invalid values: '), expect.stringContaining('The following FrontMatter'),
); );
}); });

View file

@ -106,21 +106,38 @@ export function validateFrontMatter<T>(
frontMatter: Record<string, unknown>, frontMatter: Record<string, unknown>,
schema: Joi.ObjectSchema<T>, schema: Joi.ObjectSchema<T>,
): T { ): T {
try { const {value, error, warning} = schema.validate(frontMatter, {
return JoiFrontMatter.attempt(frontMatter, schema, { convert: true,
convert: true, allowUnknown: true,
allowUnknown: true, abortEarly: false,
}); });
} catch (e) {
if (error) {
const frontMatterString = JSON.stringify(frontMatter, null, 2);
const errorDetails = error.details;
const invalidFields = errorDetails.map(({path}) => path).join(', ');
const errorMessages = errorDetails
.map(({message}) => ` - ${message}`)
.join('\n');
logValidationBugReportHint();
console.error( console.error(
chalk.red( chalk.red(
`FrontMatter contains invalid values: ${JSON.stringify( `The following FrontMatter:\n${chalk.yellow(
frontMatter, frontMatterString,
null, )}\ncontains invalid values for field(s): ${invalidFields}.\n${errorMessages}\n`,
2,
)}`,
), ),
); );
throw e; throw error;
} }
if (warning) {
const warningMessages = warning.details
.map(({message}) => message)
.join('\n');
console.log(chalk.yellow(warningMessages));
}
return value;
} }

View file

@ -55,16 +55,19 @@ A whole bunch of exploration to follow.
The only required field is `title`; however, we provide options to add author information to your blog post as well along with other options. The only required field is `title`; however, we provide options to add author information to your blog post as well along with other options.
- `author` - The author name to be displayed. - `author`: The author name to be displayed.
- `author_url` - The URL that the author's name will be linked to. This could be a GitHub, Twitter, Facebook profile URL, etc. - `author_url`: The URL that the author's name will be linked to. This could be a GitHub, Twitter, Facebook profile URL, etc.
- `author_image_url` - The URL to the author's thumbnail image. - `author_image_url`: The URL to the author's thumbnail image.
- `author_title` - A description of the author. - `author_title`: A description of the author.
- `title` - The blog post title. - `title`: The blog post title.
- `tags` - A list of strings to tag to your post. - `slug`: Allows to customize the blog post url (`/<routeBasePath>/<slug>`). Support multiple patterns: `slug: my-blog-post`, `slug: /my/path/to/blog/post`, slug: `/`.
- `draft` - A boolean flag to indicate that the blog post is work-in-progress and therefore should not be published yet. However, draft blog posts will be displayed during development. - `date`: The blog post creation date. If not specified, this could be extracted from the file name, e.g, `2021-04-15-blog-post.mdx`. By default, it is the markdown file creation time.
- `tags`: A list of strings or objects of two string fields `label` and `permalink` to tag to your post.
- `draft`: A boolean flag to indicate that the blog post is work-in-progress and therefore should not be published yet. However, draft blog posts will be displayed during development.
- `description`: The description of your post, which will become the `<meta name="description" content="..."/>` and `<meta property="og:description" content="..."/>` in `<head>`, used by search engines. If this field is not present, it will default to the first line of the contents. - `description`: The description of your post, which will become the `<meta name="description" content="..."/>` and `<meta property="og:description" content="..."/>` in `<head>`, used by search engines. If this field is not present, it will default to the first line of the contents.
- `keywords`: Keywords meta tag, which will become the `<meta name="keywords" content="keyword1,keyword2,..."/>` in `<head>`, used by search engines.
- `image`: Cover or thumbnail image that will be used when displaying the link to your post. - `image`: Cover or thumbnail image that will be used when displaying the link to your post.
- `hide_table_of_contents`: Whether to hide the table of contents to the right. By default it is `false`. - `hide_table_of_contents`: Whether to hide the table of contents to the right. By default, it is `false`.
## Summary truncation {#summary-truncation} ## Summary truncation {#summary-truncation}