mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-31 18:07:00 +02:00
Add better options validation error
This commit is contained in:
parent
1439bad84c
commit
c224efb8a1
3 changed files with 54 additions and 22 deletions
|
@ -1,13 +1,37 @@
|
|||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`normalizePluginOptions should reject bad createRedirects user inputs 1`] = `"createRedirects should be a function"`;
|
||||
exports[`normalizePluginOptions should reject bad createRedirects user inputs 1`] = `
|
||||
"Invalid @docusaurus/plugin-client-redirects options: createRedirects should be a function
|
||||
{
|
||||
\\"createRedirects\\": [
|
||||
\\"bad\\",
|
||||
\\"value\\"
|
||||
]
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`normalizePluginOptions should reject bad fromExtensions user inputs 1`] = `
|
||||
"fromExtensions[0] must be a \`string\` type, but the final value was: \`null\`.
|
||||
If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`"
|
||||
"Invalid @docusaurus/plugin-client-redirects options: fromExtensions[0] must be a \`string\` type, but the final value was: \`null\`.
|
||||
If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`
|
||||
{
|
||||
\\"fromExtensions\\": [
|
||||
null,
|
||||
null,
|
||||
123,
|
||||
true
|
||||
]
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`normalizePluginOptions should reject bad toExtensions user inputs 1`] = `
|
||||
"toExtensions[0] must be a \`string\` type, but the final value was: \`null\`.
|
||||
If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`"
|
||||
"Invalid @docusaurus/plugin-client-redirects options: toExtensions[0] must be a \`string\` type, but the final value was: \`null\`.
|
||||
If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`
|
||||
{
|
||||
\\"toExtensions\\": [
|
||||
null,
|
||||
null,
|
||||
123,
|
||||
true
|
||||
]
|
||||
}"
|
||||
`;
|
||||
|
|
|
@ -29,19 +29,11 @@ function isRedirectsCreator(value: any): value is RedirectsCreator | undefined {
|
|||
|
||||
const RedirectPluginOptionValidation = Yup.object<RedirectOption>({
|
||||
to: PathnameValidator.required(),
|
||||
// wasn't able to use .when("from")...had cyclic dependency error
|
||||
// (https://stackoverflow.com/a/56866941/82609)
|
||||
from: Yup.mixed<string | string[]>().test({
|
||||
name: 'from',
|
||||
message: '${path} contains invalid redirection value',
|
||||
test: (from) => {
|
||||
// See https://stackoverflow.com/a/62177080/82609
|
||||
from: Yup.lazy<string | string[]>((from) => {
|
||||
return Array.isArray(from)
|
||||
? Yup.array()
|
||||
.of(PathnameValidator.required())
|
||||
.required()
|
||||
.isValidSync(from)
|
||||
: PathnameValidator.required().isValidSync(from);
|
||||
},
|
||||
? Yup.array().of(PathnameValidator.required()).required()
|
||||
: PathnameValidator.required();
|
||||
}),
|
||||
});
|
||||
|
||||
|
@ -57,10 +49,17 @@ const UserOptionsSchema = Yup.object().shape<UserPluginOptions>({
|
|||
});
|
||||
|
||||
function validateUserOptions(userOptions: UserPluginOptions) {
|
||||
try {
|
||||
UserOptionsSchema.validateSync(userOptions, {
|
||||
abortEarly: true,
|
||||
abortEarly: true, // Needed otherwise the message is just "2 errors occurred"
|
||||
strict: true,
|
||||
});
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Invalid @docusaurus/plugin-client-redirects options: ${e.message}
|
||||
${JSON.stringify(userOptions, null, 2)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default function normalizePluginOptions(
|
||||
|
|
|
@ -25,6 +25,15 @@ module.exports = {
|
|||
'@docusaurus/plugin-client-redirects',
|
||||
{
|
||||
fromExtensions: ['html'],
|
||||
redirects: [
|
||||
{
|
||||
to: '/',
|
||||
from: [
|
||||
'///redirects-plugin/test1',
|
||||
'https://google.com/redirects-plugin/test2',
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
[
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue