-
-
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.
feat(v2): option and config validation life cycle method for official…
… plugins (#2943) * add validation for blog plugin * fix wrong default component * fix test and add yup to package.json * remove console.log * add validation for classic theme and code block theme * add yup to packages * remove console.log * fix build * fix logo required * replaced yup with joi * fix test * remove hapi from docusuars core * replace joi with @hapi/joi * fix eslint * fix remark plugin type * change remark plugin validation to match documentation * move schema to it's own file * allow unknown only on outer theme object * fix type for schema type * fix yarn.lock * support both commonjs and ES modules * add docs for new lifecycle method
- Loading branch information
1 parent
ce10646
commit 81d8553
Showing
18 changed files
with
490 additions
and
63 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
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
5 changes: 5 additions & 0 deletions
5
packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/validation.test.ts.snap
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`throw Error in case of invalid feedtype 1`] = `[ValidationError: "feedOptions.type" does not match any of the allowed types]`; | ||
|
||
exports[`throw Error in case of invalid options 1`] = `[ValidationError: "postsPerPage" must be larger than or equal to 1]`; |
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
60 changes: 60 additions & 0 deletions
60
packages/docusaurus-plugin-content-blog/src/__tests__/validation.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,60 @@ | ||
/** | ||
* 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 {PluginOptionSchema, DefaultOptions} from '../validation'; | ||
|
||
test('normalize options', () => { | ||
const {value} = PluginOptionSchema.validate({}); | ||
expect(value).toEqual(DefaultOptions); | ||
}); | ||
|
||
test('validate options', () => { | ||
const {value} = PluginOptionSchema.validate({ | ||
path: 'not_blog', | ||
postsPerPage: 5, | ||
include: ['api/*', 'docs/*'], | ||
routeBasePath: 'not_blog', | ||
}); | ||
expect(value).toEqual({ | ||
...DefaultOptions, | ||
postsPerPage: 5, | ||
include: ['api/*', 'docs/*'], | ||
routeBasePath: 'not_blog', | ||
path: 'not_blog', | ||
}); | ||
}); | ||
|
||
test('throw Error in case of invalid options', () => { | ||
const {error} = PluginOptionSchema.validate({ | ||
path: 'not_blog', | ||
postsPerPage: -1, | ||
include: ['api/*', 'docs/*'], | ||
routeBasePath: 'not_blog', | ||
}); | ||
|
||
expect(error).toMatchSnapshot(); | ||
}); | ||
|
||
test('throw Error in case of invalid feedtype', () => { | ||
const {error} = PluginOptionSchema.validate({ | ||
feedOptions: { | ||
type: 'none', | ||
}, | ||
}); | ||
|
||
expect(error).toMatchSnapshot(); | ||
}); | ||
|
||
test('convert all feed type to array with other feed type', () => { | ||
const {value} = PluginOptionSchema.validate({ | ||
feedOptions: {type: 'all'}, | ||
}); | ||
expect(value).toEqual({ | ||
...DefaultOptions, | ||
feedOptions: {type: ['rss', 'atom']}, | ||
}); | ||
}); |
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
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,80 @@ | ||
/** | ||
* 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 * as Joi from '@hapi/joi'; | ||
|
||
export const DefaultOptions = { | ||
feedOptions: {}, | ||
beforeDefaultRehypePlugins: [], | ||
beforeDefaultRemarkPlugins: [], | ||
admonitions: {}, | ||
truncateMarker: /<!--\s*(truncate)\s*-->/, | ||
rehypePlugins: [], | ||
remarkPlugins: [], | ||
showReadingTime: true, | ||
blogTagsPostsComponent: '@theme/BlogTagsPostsPage', | ||
blogTagsListComponent: '@theme/BlogTagsListPage', | ||
blogPostComponent: '@theme/BlogPostPage', | ||
blogListComponent: '@theme/BlogListPage', | ||
postsPerPage: 10, | ||
include: ['*.md', '*.mdx'], | ||
routeBasePath: 'blog', | ||
path: 'blog', | ||
}; | ||
|
||
export const PluginOptionSchema = Joi.object({ | ||
path: Joi.string().default(DefaultOptions.path), | ||
routeBasePath: Joi.string().default(DefaultOptions.routeBasePath), | ||
include: Joi.array().items(Joi.string()).default(DefaultOptions.include), | ||
postsPerPage: Joi.number() | ||
.integer() | ||
.min(1) | ||
.default(DefaultOptions.postsPerPage), | ||
blogListComponent: Joi.string().default(DefaultOptions.blogListComponent), | ||
blogPostComponent: Joi.string().default(DefaultOptions.blogPostComponent), | ||
blogTagsListComponent: Joi.string().default( | ||
DefaultOptions.blogTagsListComponent, | ||
), | ||
blogTagsPostsComponent: Joi.string().default( | ||
DefaultOptions.blogTagsPostsComponent, | ||
), | ||
showReadingTime: Joi.bool().default(DefaultOptions.showReadingTime), | ||
remarkPlugins: Joi.array() | ||
.items( | ||
Joi.alternatives().try( | ||
Joi.function(), | ||
Joi.array() | ||
.items(Joi.function().required(), Joi.object().required()) | ||
.length(2), | ||
), | ||
) | ||
.default(DefaultOptions.remarkPlugins), | ||
rehypePlugins: Joi.array() | ||
.items(Joi.string()) | ||
.default(DefaultOptions.rehypePlugins), | ||
editUrl: Joi.string().uri(), | ||
truncateMarker: Joi.object().default(DefaultOptions.truncateMarker), | ||
admonitions: Joi.object().default(DefaultOptions.admonitions), | ||
beforeDefaultRemarkPlugins: Joi.array() | ||
.items(Joi.object()) | ||
.default(DefaultOptions.beforeDefaultRemarkPlugins), | ||
beforeDefaultRehypePlugins: Joi.array() | ||
.items(Joi.object()) | ||
.default(DefaultOptions.beforeDefaultRehypePlugins), | ||
feedOptions: Joi.object({ | ||
type: Joi.alternatives().conditional( | ||
Joi.string().equal('all', 'rss', 'atom'), | ||
{ | ||
then: Joi.custom((val) => (val === 'all' ? ['rss', 'atom'] : [val])), | ||
}, | ||
), | ||
title: Joi.string(), | ||
description: Joi.string(), | ||
copyright: Joi.string(), | ||
language: Joi.string(), | ||
}).default(DefaultOptions.feedOptions), | ||
}); |
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
Oops, something went wrong.