mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
polish(v2): url-subpath config validation warning (#4977)
This commit is contained in:
parent
dfe32dac9e
commit
9fe79caadc
3 changed files with 51 additions and 5 deletions
|
@ -35,7 +35,7 @@ export const logValidationBugReportHint = (): void => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function printWarning(warning?: Joi.ValidationError) {
|
export function printWarning(warning?: Joi.ValidationError) {
|
||||||
if (warning) {
|
if (warning) {
|
||||||
const warningMessages = warning.details
|
const warningMessages = warning.details
|
||||||
.map(({message}) => message)
|
.map(({message}) => message)
|
||||||
|
|
|
@ -5,7 +5,11 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {DEFAULT_CONFIG, validateConfig} from '../configValidation';
|
import {
|
||||||
|
ConfigSchema,
|
||||||
|
DEFAULT_CONFIG,
|
||||||
|
validateConfig,
|
||||||
|
} from '../configValidation';
|
||||||
import {DocusaurusConfig} from '@docusaurus/types';
|
import {DocusaurusConfig} from '@docusaurus/types';
|
||||||
|
|
||||||
const baseConfig = {
|
const baseConfig = {
|
||||||
|
@ -197,3 +201,26 @@ describe('normalizeConfig', () => {
|
||||||
).toThrowErrorMatchingSnapshot();
|
).toThrowErrorMatchingSnapshot();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('config warnings', () => {
|
||||||
|
function getWarning(config: unknown) {
|
||||||
|
return ConfigSchema.validate(config).warning;
|
||||||
|
}
|
||||||
|
|
||||||
|
test('baseConfig has no warning', () => {
|
||||||
|
const warning = getWarning(baseConfig);
|
||||||
|
expect(warning).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('site url has warning when using subpath', () => {
|
||||||
|
const warning = getWarning({
|
||||||
|
...baseConfig,
|
||||||
|
url: 'https://mysite.com/someSubpath',
|
||||||
|
});
|
||||||
|
expect(warning).toBeDefined();
|
||||||
|
expect(warning?.details.length).toEqual(1);
|
||||||
|
expect(warning?.details[0].message).toMatchInlineSnapshot(
|
||||||
|
`"Docusaurus config validation warning. Field \\"url\\": the url is not supposed to contain a sub-path like '/someSubpath', please use the baseUrl field for sub-paths"`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -12,6 +12,7 @@ import {
|
||||||
logValidationBugReportHint,
|
logValidationBugReportHint,
|
||||||
isValidationDisabledEscapeHatch,
|
isValidationDisabledEscapeHatch,
|
||||||
URISchema,
|
URISchema,
|
||||||
|
printWarning,
|
||||||
} from '@docusaurus/utils-validation';
|
} from '@docusaurus/utils-validation';
|
||||||
|
|
||||||
const DEFAULT_I18N_LOCALE = 'en';
|
const DEFAULT_I18N_LOCALE = 'en';
|
||||||
|
@ -107,8 +108,20 @@ const I18N_CONFIG_SCHEMA = Joi.object<I18nConfig>({
|
||||||
.optional()
|
.optional()
|
||||||
.default(DEFAULT_I18N_CONFIG);
|
.default(DEFAULT_I18N_CONFIG);
|
||||||
|
|
||||||
|
const SiteUrlSchema = URISchema.required().custom(function (value, 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 (e) {}
|
||||||
|
return value;
|
||||||
|
}, 'siteUrlCustomValidation');
|
||||||
|
|
||||||
// TODO move to @docusaurus/utils-validation
|
// TODO move to @docusaurus/utils-validation
|
||||||
const ConfigSchema = Joi.object({
|
export const ConfigSchema = Joi.object({
|
||||||
baseUrl: Joi.string()
|
baseUrl: Joi.string()
|
||||||
.required()
|
.required()
|
||||||
.regex(new RegExp('/$', 'm'))
|
.regex(new RegExp('/$', 'm'))
|
||||||
|
@ -116,7 +129,7 @@ const ConfigSchema = Joi.object({
|
||||||
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
|
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
|
||||||
favicon: Joi.string().required(),
|
favicon: Joi.string().required(),
|
||||||
title: Joi.string().required(),
|
title: Joi.string().required(),
|
||||||
url: URISchema.required(),
|
url: SiteUrlSchema,
|
||||||
trailingSlash: Joi.boolean(), // No default value! undefined = retrocompatible legacy behavior!
|
trailingSlash: Joi.boolean(), // No default value! undefined = retrocompatible legacy behavior!
|
||||||
i18n: I18N_CONFIG_SCHEMA,
|
i18n: I18N_CONFIG_SCHEMA,
|
||||||
onBrokenLinks: Joi.string()
|
onBrokenLinks: Joi.string()
|
||||||
|
@ -163,15 +176,21 @@ const ConfigSchema = Joi.object({
|
||||||
.try(Joi.string().equal('babel'), Joi.function())
|
.try(Joi.string().equal('babel'), Joi.function())
|
||||||
.optional(),
|
.optional(),
|
||||||
}).optional(),
|
}).optional(),
|
||||||
|
}).messages({
|
||||||
|
'docusaurus.configValidationWarning':
|
||||||
|
'Docusaurus config validation warning. Field {#label}: {#warningMessage}',
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO move to @docusaurus/utils-validation
|
// TODO move to @docusaurus/utils-validation
|
||||||
export function validateConfig(
|
export function validateConfig(
|
||||||
config: Partial<DocusaurusConfig>,
|
config: Partial<DocusaurusConfig>,
|
||||||
): DocusaurusConfig {
|
): DocusaurusConfig {
|
||||||
const {error, value} = ConfigSchema.validate(config, {
|
const {error, warning, value} = ConfigSchema.validate(config, {
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
printWarning(warning);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
logValidationBugReportHint();
|
logValidationBugReportHint();
|
||||||
if (isValidationDisabledEscapeHatch) {
|
if (isValidationDisabledEscapeHatch) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue