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(validation): improve error messages for a few schemas #6997

Merged
merged 3 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -150,9 +150,11 @@ describe('normalizeDocsPluginOptions', () => {
testValidate({
remarkPlugins: [[{option1: '42'}, markdownPluginsFunctionStub]],
}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"remarkPlugins[0]\\" does not match any of the allowed types"`,
);
).toThrowErrorMatchingInlineSnapshot(`
"\\"remarkPlugins[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`);
});

it('rejects invalid rehype plugin options', () => {
Expand All @@ -166,9 +168,11 @@ describe('normalizeDocsPluginOptions', () => {
],
],
}),
).toThrowErrorMatchingInlineSnapshot(
`"\\"rehypePlugins[0]\\" does not match any of the allowed types"`,
);
).toThrowErrorMatchingInlineSnapshot(`
"\\"rehypePlugins[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`);
});

it('rejects bad path inputs', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,71 @@ exports[`validation schemas pluginIdSchema: for value=null 1`] = `"\\"value\\" m

exports[`validation schemas pluginIdSchema: for value=true 1`] = `"\\"value\\" must be a string"`;

exports[`validation schemas rehypePluginsSchema: for value=[[]] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas rehypePluginsSchema: for value=[[null,null]] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas rehypePluginsSchema: for value=[3] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas rehypePluginsSchema: for value=[false] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas rehypePluginsSchema: for value=[null] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;
exports[`validation schemas rehypePluginsSchema: for value=[[]] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas rehypePluginsSchema: for value=[[null,null]] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas rehypePluginsSchema: for value=[3] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas rehypePluginsSchema: for value=[false] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas rehypePluginsSchema: for value=[null] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas rehypePluginsSchema: for value=3 1`] = `"\\"value\\" must be an array"`;

exports[`validation schemas rehypePluginsSchema: for value=false 1`] = `"\\"value\\" must be an array"`;

exports[`validation schemas rehypePluginsSchema: for value=null 1`] = `"\\"value\\" must be an array"`;

exports[`validation schemas remarkPluginsSchema: for value=[[]] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas remarkPluginsSchema: for value=[[null,null]] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas remarkPluginsSchema: for value=[3] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas remarkPluginsSchema: for value=[false] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;

exports[`validation schemas remarkPluginsSchema: for value=[null] 1`] = `"\\"[0]\\" does not match any of the allowed types"`;
exports[`validation schemas remarkPluginsSchema: for value=[[]] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas remarkPluginsSchema: for value=[[null,null]] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas remarkPluginsSchema: for value=[3] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas remarkPluginsSchema: for value=[false] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas remarkPluginsSchema: for value=[null] 1`] = `
"\\"[0]\\" does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require(\\"rehype-katex\\"), { strict: false }]\`, or
- A simple module, like \`require(\\"remark-math\\")\`"
`;

exports[`validation schemas remarkPluginsSchema: for value=3 1`] = `"\\"value\\" must be an array"`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const MarkdownPluginsSchema = Joi.array()
Joi.function(),
Joi.object(),
)
.messages({
'array.includes': `{#label} does not look like a valid MDX plugin config. A plugin config entry should be one of:
- A tuple, like \`[require("rehype-katex"), \\{ strict: false \\}]\`, or
- A simple module, like \`require("remark-math")\``,
})
.default([]);

export const RemarkPluginsSchema = MarkdownPluginsSchema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,3 @@ exports[`normalizeConfig throws error for unknown field 1`] = `
If you still want these fields to be in your configuration, put them in the \\"customFields\\" field.
See https://docusaurus.io/docs/docusaurus.config.js/#customfields"
`;

exports[`normalizeConfig throws error if css doesn't have href 1`] = `
"\\"stylesheets[1]\\" does not match any of the allowed types
"
`;

exports[`normalizeConfig throws error if presets is not array 1`] = `
"\\"presets\\" must be an array
"
`;

exports[`normalizeConfig throws error if scripts doesn't have src 1`] = `
"\\"scripts[1]\\" does not match any of the allowed types
"
`;

exports[`normalizeConfig throws error if themes is not array 1`] = `
"\\"themes\\" must be an array
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,56 @@ describe('normalizeConfig', () => {
normalizeConfig({
themes: {},
});
}).toThrowErrorMatchingSnapshot();
}).toThrowErrorMatchingInlineSnapshot(`
"\\"themes\\" must be an array
"
`);
});

it('throws error if presets is not array', () => {
expect(() => {
normalizeConfig({
presets: {},
});
}).toThrowErrorMatchingSnapshot();
}).toThrowErrorMatchingInlineSnapshot(`
"\\"presets\\" must be an array
"
`);
});

it('throws error if presets looks invalid', () => {
expect(() => {
normalizeConfig({
presets: [() => {}],
});
}).toThrowErrorMatchingInlineSnapshot(`
"\\"presets[0]\\" does not look like a valid preset config. A preset config entry should be one of:
- A tuple of [presetName, options], like \`[\\"classic\\", { blog: false }]\`, or
- A simple string, like \`\\"classic\\"\`
"
`);
});

it("throws error if scripts doesn't have src", () => {
expect(() => {
normalizeConfig({
scripts: ['https://some.com', {}],
});
}).toThrowErrorMatchingSnapshot();
}).toThrowErrorMatchingInlineSnapshot(`
"\\"scripts[1]\\" is invalid. A script must be a plain string (the src), or an object with at least a \\"src\\" property.
"
`);
});

it("throws error if css doesn't have href", () => {
expect(() => {
normalizeConfig({
stylesheets: ['https://somescript.com', {type: 'text/css'}],
});
}).toThrowErrorMatchingSnapshot();
}).toThrowErrorMatchingInlineSnapshot(`
"\\"stylesheets[1]\\" is invalid. A stylesheet must be a plain string (the href), or an object with at least a \\"href\\" property.
"
`);
});

it('throws error for required fields', () => {
Expand Down
24 changes: 20 additions & 4 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,18 @@ const PluginSchema = createPluginSchema(false);

const ThemeSchema = createPluginSchema(true);

const PresetSchema = Joi.alternatives().try(
Joi.string(),
Joi.array().items(Joi.string().required(), Joi.object().required()).length(2),
);
const PresetSchema = Joi.alternatives()
.try(
Joi.string(),
Joi.array()
.items(Joi.string().required(), Joi.object().required())
.length(2),
)
.messages({
'alternatives.types': `{#label} does not look like a valid preset config. A preset config entry should be one of:
- A tuple of [presetName, options], like \`["classic", \\{ blog: false \\}]\`, or
- A simple string, like \`"classic"\``,
});

const LocaleConfigSchema = Joi.object({
label: Joi.string(),
Expand Down Expand Up @@ -190,6 +198,10 @@ export const ConfigSchema = Joi.object({
// See https://github.com/facebook/docusaurus/issues/3378
.unknown(),
)
.messages({
'array.includes':
'{#label} is invalid. A script must be a plain string (the src), or an object with at least a "src" property.',
})
.default(DEFAULT_CONFIG.scripts),
ssrTemplate: Joi.string(),
stylesheets: Joi.array()
Expand All @@ -200,6 +212,10 @@ export const ConfigSchema = Joi.object({
type: Joi.string(),
}).unknown(),
)
.messages({
'array.includes':
'{#label} is invalid. A stylesheet must be a plain string (the href), or an object with at least a "href" property.',
})
.default(DEFAULT_CONFIG.stylesheets),
clientModules: Joi.array()
.items(Joi.string())
Expand Down