-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: start work on validating plugin options #16027
Conversation
Is this distinction that need to be made? Only usecase I see where this could be problematic (depending how it's handled) is if there is theme that also doubles as starter (where you would want validation for theme options, but maybe not when using it as a starter (tho then, using default values might actually be good idea) |
It feels like a "useless" API in the root of a project (because there aren't any options?), but yeah -- perhaps not a fix that needs to be made. In other words, if a user wants to use it (even though it's not valuable) -- so be it! |
Because this is quite breaking for any plugins that implement (without updating Gatsby in the consuming application), let's either:
I think I'm leaning towards 1 in the interest of shipping the base functionality, however, I do also think it's quite important to use the API in various plugins so that we can ensure it accommodates all use cases. |
This is no longer blocked since #16027 landed. I'll revisit work on this today and this week. There will be some churn in any new APIs until people upgrade to the latest Gatsby, but I think that's OK. |
I think that's wrong PR/Issue number (it's links to this PR) ;) |
lol -- I double down'd on my assumption. *** #16105 |
BREAKING: relying upon nested SitePlugin fields (pluginOptions and packageJson) is no longer supported. These are now typed as generic GraphQL `JSON` types to avoid any issues regarding missing or conflicting field types.
OK -- got a lot done on this.
What's not done?
Example |
* validatePluginOptions is special | ||
* it validates plugin options and (optionally) mutates them | ||
*/ | ||
if (api === `validatePluginOptions`) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels extra janky. Any suggestions for improvements?
allowUnknown: true, | ||
}) | ||
} | ||
return resChain.then(options => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's where options are actually coerced. A "gotcha" here is that whether using Joi or not, the returned structure is what will be passed to other APIs that consume plugin options.
Note: I haven't forgotten about this (important!) PR -- but other priorities have come up. I'd like to circle back to this before the end of the week. |
@DSchau because of the complex plugin options for |
@moonmeister I was just gonna pick this up again today! Yeah -- that'd be a great one. It should be ready to test for plugins. Would you want to open a PR to this branch when you test things out? |
@DSchau that works for me. |
…option-validation
Hey @moonmeister! Awesome! Let me add a few comments/questions:
Why is this necessary? Removing "strict" (e.g. allowing unknown keys) should prevent this from being necessary? (which should be the behavior now, e.g. I don't add plugins to my Contentful test -> gatsby/packages/gatsby-source-contentful/src/plugin-options.js Lines 15 to 28 in 01f4b8f
Agreed the concat approach would work, but ideally we don't need that at all. |
@DSchau Hmm, I suppose I'd lean towards the strict mode by default. It eliminates the typo case. I meant to set property |
@moonmeister I think you should check out the code I linked to! Strict (as I used it) basically means it doesn't fail on extra keys passed that aren't part of the validation schema (in this case, plugins!). In your example, if you set |
@DSchau Opps, you must have added code later, cause I didn't see it originally. Either way What you describe is what I'm suggesting, I want it to throw when I pass |
You could directly use Joi to workaround the baked-in defaults. I think what you're describing is a case that I'm not too concerned about. If we think about your exact example, if you typo Happy to be proven wrong! |
@DSchau Okay I get what your saying, there is still the case if |
@moonmeister what if a plugin wants to enforce a particular shape for plugins? We then have a non-overridable key, right? I guess I'd toss it back to ya -- why is Also to be clear, if this is a concern, you can use your own instance of Joi (or really any validation). Ours is passed as a convenience, it's not strictly necessary. |
@moonmeister in thinking more about my above statement -- I think |
It's not about the negative, it's about the positive experience it provides. I think I've articulated a very valid and probably common case where a dev (especially new ones) miss type or are trying to add properties not understanding where they should go. Explicitly setting |
@moonmeister you've persuaded me! I'll keep thinking if there are any plugins that may not match this expected structure (I suppose we could just make it more generic, e.g. validate that plugins is an array!), but for now, I agree that this is an improvement. It does mean that developers will have to be strict about valid options, but perhaps that's a valuable outcome? See f0ef733 for the implementation and 870def4 for the tests |
I think it's valuable. |
…n options validation
…c they adhear to.
feat: add initial options validation
@DSchau Where we at on this? Anything else I can do to help? |
This was successfully implemented in a bunch of others PRs :) |
Description
Extremely WIP work on implementing #15281
Issues to Solve
pluginOptions
for the plugin from that pointallowUnknown: true
to avoid emptyplugins
option causing issuesabortEarly: false
to show all errorsand probably more I'm forgetting. It's a start! 🚀
Still to do
validator
doesn't cause any issues with the error messageRelated Issues