mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-29 17:07:08 +02:00
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:
parent
c04e613ffe
commit
e11597aba9
8 changed files with 238 additions and 21 deletions
|
@ -83,3 +83,44 @@ export function normalizeThemeConfig<T>(
|
|||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
// Enhance the default Joi.string() type so that it can convert number to strings
|
||||
// If user use frontmatter "tag: 2021", we shouldn't need to ask the user to write "tag: '2021'"
|
||||
// Also yaml tries to convert patterns like "2019-01-01" to dates automatically
|
||||
// see https://github.com/facebook/docusaurus/issues/4642
|
||||
// see https://github.com/sideway/joi/issues/1442#issuecomment-823997884
|
||||
const JoiFrontMatterString: Joi.Extension = {
|
||||
type: 'string',
|
||||
base: Joi.string(),
|
||||
// Fix Yaml that tries to auto-convert many things to string out of the box
|
||||
prepare: (value) => {
|
||||
if (typeof value === 'number' || value instanceof Date) {
|
||||
return {value: value.toString()};
|
||||
}
|
||||
return {value};
|
||||
},
|
||||
};
|
||||
export const JoiFrontMatter: typeof Joi = Joi.extend(JoiFrontMatterString);
|
||||
|
||||
export function validateFrontMatter<T>(
|
||||
frontMatter: Record<string, unknown>,
|
||||
schema: Joi.ObjectSchema<T>,
|
||||
): T {
|
||||
try {
|
||||
return JoiFrontMatter.attempt(frontMatter, schema, {
|
||||
convert: true,
|
||||
allowUnknown: true,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(
|
||||
chalk.red(
|
||||
`FrontMatter contains invalid values: ${JSON.stringify(
|
||||
frontMatter,
|
||||
null,
|
||||
2,
|
||||
)}`,
|
||||
),
|
||||
);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue