nexus icon indicating copy to clipboard operation
nexus copied to clipboard

input validation?

Open moltar opened this issue 4 years ago • 13 comments

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?

moltar avatar Jul 07 '19 02:07 moltar

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 :)

jasonkuhrt avatar Sep 02 '19 20:09 jasonkuhrt

Is there a plan to implement it soo? Maybe I would try to tackle it :)

huv1k avatar Jul 02 '20 05:07 huv1k

@huv1k no plan currently. Give it a shot, curious what you come up with.

jasonkuhrt avatar Jul 02 '20 19:07 jasonkuhrt

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.

coopbri avatar May 21 '21 20:05 coopbri

We've created nexus plugin here https://github.com/dooboolab/nexus-validation-plugin and using it for production.

Written by @0916dhkim

hyochan avatar May 22 '21 04:05 hyochan

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 avatar Jul 30 '21 13:07 PhilippMolitor

@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.

0916dhkim avatar Jul 30 '21 13:07 0916dhkim

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 avatar Sep 04 '21 09:09 ysfaran

@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 avatar Sep 04 '21 10:09 filipstefansson

@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.

ysfaran avatar Sep 05 '21 10:09 ysfaran

@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 avatar Sep 08 '21 08:09 filipstefansson

@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 😄

ysfaran avatar Sep 10 '21 17:09 ysfaran

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!

ahmedosama7450 avatar Mar 24 '22 19:03 ahmedosama7450