From 89038dea0ceb66ced2fd5c2e26332c3ebde52022 Mon Sep 17 00:00:00 2001 From: slorber Date: Wed, 3 Jun 2020 19:46:57 +0200 Subject: [PATCH] Fix non-strict validation error, allowing [] for string --- .../redirectValidation.test.ts.snap | 4 ++++ .../src/__tests__/collectRedirects.test.ts | 18 ++++++++++++++++++ .../src/__tests__/redirectValidation.test.ts | 14 ++++++++++++++ .../src/normalizePluginOptions.ts | 2 +- .../src/redirectValidation.ts | 11 ++++++----- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap b/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap index 3b129aee9e..5c42d34682 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/redirectValidation.test.ts.snap @@ -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"`; diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index 865ae37ae9..3b08d276b6 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -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( diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts index 035001e40e..32c009c945 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/redirectValidation.test.ts @@ -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(); }); }); diff --git a/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts b/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts index 507a32b71e..0c3cda9e80 100644 --- a/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts +++ b/packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts @@ -51,8 +51,8 @@ const UserOptionsSchema = Yup.object().shape({ 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( diff --git a/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts b/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts index b02d44a306..bd79ce60a1 100644 --- a/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts +++ b/packages/docusaurus-plugin-client-redirects/src/redirectValidation.ts @@ -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({ fromRoutePath: PathnameValidator.required(), @@ -25,7 +23,10 @@ const RedirectSchema = Yup.object({ 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(