mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-31 15:42:48 +00:00
Fix non-strict validation error, allowing [] for string
This commit is contained in:
parent
00a79f69c7
commit
89038dea0c
|
|
@ -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"`;
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Reference in New Issue