-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(client-redirects): migrate validation to validateOptions lif…
…ecycle (#6924)
- Loading branch information
Showing
8 changed files
with
86 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
...s-plugin-client-redirects/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
packages/docusaurus-plugin-client-redirects/src/__tests__/__snapshots__/options.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`normalizePluginOptions rejects bad createRedirects user inputs 1`] = `"\\"createRedirects\\" must be of type function"`; | ||
|
||
exports[`normalizePluginOptions rejects bad fromExtensions user inputs 1`] = `"\\"fromExtensions[0]\\" contains an invalid value"`; | ||
|
||
exports[`normalizePluginOptions rejects bad toExtensions user inputs 1`] = `"\\"toExtensions[0]\\" contains an invalid value"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 0 additions & 59 deletions
59
packages/docusaurus-plugin-client-redirects/src/normalizePluginOptions.ts
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
packages/docusaurus-plugin-client-redirects/src/options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import type { | ||
PluginOptions, | ||
RedirectOption, | ||
} from '@docusaurus/plugin-client-redirects'; | ||
import type { | ||
OptionValidationContext, | ||
ValidationResult, | ||
} from '@docusaurus/types'; | ||
import {Joi, PathnameSchema} from '@docusaurus/utils-validation'; | ||
|
||
export const DEFAULT_OPTIONS: Partial<PluginOptions> = { | ||
fromExtensions: [], | ||
toExtensions: [], | ||
redirects: [], | ||
}; | ||
|
||
const RedirectPluginOptionValidation = Joi.object<RedirectOption>({ | ||
to: PathnameSchema.required(), | ||
from: Joi.alternatives().try( | ||
PathnameSchema.required(), | ||
Joi.array().items(PathnameSchema.required()), | ||
), | ||
}); | ||
|
||
const isString = Joi.string().required().not(null); | ||
|
||
const UserOptionsSchema = Joi.object<PluginOptions>({ | ||
fromExtensions: Joi.array() | ||
.items(isString) | ||
.default(DEFAULT_OPTIONS.fromExtensions), | ||
toExtensions: Joi.array() | ||
.items(isString) | ||
.default(DEFAULT_OPTIONS.toExtensions), | ||
redirects: Joi.array() | ||
.items(RedirectPluginOptionValidation) | ||
.default(DEFAULT_OPTIONS.redirects), | ||
createRedirects: Joi.function().arity(1), | ||
}).default(DEFAULT_OPTIONS); | ||
|
||
export function validateOptions({ | ||
validate, | ||
options: userOptions, | ||
}: OptionValidationContext<PluginOptions>): ValidationResult<PluginOptions> { | ||
return validate(UserOptionsSchema, userOptions); | ||
} |