Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): modify validation schema and tests for rehype/remark + remove duplicate dependency #3247

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';

// the type of remark/rehype plugins is function
const remarkRehypePluginStub = () => {};
// the type of remark/rehype plugins can be either function, object or array
const markdownPluginsFunctionStub = () => {};
const markdownPluginsObjectStub = {};

test('should normalize options', () => {
const {value} = PluginOptionSchema.validate({});
const {value, error} = PluginOptionSchema.validate({});
expect(value).toEqual(DEFAULT_OPTIONS);
expect(error).toBe(undefined);
});

test('should accept correctly defined user options', () => {
Expand All @@ -24,27 +26,29 @@ test('should accept correctly defined user options', () => {
postsPerPage: 5,
include: ['api/*', 'docs/*'],
};
const {value} = PluginOptionSchema.validate(userOptions);
const {value, error} = PluginOptionSchema.validate(userOptions);
expect(value).toEqual({
...userOptions,
feedOptions: {type: ['rss'], title: 'myTitle'},
});
expect(error).toBe(undefined);
});

test('should accept valid user options', async () => {
const userOptions = {
...DEFAULT_OPTIONS,
routebasePath: '',
routeBasePath: '',
beforeDefaultRemarkPlugins: [],
beforeDefaultRehypePlugins: [remarkRehypePluginStub],
remarkPlugins: [remarkRehypePluginStub, {option1: '42'}],
beforeDefaultRehypePlugins: [markdownPluginsFunctionStub],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
remarkRehypePluginStub,
[remarkRehypePluginStub, {option1: '42'}],
markdownPluginsObjectStub,
[markdownPluginsFunctionStub, {option1: '42'}],
],
};
const {value} = await PluginOptionSchema.validate(userOptions);
const {value, error} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
expect(error).toBe(undefined);
});

test('should throw Error in case of invalid options', () => {
Expand Down
22 changes: 6 additions & 16 deletions packages/docusaurus-plugin-content-blog/src/pluginOptionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,12 @@ export const PluginOptionSchema = Joi.object({
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
editUrl: URISchema,
truncateMarker: Joi.object().default(DEFAULT_OPTIONS.truncateMarker),
beforeDefaultRemarkPlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.beforeDefaultRemarkPlugins),
beforeDefaultRehypePlugins: Joi.array()
.items(
Joi.array()
.items(Joi.function().required(), Joi.object().required())
.length(2),
Joi.function(),
)
.default(DEFAULT_OPTIONS.beforeDefaultRehypePlugins),
beforeDefaultRemarkPlugins: RemarkPluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
beforeDefaultRehypePlugins: RehypePluginsSchema.default(
DEFAULT_OPTIONS.beforeDefaultRehypePlugins,
),
feedOptions: Joi.object({
type: Joi.alternatives().conditional(
Joi.string().equal('all', 'rss', 'atom'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {PluginOptionSchema, DEFAULT_OPTIONS} from '../pluginOptionSchema';
import {normalizePluginOptions} from '@docusaurus/utils-validation';

// the type of remark/rehype plugins is function
const remarkRehypePluginStub = () => {};
const markdownPluginsFunctionStub = () => {};
const markdownPluginsObjectStub = {};

describe('normalizeDocsPluginOptions', () => {
test('should return default options for undefined user options', async () => {
const {value} = await PluginOptionSchema.validate({});
const {value, error} = await PluginOptionSchema.validate({});
expect(value).toEqual(DEFAULT_OPTIONS);
expect(error).toBe(undefined);
Copy link
Contributor Author

@teikjun teikjun Aug 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible for value to match when there is an error, so it is safer to include this

});

test('should accept correctly defined user options', async () => {
Expand All @@ -26,29 +28,57 @@ describe('normalizeDocsPluginOptions', () => {
sidebarPath: 'my-sidebar', // Path to sidebar configuration for showing a list of markdown pages.
docLayoutComponent: '@theme/DocPage',
docItemComponent: '@theme/DocItem',
remarkPlugins: [],
rehypePlugins: [remarkRehypePluginStub],
remarkPlugins: [markdownPluginsObjectStub],
rehypePlugins: [markdownPluginsFunctionStub],
showLastUpdateTime: true,
showLastUpdateAuthor: true,
admonitions: {},
excludeNextVersionDocs: true,
disableVersioning: true,
};
const {value} = await PluginOptionSchema.validate(userOptions);
const {value, error} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
expect(error).toBe(undefined);
});

test('should accept correctly defined remark and rehype plugin options', async () => {
const userOptions = {
...DEFAULT_OPTIONS,
remarkPlugins: [remarkRehypePluginStub, {option1: '42'}],
remarkPlugins: [[markdownPluginsFunctionStub, {option1: '42'}]],
rehypePlugins: [
remarkRehypePluginStub,
[remarkRehypePluginStub, {option1: '42'}],
markdownPluginsObjectStub,
[markdownPluginsFunctionStub, {option1: '42'}],
],
};
const {value} = await PluginOptionSchema.validate(userOptions);
const {value, error} = await PluginOptionSchema.validate(userOptions);
expect(value).toEqual(userOptions);
expect(error).toBe(undefined);
});

test('should reject invalid remark plugin options', () => {
expect(() => {
normalizePluginOptions(PluginOptionSchema, {
remarkPlugins: [[{option1: '42'}, markdownPluginsFunctionStub]],
});
}).toThrowErrorMatchingInlineSnapshot(
`"\\"remarkPlugins[0]\\" does not match any of the allowed types"`,
);
});

test('should reject invalid rehype plugin options', () => {
expect(() => {
normalizePluginOptions(PluginOptionSchema, {
rehypePlugins: [
[
markdownPluginsFunctionStub,
{option1: '42'},
markdownPluginsFunctionStub,
],
],
});
}).toThrowErrorMatchingInlineSnapshot(
`"\\"rehypePlugins[0]\\" does not match any of the allowed types"`,
);
});

test('should reject bad path inputs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const PluginOptionSchema = Joi.object({
routeBasePath: Joi.string().allow('').default(DEFAULT_OPTIONS.routeBasePath),
homePageId: Joi.string().optional(),
include: Joi.array().items(Joi.string()).default(DEFAULT_OPTIONS.include),
sidebarPath: Joi.string().default(DEFAULT_OPTIONS.sidebarPath),
sidebarPath: Joi.string().allow('').default(DEFAULT_OPTIONS.sidebarPath),
docLayoutComponent: Joi.string().default(DEFAULT_OPTIONS.docLayoutComponent),
docItemComponent: Joi.string().default(DEFAULT_OPTIONS.docItemComponent),
remarkPlugins: RemarkPluginsSchema.default(DEFAULT_OPTIONS.remarkPlugins),
Expand Down
5 changes: 2 additions & 3 deletions packages/docusaurus-utils-validation/src/validationSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ export const PluginIdSchema = Joi.string()
const MarkdownPluginsSchema = Joi.array()
.items(
Joi.array()
// TODO, this allows [config,fn] too?
.items(Joi.function().required(), Joi.object().required())
.length(2),
.ordered(Joi.function().required(), Joi.object().required()),
Joi.function(),
Joi.object(),
)
.default([]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This completes the TODO for validating 'babel style' input and fixes the validation issue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ordered is really cool

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, looked for that but couldn't find it. Joi doc is a pain :D


Expand Down
3 changes: 1 addition & 2 deletions packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@
"webpack-bundle-analyzer": "^3.6.1",
"webpack-dev-server": "^3.11.0",
"webpack-merge": "^4.2.2",
"webpackbar": "^4.0.0",
"@docusaurus/utils-validation": "^2.0.0-alpha.61"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a duplicate dependency, the other one is on line 50

"webpackbar": "^4.0.0"
},
"peerDependencies": {
"react": "^16.8.4",
Expand Down