Skip to content

Commit

Permalink
normalize routeBasePath
Browse files Browse the repository at this point in the history
allow '' for baseUrl + routeBasePath
  • Loading branch information
slorber committed Dec 8, 2022
1 parent 8c065d3 commit 1eb9827
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ function createTestHelpers({
schema: Joi.Schema;
defaultValue?: unknown;
}) {
function testOK(value: unknown) {
expect(Joi.attempt(value, schema)).toEqual(value ?? defaultValue);
function testOK(value: unknown, options?: {normalizedValue?: unknown}) {
const expectedValue = options?.normalizedValue ?? value ?? defaultValue;
expect(Joi.attempt(value, schema)).toEqual(expectedValue);
}

function testFail(value: unknown) {
Expand Down Expand Up @@ -172,13 +173,18 @@ describe('validation schemas', () => {
it('routeBasePathSchema', () => {
const {testFail, testOK} = createTestHelpers({
schema: RouteBasePathSchema,
defaultValue: '',
defaultValue: undefined,
});

testOK('');
testOK('', {normalizedValue: '/'});
testOK('/');
testOK('/foo');
testOK('blog');
testOK('/foo', {normalizedValue: '/foo'});
testOK('foo', {normalizedValue: '/foo'});
testOK('blog', {normalizedValue: '/blog'});
testOK('blog/', {normalizedValue: '/blog/'});
testOK('prefix/blog', {normalizedValue: '/prefix/blog'});
testOK('prefix/blog/', {normalizedValue: '/prefix/blog/'});
testOK('/prefix/blog', {normalizedValue: '/prefix/blog'});
testOK(undefined);

testFail(3);
Expand Down
28 changes: 25 additions & 3 deletions packages/docusaurus-utils-validation/src/validationSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
* LICENSE file in the root directory of this source tree.
*/

import {isValidPathname, DEFAULT_PLUGIN_ID, type Tag} from '@docusaurus/utils';
import {
isValidPathname,
DEFAULT_PLUGIN_ID,
type Tag,
addLeadingSlash,
} from '@docusaurus/utils';
import Joi from './Joi';
import {JoiFrontMatter} from './JoiFrontMatter';

Expand Down Expand Up @@ -96,8 +101,25 @@ export const PathnameSchema = Joi.string()
'{{#label}} is not a valid pathname. Pathname should start with slash and not contain any domain or query string.',
);

// joi empty string not allowed by default. so using empty('') for custom use
export const RouteBasePathSchema = Joi.string().empty('').default('');
// Normalized schema for url path segments: baseUrl + routeBasePath...
// Note we only add a leading slash
// we don't always want to enforce a trailing slash on urls such as /docs
//
// Examples:
// '' => '/'
// 'docs' => '/docs'
// '/docs' => '/docs'
// 'docs/' => '/docs'
// 'prefix/docs' => '/prefix/docs'
// TODO tighter validation: not all strings are valid path segments
export const RouteBasePathSchema = Joi
// Weird Joi trick needed, otherwise value '' is not normalized...
.alternatives()
.try(Joi.string().required().allow(''))
.custom((value: string) =>
// /!\ do not add trailing slash here
addLeadingSlash(value),
);

const FrontMatterTagSchema = JoiFrontMatter.alternatives()
.try(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ describe('normalizeConfig', () => {
});

it('normalizes various base URLs', () => {
expect(
normalizeConfig({
baseUrl: '',
}).baseUrl,
).toBe('/');
expect(
normalizeConfig({
baseUrl: 'noSlash',
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus/src/server/configValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ const SiteUrlSchema = Joi.string()

// TODO move to @docusaurus/utils-validation
export const ConfigSchema = Joi.object<DocusaurusConfig>({
baseUrl: Joi.string()
baseUrl: Joi
// Weird Joi trick needed, otherwise value '' is not normalized...
.alternatives()
.try(Joi.string().required().allow(''))
.required()
.custom((value: string) => addLeadingSlash(addTrailingSlash(value))),
baseUrlIssueBanner: Joi.boolean().default(DEFAULT_CONFIG.baseUrlIssueBanner),
Expand Down

0 comments on commit 1eb9827

Please sign in to comment.