Skip to content

Commit

Permalink
fix(core): throw error for invalid URL in config file (#8192)
Browse files Browse the repository at this point in the history
  • Loading branch information
forgeRW authored and slorber committed Oct 28, 2022
1 parent 6f5e669 commit a990a5e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
32 changes: 8 additions & 24 deletions packages/docusaurus/src/server/__tests__/configValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import {jest} from '@jest/globals';
import {
ConfigSchema,
DEFAULT_CONFIG,
Expand Down Expand Up @@ -110,26 +109,12 @@ describe('normalizeConfig', () => {
`);
});

it('normalizes various URLs', () => {
const consoleMock = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});

it('normalizes URL', () => {
expect(
normalizeConfig({
url: 'https://mysite.com/',
}).url,
).toBe('https://mysite.com');
expect(
normalizeConfig({
// This shouldn't happen
url: 'https://mysite.com/foo/',
}).url,
).toBe('https://mysite.com/foo');

expect(consoleMock.mock.calls[0][0]).toMatchInlineSnapshot(
`"[WARNING] Docusaurus config validation warning. Field "url": The url is not supposed to contain a sub-path like '/foo/'. Please use the baseUrl field for sub-paths."`,
);
});

it('throws for non-string base URLs', () => {
Expand Down Expand Up @@ -460,7 +445,7 @@ describe('normalizeConfig', () => {
});
});

describe('config warnings', () => {
describe('config warning and error', () => {
function getWarning(config: unknown) {
return ConfigSchema.validate(config).warning;
}
Expand All @@ -470,15 +455,14 @@ describe('config warnings', () => {
expect(warning).toBeUndefined();
});

it('site url has warning when using subpath', () => {
const warning = getWarning({
it('site url fails validation when using subpath', () => {
const {error} = ConfigSchema.validate({
...baseConfig,
url: 'https://mysite.com/someSubpath',
})!;
expect(warning).toBeDefined();
expect(warning.details).toHaveLength(1);
expect(warning.details[0]!.message).toMatchInlineSnapshot(
`"Docusaurus config validation warning. Field "url": The url is not supposed to contain a sub-path like '/someSubpath'. Please use the baseUrl field for sub-paths."`,
});
expect(error).toBeDefined();
expect(error.message).toBe(
'The url is not supposed to contain a sub-path like "/someSubpath". Please use the baseUrl field for sub-paths.',
);
});
});
6 changes: 3 additions & 3 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ const SiteUrlSchema = Joi.string()
try {
const {pathname} = new URL(value);
if (pathname !== '/') {
helpers.warn('docusaurus.configValidationWarning', {
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
});
return helpers.error('docusaurus.subPathError', {pathname});
}
} catch {
return helpers.error('any.invalid');
Expand All @@ -172,6 +170,8 @@ const SiteUrlSchema = Joi.string()
.messages({
'any.invalid':
'"{#value}" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".',
'docusaurus.subPathError':
'The url is not supposed to contain a sub-path like "{#pathname}". Please use the baseUrl field for sub-paths.',
});

// TODO move to @docusaurus/utils-validation
Expand Down

0 comments on commit a990a5e

Please sign in to comment.