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 #8159

Merged
merged 3 commits into from Oct 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
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', {
Copy link
Collaborator

@slorber slorber Oct 5, 2022

Choose a reason for hiding this comment

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

Thanks

in case you want to follow-up, I think we should make this other case an error instead of a warning

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I'll work on this and make a new PR.

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