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 <sidachen2003@gmail.com>
This commit is contained in:
forgeRW 2022-10-02 11:31:14 -04:00 committed by GitHub
parent eabca78c38
commit 53dbbe36f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 12 deletions

View file

@ -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".
"
`);
});

View file

@ -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';
@ -152,17 +152,25 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
.optional()
.default(DEFAULT_I18N_CONFIG);
const SiteUrlSchema = URISchema.required().custom((value: string, helpers) => {
const SiteUrlSchema = Joi.string()
.required()
.custom((value: string, helpers) => {
try {
const {pathname} = new URL(String(value));
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 {}
} catch {
return helpers.error('any.invalid');
}
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>({