Skip to content
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

Using NexusTypes with ZOD validation tool (Or yup, joy ...) #1133

Open
Capsicum42 opened this issue Sep 15, 2022 · 3 comments
Open

Using NexusTypes with ZOD validation tool (Or yup, joy ...) #1133

Capsicum42 opened this issue Sep 15, 2022 · 3 comments

Comments

@Capsicum42
Copy link

I try to create a validation of my input args of Nexus e.g.
const MembersFilter = inputObjectType({ name: 'MembersFilter', definition(t) { t.list.string('memberIds', { nullable: true }) }, })

with the ZOD https://github.com/colinhacks/zod#basic-usage validation libray.

e.g. min characters 3, max 10 of memberId

Currently I define the type twice and keep them in sync.

Has anybody an Idea how to define it once (in typescript) and then use it in both tools ?

Regards
Holger

@ahmedosama7450
Copy link
Contributor

Hello @Capsicum42
I have recently created a validation plugin for nexus with tons of features. You can check it out here

Also, I think that using a validation library (e.g. zod, yup,...) with graphql is not a very good idea because the main objective of these libraries is to check that types are correct. graphql does this for us already. Please, read through motivation section to further know what I mean.

@jakobo
Copy link

jakobo commented Sep 20, 2022

As pointed out by ahmedosama, the basic typings are validated automatically by GraphQL. For typing beyond this, I do create a specific "business logic" schema such as z.string().min(3).max(10).parse(input.memberId) as these describe runtime validations and don't have an equivalent type expression in GraphQL.

If you are needing auto-generated schema to compliment your nexus types, you may want to consider a tool such as ts-to-zod, which can take your generated types in NexusGenInputs and convert those to zod validators. It has a programmatic API and understands JSDoc comments, meaning you can generate your zod validators at the same time you call makeSchema

@ahmedosama7450
Copy link
Contributor

Hello @jakobo
nexus-args-validator provides builtin validators for common validations like min, max, pattern, etc. You can check the rest here.

You can also build your own custom resuable validators (which can return promises that we internally resolve without sacrificing the performance of synchronous validators).
We also provide helpers to combine validators.

Moreover, we provide typesafe transformation for arguemnts before validating them. e.g. trimming strings, converting to lowercase/uppercase, clamping a numeric argument or any kind of transformation you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants