Fix non-strict validation error, allowing [] for string

This commit is contained in:
slorber 2020-06-03 19:46:57 +02:00
parent 00a79f69c7
commit 89038dea0c
5 changed files with 43 additions and 6 deletions

View File

@ -5,3 +5,7 @@ exports[`validateRedirect throw for bad redirects 1`] = `"{\\"fromRoutePath\\":\
exports[`validateRedirect throw for bad redirects 2`] = `"{\\"fromRoutePath\\":\\"/fromSomePath\\",\\"toRoutePath\\":\\"https://fb.com/toSomePath\\"} => Validation error: toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`;
exports[`validateRedirect throw for bad redirects 3`] = `"{\\"fromRoutePath\\":\\"/fromSomePath\\",\\"toRoutePath\\":\\"/toSomePath?queryString=xyz\\"} => Validation error: toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`;
exports[`validateRedirect throw for bad redirects 4`] = `"{\\"fromRoutePath\\":null,\\"toRoutePath\\":\\"/toSomePath?queryString=xyz\\"} => Validation error: toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`;
exports[`validateRedirect throw for bad redirects 5`] = `"{\\"fromRoutePath\\":[\\"heyho\\"],\\"toRoutePath\\":\\"/toSomePath?queryString=xyz\\"} => Validation error: toRoutePath is not a valid pathname. Pathname should start with / and not contain any domain or query string"`;

View File

@ -201,6 +201,24 @@ describe('collectRedirects', () => {
).toThrowErrorMatchingSnapshot();
});
test('should throw if redirect creator creates array of array redirect', () => {
expect(() =>
collectRedirects(
createTestPluginContext(
{
createRedirects: (routePath) => {
if (routePath === '/') {
return [[`/fromPath`]] as any;
}
return;
},
},
['/'],
),
),
).toThrowErrorMatchingSnapshot();
});
test('should filter unwanted redirects', () => {
expect(
collectRedirects(

View File

@ -48,5 +48,19 @@ describe('validateRedirect', () => {
toRoutePath: '/toSomePath?queryString=xyz',
}),
).toThrowErrorMatchingSnapshot();
expect(() =>
validateRedirect({
fromRoutePath: null as any,
toRoutePath: '/toSomePath?queryString=xyz',
}),
).toThrowErrorMatchingSnapshot();
expect(() =>
validateRedirect({
fromRoutePath: ['heyho'] as any,
toRoutePath: '/toSomePath?queryString=xyz',
}),
).toThrowErrorMatchingSnapshot();
});
});

View File

@ -51,8 +51,8 @@ const UserOptionsSchema = Yup.object().shape<UserPluginOptions>({
function validateUserOptions(userOptions: UserPluginOptions) {
try {
UserOptionsSchema.validateSync(userOptions, {
abortEarly: true, // Needed otherwise the message is just "2 errors occurred"
strict: true,
abortEarly: true,
});
} catch (e) {
throw new Error(

View File

@ -9,14 +9,12 @@ import {isValidPathname} from '@docusaurus/utils';
import * as Yup from 'yup';
import {RedirectMetadata} from './types';
const validPathnameTest: Yup.TestOptions = {
export const PathnameValidator = Yup.string().test({
name: 'isValidPathname',
message:
'${path} is not a valid pathname. Pathname should start with / and not contain any domain or query string',
test: isValidPathname,
};
export const PathnameValidator = Yup.string().test(validPathnameTest);
});
const RedirectSchema = Yup.object<RedirectMetadata>({
fromRoutePath: PathnameValidator.required(),
@ -25,7 +23,10 @@ const RedirectSchema = Yup.object<RedirectMetadata>({
export function validateRedirect(redirect: RedirectMetadata) {
try {
RedirectSchema.validateSync(redirect);
RedirectSchema.validateSync(redirect, {
strict: true,
abortEarly: true,
});
} catch (e) {
// Tells the user which redirect is the problem!
throw new Error(