Add better options validation error

This commit is contained in:
slorber 2020-06-03 17:53:54 +02:00
parent 1439bad84c
commit c224efb8a1
3 changed files with 54 additions and 22 deletions

View file

@ -1,13 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // 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`] = ` exports[`normalizePluginOptions should reject bad fromExtensions user inputs 1`] = `
"fromExtensions[0] must be a \`string\` type, but the final value was: \`null\`. "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()\`" 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`] = ` exports[`normalizePluginOptions should reject bad toExtensions user inputs 1`] = `
"toExtensions[0] must be a \`string\` type, but the final value was: \`null\`. "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()\`" If \\"null\\" is intended as an empty value be sure to mark the schema as \`.nullable()\`
{
\\"toExtensions\\": [
null,
null,
123,
true
]
}"
`; `;

View file

@ -29,19 +29,11 @@ function isRedirectsCreator(value: any): value is RedirectsCreator | undefined {
const RedirectPluginOptionValidation = Yup.object<RedirectOption>({ const RedirectPluginOptionValidation = Yup.object<RedirectOption>({
to: PathnameValidator.required(), to: PathnameValidator.required(),
// wasn't able to use .when("from")...had cyclic dependency error // See https://stackoverflow.com/a/62177080/82609
// (https://stackoverflow.com/a/56866941/82609) from: Yup.lazy<string | string[]>((from) => {
from: Yup.mixed<string | string[]>().test({ return Array.isArray(from)
name: 'from', ? Yup.array().of(PathnameValidator.required()).required()
message: '${path} contains invalid redirection value', : PathnameValidator.required();
test: (from) => {
return Array.isArray(from)
? Yup.array()
.of(PathnameValidator.required())
.required()
.isValidSync(from)
: PathnameValidator.required().isValidSync(from);
},
}), }),
}); });
@ -57,10 +49,17 @@ const UserOptionsSchema = Yup.object().shape<UserPluginOptions>({
}); });
function validateUserOptions(userOptions: UserPluginOptions) { function validateUserOptions(userOptions: UserPluginOptions) {
UserOptionsSchema.validateSync(userOptions, { try {
abortEarly: true, UserOptionsSchema.validateSync(userOptions, {
strict: 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( export default function normalizePluginOptions(

View file

@ -25,6 +25,15 @@ module.exports = {
'@docusaurus/plugin-client-redirects', '@docusaurus/plugin-client-redirects',
{ {
fromExtensions: ['html'], fromExtensions: ['html'],
redirects: [
{
to: '/',
from: [
'///redirects-plugin/test1',
'https://google.com/redirects-plugin/test2',
],
},
],
}, },
], ],
[ [