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>
This commit is contained in:
John Reilly 2021-04-21 15:19:55 +01:00 committed by GitHub
parent c04e613ffe
commit e11597aba9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 238 additions and 21 deletions

View file

@ -5,11 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/
import {Joi} from '@docusaurus/utils-validation';
import {
JoiFrontMatter as Joi, // Custom instance for frontmatter
validateFrontMatter,
} from '@docusaurus/utils-validation';
import {Tag} from './types';
// TODO complete this frontmatter + add unit tests
type BlogPostFrontMatter = {
export type BlogPostFrontMatter = {
id?: string;
title?: string;
description?: string;
@ -19,6 +22,10 @@ type BlogPostFrontMatter = {
date?: string;
};
// NOTE: we don't add any default value on purpose here
// We don't want default values to magically appear in doc metadatas and props
// While the user did not provide those values explicitly
// We use default values in code instead
const BlogTagSchema = Joi.alternatives().try(
Joi.string().required(),
Joi.object<Tag>({
@ -29,15 +36,16 @@ const BlogTagSchema = Joi.alternatives().try(
const BlogFrontMatterSchema = Joi.object<BlogPostFrontMatter>({
id: Joi.string(),
title: Joi.string(),
description: Joi.string(),
title: Joi.string().allow(''),
description: Joi.string().allow(''),
tags: Joi.array().items(BlogTagSchema),
slug: Joi.string(),
draft: Joi.boolean(),
date: Joi.string().allow(''), // TODO validate the date better!
}).unknown();
export function assertBlogPostFrontMatter(
export function validateBlogPostFrontMatter(
frontMatter: Record<string, unknown>,
): asserts frontMatter is BlogPostFrontMatter {
Joi.attempt(frontMatter, BlogFrontMatterSchema);
): BlogPostFrontMatter {
return validateFrontMatter(frontMatter, BlogFrontMatterSchema);
}