From 575c0c48a670225479fc3e99786d7eb016c1896b Mon Sep 17 00:00:00 2001 From: forgeRW <20483211+forgeRW@users.noreply.github.com> Date: Sat, 8 Oct 2022 15:37:00 -0400 Subject: [PATCH 1/2] fix(core): throw error for invalid URL in config file (#8116) * fix: throw error for site URL in sub-path format * Also update unit tests to check error is thrown --- .../server/__tests__/configValidation.test.ts | 33 ++++++++----------- .../docusaurus/src/server/configValidation.ts | 6 ++-- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/packages/docusaurus/src/server/__tests__/configValidation.test.ts b/packages/docusaurus/src/server/__tests__/configValidation.test.ts index 9297df2db422..4bc3e9246152 100644 --- a/packages/docusaurus/src/server/__tests__/configValidation.test.ts +++ b/packages/docusaurus/src/server/__tests__/configValidation.test.ts @@ -5,7 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import {jest} from '@jest/globals'; import { ConfigSchema, DEFAULT_CONFIG, @@ -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. + " + `); }); it('throws for non-string base URLs', () => { @@ -392,7 +386,7 @@ describe('normalizeConfig', () => { }); }); -describe('config warnings', () => { +describe('config warning and error', () => { function getWarning(config: unknown) { return ConfigSchema.validate(config).warning; } @@ -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.', ); }); }); diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index ff8da9bd96eb..9496954d8bc8 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -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'); } } catch { return helpers.error('any.invalid'); @@ -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 From 66fee9394a6df09937a56232a32852efbec53359 Mon Sep 17 00:00:00 2001 From: forgeRW <20483211+forgeRW@users.noreply.github.com> Date: Wed, 12 Oct 2022 22:41:02 -0400 Subject: [PATCH 2/2] fix(core): throw error for invalid URL with sub-path in config file (#8192) * fix: throw custom error for site URL with sub-path * Also update unit test to check error is thrown --- .../src/server/__tests__/configValidation.test.ts | 13 ++----------- packages/docusaurus/src/server/configValidation.ts | 4 ++-- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/docusaurus/src/server/__tests__/configValidation.test.ts b/packages/docusaurus/src/server/__tests__/configValidation.test.ts index 4bc3e9246152..4d4688e6968a 100644 --- a/packages/docusaurus/src/server/__tests__/configValidation.test.ts +++ b/packages/docusaurus/src/server/__tests__/configValidation.test.ts @@ -109,21 +109,12 @@ describe('normalizeConfig', () => { `); }); - it('normalizes various URLs', () => { + 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/', - }), - ).toThrowErrorMatchingInlineSnapshot(` - "The url is not supposed to contain a sub-path like "". Please use the baseUrl field for sub-paths. - " - `); }); it('throws for non-string base URLs', () => { @@ -403,7 +394,7 @@ describe('config warning and error', () => { }); 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.', + 'The url is not supposed to contain a sub-path like "/someSubpath". Please use the baseUrl field for sub-paths.', ); }); }); diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index 9496954d8bc8..95af3bf34cbc 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -158,7 +158,7 @@ const SiteUrlSchema = Joi.string() try { const {pathname} = new URL(value); if (pathname !== '/') { - return helpers.error('docusaurus.configValidationWarning'); + return helpers.error('docusaurus.subPathError', {pathname}); } } catch { return helpers.error('any.invalid'); @@ -168,7 +168,7 @@ 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': + 'docusaurus.subPathError': 'The url is not supposed to contain a sub-path like "{#pathname}". Please use the baseUrl field for sub-paths.', });