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(core): throw error for invalid URL in config file #8192

Merged
merged 2 commits into from Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 13 additions & 20 deletions packages/docusaurus/src/server/__tests__/configValidation.test.ts
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 @@ -111,25 +110,20 @@ describe('normalizeConfig', () => {
});

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

expect(
normalizeConfig({
url: 'https://mysite.com/',
}).url,
).toBe('https://mysite.com');
expect(
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."`,
);
}),
).toThrowErrorMatchingInlineSnapshot(`
"The url is not supposed to contain a sub-path like "". Please use the baseUrl field for sub-paths.
"
`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure we need this anymore, at least not in this test group, because here it's not testing URL normalization but URL rejection so this feels weird

});

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

describe('config warnings', () => {
describe('config warning and error', () => {
function getWarning(config: unknown) {
return ConfigSchema.validate(config).warning;
}
Expand All @@ -402,15 +396,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 "". Please use the baseUrl field for sub-paths.',
);
});
});
6 changes: 3 additions & 3 deletions packages/docusaurus/src/server/configValidation.ts
Expand Up @@ -158,9 +158,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.configValidationWarning');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should use configValidationWarning for something that is an error. Also pathname used to be interpolated but it's not anymore

Unfortunately I can't really guide you to the correct solution: Joi is really a pain to do anything basic such as returning a custom error message 😅

}
} catch {
return helpers.error('any.invalid');
Expand All @@ -170,6 +168,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.configValidationWarning':
'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