mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 19:16:58 +02:00
feat(v2): add option validation for remaining official plugins (#2970)
* feat(v2): add option validation lifecycle method * chore(v2): add dependencies * chore(v2): add yup dependency * feat(v2): add option validation for plugin-content-docs * chore(v2): add facebook copyright * refactor(v2): remove unused variable * chore(v2): add dependencies * chore(v2): add copyright * fix(v2): use strict for option validation * feat(v2): add option validation for plugin-content-pages * feat(v2): add schema for plugin-google-analytics and plugin-google-gtag * feat(v2): add option validation for plugin-sitemap * chore(v2): add dependency for yup * fix(v2): remove strict to allow normalization * refactor(v2): refactor validate method * feat(v2): modify existing tests * feat(v2): add tests for plugin normalization * style(v2): use a more descriptive filename for schema * feat(v2): add normalization tests * feat(v2): add more tests for option validation * refactor(v2): remove unused code * refactor(v2): remove unused code * refactor(v2): refactor methods and types * feat(v2): replace Yup with Joi * fix(v2): fix plugin-content-docs schema * feat(v2): modify tests for plugin-content-docs * fix(v2): fix a typo * refactor(v2): improve tests and refactor code * feat(v2): support both commonjs and ES modules * refactor(v2): refactor validateOption method * style(v2): fix eslint errors and typo in types * chore(v2): remove unused yup dependency * style(v2): standardize naming across official plugins * chore(v2): update test snapshots * chore(v2): remove obsolete snapshots * chore(v2): fix a typo and check test * feat(v2): add validation for new field * feat(v2): add test for new field
This commit is contained in:
parent
3213955e72
commit
0f59cd1599
24 changed files with 444 additions and 169 deletions
|
@ -9,7 +9,7 @@ import fs from 'fs-extra';
|
|||
import path from 'path';
|
||||
import pluginContentBlog from '../index';
|
||||
import {DocusaurusConfig, LoadContext} from '@docusaurus/types';
|
||||
import {PluginOptionSchema} from '../validation';
|
||||
import {PluginOptionSchema} from '../pluginOptionSchema';
|
||||
|
||||
function validateAndNormalize(schema, options) {
|
||||
const {value, error} = schema.validate(options);
|
||||
|
|
|
@ -5,11 +5,11 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {PluginOptionSchema, DefaultOptions} from '../validation';
|
||||
import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
|
||||
|
||||
test('normalize options', () => {
|
||||
const {value} = PluginOptionSchema.validate({});
|
||||
expect(value).toEqual(DefaultOptions);
|
||||
expect(value).toEqual(DEFAULT_OPTIONS);
|
||||
});
|
||||
|
||||
test('validate options', () => {
|
||||
|
@ -20,7 +20,7 @@ test('validate options', () => {
|
|||
routeBasePath: 'not_blog',
|
||||
});
|
||||
expect(value).toEqual({
|
||||
...DefaultOptions,
|
||||
...DEFAULT_OPTIONS,
|
||||
postsPerPage: 5,
|
||||
include: ['api/*', 'docs/*'],
|
||||
routeBasePath: 'not_blog',
|
||||
|
@ -54,7 +54,7 @@ test('convert all feed type to array with other feed type', () => {
|
|||
feedOptions: {type: 'all'},
|
||||
});
|
||||
expect(value).toEqual({
|
||||
...DefaultOptions,
|
||||
...DEFAULT_OPTIONS,
|
||||
feedOptions: {type: ['rss', 'atom']},
|
||||
});
|
||||
});
|
|
@ -21,7 +21,7 @@ import {
|
|||
BlogPaginated,
|
||||
BlogPost,
|
||||
} from './types';
|
||||
import {PluginOptionSchema} from './validation';
|
||||
import {PluginOptionSchema} from './pluginOptionSchema';
|
||||
import {
|
||||
LoadContext,
|
||||
PluginContentLoadedActions,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import * as Joi from '@hapi/joi';
|
||||
|
||||
export const DefaultOptions = {
|
||||
export const DEFAULT_OPTIONS = {
|
||||
feedOptions: {},
|
||||
beforeDefaultRehypePlugins: [],
|
||||
beforeDefaultRemarkPlugins: [],
|
||||
|
@ -27,22 +27,22 @@ export const DefaultOptions = {
|
|||
};
|
||||
|
||||
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),
|
||||
path: Joi.string().default(DEFAULT_OPTIONS.path),
|
||||
routeBasePath: Joi.string().default(DEFAULT_OPTIONS.routeBasePath),
|
||||
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
|
||||
postsPerPage: Joi.number()
|
||||
.integer()
|
||||
.min(1)
|
||||
.default(DefaultOptions.postsPerPage),
|
||||
blogListComponent: Joi.string().default(DefaultOptions.blogListComponent),
|
||||
blogPostComponent: Joi.string().default(DefaultOptions.blogPostComponent),
|
||||
.default(DEFAULT_OPTIONS.postsPerPage),
|
||||
blogListComponent: Joi.string().default(DEFAULT_OPTIONS.blogListComponent),
|
||||
blogPostComponent: Joi.string().default(DEFAULT_OPTIONS.blogPostComponent),
|
||||
blogTagsListComponent: Joi.string().default(
|
||||
DefaultOptions.blogTagsListComponent,
|
||||
DEFAULT_OPTIONS.blogTagsListComponent,
|
||||
),
|
||||
blogTagsPostsComponent: Joi.string().default(
|
||||
DefaultOptions.blogTagsPostsComponent,
|
||||
DEFAULT_OPTIONS.blogTagsPostsComponent,
|
||||
),
|
||||
showReadingTime: Joi.bool().default(DefaultOptions.showReadingTime),
|
||||
showReadingTime: Joi.bool().default(DEFAULT_OPTIONS.showReadingTime),
|
||||
remarkPlugins: Joi.array()
|
||||
.items(
|
||||
Joi.alternatives().try(
|
||||
|
@ -52,19 +52,19 @@ export const PluginOptionSchema = Joi.object({
|
|||
.length(2),
|
||||
),
|
||||
)
|
||||
.default(DefaultOptions.remarkPlugins),
|
||||
.default(DEFAULT_OPTIONS.remarkPlugins),
|
||||
rehypePlugins: Joi.array()
|
||||
.items(Joi.string())
|
||||
.default(DefaultOptions.rehypePlugins),
|
||||
.default(DEFAULT_OPTIONS.rehypePlugins),
|
||||
editUrl: Joi.string().uri(),
|
||||
truncateMarker: Joi.object().default(DefaultOptions.truncateMarker),
|
||||
admonitions: Joi.object().default(DefaultOptions.admonitions),
|
||||
truncateMarker: Joi.object().default(DEFAULT_OPTIONS.truncateMarker),
|
||||
admonitions: Joi.object().default(DEFAULT_OPTIONS.admonitions),
|
||||
beforeDefaultRemarkPlugins: Joi.array()
|
||||
.items(Joi.object())
|
||||
.default(DefaultOptions.beforeDefaultRemarkPlugins),
|
||||
.default(DEFAULT_OPTIONS.beforeDefaultRemarkPlugins),
|
||||
beforeDefaultRehypePlugins: Joi.array()
|
||||
.items(Joi.object())
|
||||
.default(DefaultOptions.beforeDefaultRehypePlugins),
|
||||
.default(DEFAULT_OPTIONS.beforeDefaultRehypePlugins),
|
||||
feedOptions: Joi.object({
|
||||
type: Joi.alternatives().conditional(
|
||||
Joi.string().equal('all', 'rss', 'atom'),
|
||||
|
@ -76,5 +76,5 @@ export const PluginOptionSchema = Joi.object({
|
|||
description: Joi.string(),
|
||||
copyright: Joi.string(),
|
||||
language: Joi.string(),
|
||||
}).default(DefaultOptions.feedOptions),
|
||||
}).default(DEFAULT_OPTIONS.feedOptions),
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue