fix(core): throw error for invalid URL in config file (#8192)

This commit is contained in:
forgeRW 2022-10-13 05:39:57 -04:00 committed by GitHub
parent 84e6dd2473
commit 2372335532
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 27 deletions

View file

@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import {jest} from '@jest/globals';
import { import {
ConfigSchema, ConfigSchema,
DEFAULT_CONFIG, DEFAULT_CONFIG,
@ -110,26 +109,12 @@ describe('normalizeConfig', () => {
`); `);
}); });
it('normalizes various URLs', () => { it('normalizes URL', () => {
const consoleMock = jest
.spyOn(console, 'warn')
.mockImplementation(() => {});
expect( expect(
normalizeConfig({ normalizeConfig({
url: 'https://mysite.com/', url: 'https://mysite.com/',
}).url, }).url,
).toBe('https://mysite.com'); ).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', () => { it('throws for non-string base URLs', () => {
@ -460,7 +445,7 @@ describe('normalizeConfig', () => {
}); });
}); });
describe('config warnings', () => { describe('config warning and error', () => {
function getWarning(config: unknown) { function getWarning(config: unknown) {
return ConfigSchema.validate(config).warning; return ConfigSchema.validate(config).warning;
} }
@ -470,15 +455,14 @@ describe('config warnings', () => {
expect(warning).toBeUndefined(); expect(warning).toBeUndefined();
}); });
it('site url has warning when using subpath', () => { it('site url fails validation when using subpath', () => {
const warning = getWarning({ const {error} = ConfigSchema.validate({
...baseConfig, ...baseConfig,
url: 'https://mysite.com/someSubpath', url: 'https://mysite.com/someSubpath',
})!; });
expect(warning).toBeDefined(); expect(error).toBeDefined();
expect(warning.details).toHaveLength(1); expect(error.message).toBe(
expect(warning.details[0]!.message).toMatchInlineSnapshot( 'The url is not supposed to contain a sub-path like "/someSubpath". Please use the baseUrl field for sub-paths.',
`"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."`,
); );
}); });
}); });

View file

@ -160,9 +160,7 @@ const SiteUrlSchema = Joi.string()
try { try {
const {pathname} = new URL(value); const {pathname} = new URL(value);
if (pathname !== '/') { if (pathname !== '/') {
helpers.warn('docusaurus.configValidationWarning', { return helpers.error('docusaurus.subPathError', {pathname});
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
});
} }
} catch { } catch {
return helpers.error('any.invalid'); return helpers.error('any.invalid');
@ -172,6 +170,8 @@ const SiteUrlSchema = Joi.string()
.messages({ .messages({
'any.invalid': 'any.invalid':
'"{#value}" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".', '"{#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 // TODO move to @docusaurus/utils-validation