Skip to content

Commit da01da1

Browse files
forgeRWJosh-Cena
authored andcommitted
fix(core): throw error for invalid URL in config file (#8159)
* fix: throw error for invalid URL in Docusaurus config file * Also add unit test to check error is thrown * fix: perform error check for invalid URL to configValidation.ts * Throw error for invalid URL in Docusaurus config file * Perform error check in configValidation.ts * Undo error check in createSitemap.ts * Better message Co-authored-by: Joshua Chen <[email protected]>
1 parent 6d405a6 commit da01da1

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

packages/docusaurus/src/server/__tests__/configValidation.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,18 @@ describe('normalizeConfig', () => {
9494
url: 1,
9595
}),
9696
).toThrowErrorMatchingInlineSnapshot(`
97-
""url" contains an invalid value
97+
""url" must be a string
98+
"
99+
`);
100+
});
101+
102+
it('throws for invalid URL', () => {
103+
expect(() =>
104+
normalizeConfig({
105+
url: 'mysite.com',
106+
}),
107+
).toThrowErrorMatchingInlineSnapshot(`
108+
""mysite.com" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".
98109
"
99110
`);
100111
});

packages/docusaurus/src/server/configValidation.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
addTrailingSlash,
1313
removeTrailingSlash,
1414
} from '@docusaurus/utils';
15-
import {Joi, URISchema, printWarning} from '@docusaurus/utils-validation';
15+
import {Joi, printWarning} from '@docusaurus/utils-validation';
1616
import type {DocusaurusConfig, I18nConfig} from '@docusaurus/types';
1717

1818
const DEFAULT_I18N_LOCALE = 'en';
@@ -152,17 +152,25 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
152152
.optional()
153153
.default(DEFAULT_I18N_CONFIG);
154154

155-
const SiteUrlSchema = URISchema.required().custom((value: string, helpers) => {
156-
try {
157-
const {pathname} = new URL(String(value));
158-
if (pathname !== '/') {
159-
helpers.warn('docusaurus.configValidationWarning', {
160-
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
161-
});
155+
const SiteUrlSchema = Joi.string()
156+
.required()
157+
.custom((value: string, helpers) => {
158+
try {
159+
const {pathname} = new URL(value);
160+
if (pathname !== '/') {
161+
helpers.warn('docusaurus.configValidationWarning', {
162+
warningMessage: `The url is not supposed to contain a sub-path like '${pathname}'. Please use the baseUrl field for sub-paths.`,
163+
});
164+
}
165+
} catch {
166+
return helpers.error('any.invalid');
162167
}
163-
} catch {}
164-
return removeTrailingSlash(value);
165-
});
168+
return removeTrailingSlash(value);
169+
})
170+
.messages({
171+
'any.invalid':
172+
'"{#value}" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".',
173+
});
166174

167175
// TODO move to @docusaurus/utils-validation
168176
export const ConfigSchema = Joi.object<DocusaurusConfig>({

0 commit comments

Comments
 (0)