Skip to content

Commit 194f429

Browse files
swyxioslorber
andauthored
fix: add docs tag validation to solve facebook#5478 (facebook#5479)
* fix: add docs tag validation to solve facebook#5478 fix: add docs tag validation to solve facebook#5478 * Update docFrontMatter.ts * Update docs-create-doc.mdx * improve tag validation error messages + tests * improve tags doc * fix test Co-authored-by: slorber <lorber.sebastien@gmail.com>
1 parent 8123271 commit 194f429

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

packages/docusaurus-plugin-content-blog/src/__tests__/blogFrontMatter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ describe('validateBlogPostFrontMatter tags', () => {
314314
{tags: ['hello', {label: 'tagLabel', permalink: '/tagPermalink'}]},
315315
],
316316
invalidFrontMatters: [
317-
[{tags: ''}, 'must be an array'],
317+
[{tags: ''}, '"tags" does not look like a valid FrontMatter Yaml array.'],
318318
[{tags: ['']}, 'not allowed to be empty'],
319319
],
320320
// See https://github.com/facebook/docusaurus/issues/4642

packages/docusaurus-plugin-content-docs/src/__tests__/docFrontMatter.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,24 @@ describe('validateDocFrontMatter parse_number_prefixes', () => {
242242
],
243243
});
244244
});
245+
246+
describe('validateDocFrontMatter tags', () => {
247+
testField({
248+
fieldName: 'tags',
249+
validFrontMatters: [{}, {tags: undefined}, {tags: ['tag1', 'tag2']}],
250+
convertibleFrontMatter: [[{tags: ['tag1', 42]}, {tags: ['tag1', '42']}]],
251+
invalidFrontMatters: [
252+
[{tags: 42}, '"tags" does not look like a valid FrontMatter Yaml array.'],
253+
[
254+
{tags: 'tag1, tag2'},
255+
'"tags" does not look like a valid FrontMatter Yaml array.',
256+
],
257+
[{tags: [{}]}, '"tags[0]" does not look like a valid tag'],
258+
[{tags: [true]}, '"tags[0]" does not look like a valid tag'],
259+
[
260+
{tags: ['tag1', {hey: 'test'}]},
261+
'"tags[1]" does not look like a valid tag',
262+
],
263+
],
264+
});
265+
});

packages/docusaurus-plugin-content-docs/src/docFrontMatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import {
99
JoiFrontMatter as Joi, // Custom instance for frontmatter
1010
URISchema,
11+
FrontMatterTagsSchema,
1112
validateFrontMatter,
1213
} from '@docusaurus/utils-validation';
1314
import {DocFrontMatter} from './types';
@@ -27,6 +28,7 @@ const DocFrontMatterSchema = Joi.object<DocFrontMatter>({
2728
slug: Joi.string(),
2829
sidebar_label: Joi.string(),
2930
sidebar_position: Joi.number(),
31+
tags: FrontMatterTagsSchema,
3032
pagination_label: Joi.string(),
3133
custom_edit_url: URISchema.allow('', null),
3234
parse_number_prefixes: Joi.boolean(),

packages/docusaurus-utils-validation/src/validationSchemas.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,22 @@ export const PathnameSchema = Joi.string()
6161
'{{#label}} is not a valid pathname. Pathname should start with slash and not contain any domain or query string.',
6262
);
6363

64-
export const FrontMatterTagsSchema = JoiFrontMatter.array().items(
65-
JoiFrontMatter.alternatives().try(
64+
const FrontMatterTagSchema = JoiFrontMatter.alternatives()
65+
.try(
6666
JoiFrontMatter.string().required(),
6767
JoiFrontMatter.object<Tag>({
6868
label: JoiFrontMatter.string().required(),
6969
permalink: JoiFrontMatter.string().required(),
7070
}).required(),
71-
),
72-
);
71+
)
72+
.messages({
73+
'alternatives.match': '{{#label}} does not look like a valid tag',
74+
'alternatives.types': '{{#label}} does not look like a valid tag',
75+
});
76+
77+
export const FrontMatterTagsSchema = JoiFrontMatter.array()
78+
.items(FrontMatterTagSchema)
79+
.messages({
80+
'array.base':
81+
'{{#label}} does not look like a valid FrontMatter Yaml array.',
82+
});

website/docs/guides/docs/docs-create-doc.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,11 @@ tags:
9999
---
100100
```
101101
<!-- prettier-ignore-end -->
102+
103+
:::tip
104+
105+
Tags can also be declared with `tags [Demo, Getting started]`
106+
107+
Read more about all the possible [Yaml array syntaxes](https://www.w3schools.io/file/yaml-arrays/).
108+
109+
:::

0 commit comments

Comments
 (0)