fix(docusaurus-utils-validation): baseUrl + routeBasePath: allow empty string, normalized as "/" (#8258)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
fix https://github.com/facebook/docusaurus/issues/8254
This commit is contained in:
Dongjoon Lee 2022-12-09 00:54:32 +09:00 committed by GitHub
parent 85d0b5674f
commit e553ce7e59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 92 additions and 17 deletions

View file

@ -14,6 +14,7 @@ import {
PluginIdSchema,
URISchema,
PathnameSchema,
RouteBasePathSchema,
ContentVisibilitySchema,
} from '../validationSchemas';
@ -24,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) {
@ -168,6 +170,29 @@ describe('validation schemas', () => {
testFail('https://github.com/foo');
});
it('routeBasePathSchema', () => {
const {testFail, testOK} = createTestHelpers({
schema: RouteBasePathSchema,
defaultValue: undefined,
});
testOK('', {normalizedValue: '/'});
testOK('/');
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);
testFail([]);
testFail(null);
testFail({});
});
it('contentVisibilitySchema', () => {
const {testFail, testOK} = createTestHelpers({
schema: ContentVisibilitySchema,