Skip to content

Commit

Permalink
fix(core): throw error for invalid URL in config file (#8159)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
forgeRW and Josh-Cena authored Oct 2, 2022
1 parent eabca78 commit 53dbbe3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ describe('normalizeConfig', () => {
url: 1,
}),
).toThrowErrorMatchingInlineSnapshot(`
""url" contains an invalid value
""url" must be a string
"
`);
});

it('throws for invalid URL', () => {
expect(() =>
normalizeConfig({
url: 'mysite.com',
}),
).toThrowErrorMatchingInlineSnapshot(`
""mysite.com" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".
"
`);
});
Expand Down
30 changes: 19 additions & 11 deletions packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
addTrailingSlash,
removeTrailingSlash,
} from '@docusaurus/utils';
import {Joi, URISchema, printWarning} from '@docusaurus/utils-validation';
import {Joi, printWarning} from '@docusaurus/utils-validation';
import type {DocusaurusConfig, I18nConfig} from '@docusaurus/types';

const DEFAULT_I18N_LOCALE = 'en';
Expand Down Expand Up @@ -152,17 +152,25 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
.optional()
.default(DEFAULT_I18N_CONFIG);

const SiteUrlSchema = URISchema.required().custom((value: string, helpers) => {
try {
const {pathname} = new URL(String(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.`,
});
const SiteUrlSchema = Joi.string()
.required()
.custom((value: string, helpers) => {
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.`,
});
}
} catch {
return helpers.error('any.invalid');
}
} catch {}
return removeTrailingSlash(value);
});
return removeTrailingSlash(value);
})
.messages({
'any.invalid':
'"{#value}" does not look like a valid URL. Make sure it has a protocol; for example, "https://example.com".',
});

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

0 comments on commit 53dbbe3

Please sign in to comment.