nexus
nexus copied to clipboard
input validation?
Are there any plans for input validation?
I think a great contender for that can be either io-ts
or runtypes
.
But ideally, of course, it should be package-agnostic.
Maybe it could look something like this:
export const InputType = inputObjectType({
name: "InputType",
definition(t) {
t.string("key", { required: true });
t.int("answer");
},
// added
validate(o: InputTypeInterface) => {
if (!o.key) {
throw new Error('key is required!')
}
return Promise.resolve(o)
}
});
Thoughts?
Hey @moltar, tackling validation makes sense to me. There's a lot of ways to cut it and I also wonder if we can solve it as a plugin or need to bake it into core. We'll take a look at this more in the coming weeks/months hopefully.
Aside: yeah io-ts
is awesome 🙏@gcanti :)
Is there a plan to implement it soo? Maybe I would try to tackle it :)
@huv1k no plan currently. Give it a shot, curious what you come up with.
It would be awesome to have input validation built into Nexus! nexus-validate by @filipstefansson is a nice package that looks somewhat similar to this proposition, and uses yup
under the hood.
We've created nexus plugin here https://github.com/dooboolab/nexus-validation-plugin and using it for production.
Written by @0916dhkim
I tried both available plugins for validation, sadly both seem to not work properly with the current version of nexus. I would very much appreciate if a properly specified input validation would be baked right into the nexus core.
One thought on the error handling though: GraphQL's error responses are not typed, and therefore are not very much fun on the client side. I use an "errors" field which is a list of one specific enum (e.g. "LoginErrors" --> ['INVALID_USERNAME', 'INVALID_PASSWORD', 'BANNED']).
I would love the implementation to be able to define such a field for every response where the input with validation rules is attached to the arguments of that query/mutation. I know this adds a lot of complexity, but this could really improve the developer experience as errors are then typed by default.
@PhilippMolitor Hello! Thank you for trying nexus-validation-plugin
. May I ask what kind of error you experienced when you tried the plugin? I would like to work on a fix if there is something wrong in my implementation.
Are there any updates on this?
I think the possibility to have straightforward input validation is really crucial and is one major part that is missing in this wonderful library. It would be a real productivity boost.
From the libraries I checked so far I found nexus-validate the most promising because of the simplicity. Sadly there was no update in months and the author also doesn't seem to respond to PR's quickly.
Are there any plans here? Is help from the community required? I appreciate any update, thanks!
@ysfaran I've had a few busy months! I use nexus-validate in one of my apps and it works well, so I haven't had any need to update it. I'll give it some love the coming week and make sure it works with the latest version of nexus and it should be good for another few months. :-)
The plugin is very small (only like 10 lines is actual logic) so should be quick!
@filipstefansson wow that was a fast counter to my assumption 😄 Thanks for clearing it up and the efforts. I will probably try it out in near future (somewhere next week probably).
But still, it would be nice to have a built-in input validation or at least officially hightlight plugins like nexus-validate
in the docs so they gain more traction.
@filipstefansson wow that was a fast counter to my assumption 😄 Thanks for clearing it up and the efforts. I will probably try it out in near future (somewhere next week probably).
But still, it would be nice to have a built-in input validation or at least officially hightlight plugins like
nexus-validate
in the docs so they gain more traction.
New version is up! Take a look and if it works as expected with the latest nexus version now.
@filipstefansson I just tried it out for few days and it looks very nice so far 🚀 Thanks for the efforts!
I also opened an issue already 😄
Hey guys! I recently created a validation plugin for nexus with a ton of features. I hope you check it out and tell me what you think!