mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 02:37:59 +02:00
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:
parent
eabca78c38
commit
53dbbe36f2
2 changed files with 31 additions and 12 deletions
|
@ -94,7 +94,18 @@ describe('normalizeConfig', () => {
|
||||||
url: 1,
|
url: 1,
|
||||||
}),
|
}),
|
||||||
).toThrowErrorMatchingInlineSnapshot(`
|
).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".
|
||||||
"
|
"
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {
|
||||||
addTrailingSlash,
|
addTrailingSlash,
|
||||||
removeTrailingSlash,
|
removeTrailingSlash,
|
||||||
} from '@docusaurus/utils';
|
} 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';
|
import type {DocusaurusConfig, I18nConfig} from '@docusaurus/types';
|
||||||
|
|
||||||
const DEFAULT_I18N_LOCALE = 'en';
|
const DEFAULT_I18N_LOCALE = 'en';
|
||||||
|
@ -152,17 +152,25 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
|
||||||
.optional()
|
.optional()
|
||||||
.default(DEFAULT_I18N_CONFIG);
|
.default(DEFAULT_I18N_CONFIG);
|
||||||
|
|
||||||
const SiteUrlSchema = URISchema.required().custom((value: string, helpers) => {
|
const SiteUrlSchema = Joi.string()
|
||||||
|
.required()
|
||||||
|
.custom((value: string, helpers) => {
|
||||||
try {
|
try {
|
||||||
const {pathname} = new URL(String(value));
|
const {pathname} = new URL(value);
|
||||||
if (pathname !== '/') {
|
if (pathname !== '/') {
|
||||||
helpers.warn('docusaurus.configValidationWarning', {
|
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.`,
|
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);
|
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
|
// TODO move to @docusaurus/utils-validation
|
||||||
export const ConfigSchema = Joi.object<DocusaurusConfig>({
|
export const ConfigSchema = Joi.object<DocusaurusConfig>({
|
||||||
|
|
Loading…
Add table
Reference in a new issue