-
Notifications
You must be signed in to change notification settings - Fork 139
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
Validate Input for Create & Update #19
Comments
Thanks for the detailed issue @johannesschobel! Yes I think this would be a great addition. |
Shall i make a PR for you? Or will you work on this? |
Yeah if you want to make a PR that would be great! |
Dear @doug-martin , i just wanted to create a PR for this issue. My first approach was like this: import { Class } from '@nestjs-query/core';
import { Field, ArgsType } from 'type-graphql';
import { ValidateNested } from 'class-validator';
impor { Type } from 'class-transformer';
export interface CreateOneArgsType<C> {
input: C;
}
export function CreateOneArgsType<C>(ItemClass: Class<C>): Class<CreateOneArgsType<C>> {
@ArgsType()
class CreateOneArgs implements CreateOneArgsType<C> {
@Type(() => C) // <-- this fails because 'C' only refers to a type, but is being used as a value here.
@ValidateNested()
@Field(() => ItemClass, { description: 'The record to create' })
input!: C;
}
return CreateOneArgs;
} However, this does not work, because Any idea how to cope with this? |
Yep,you need to pass in the If you take a look at https://github.com/doug-martin/nestjs-query/blob/master/packages/query-graphql/src/types/query/query-args.type.ts#L46 you'll see I had to do the same thing. |
ok, i don't have any idea how to test this properly.. because i would need the |
For the unit tests I've been adding spys just to the decorators to make sure everything is wired up properly. For e2e I haven't automated that yet, its on my todo list. But I usually add something to the example and test manually. |
You might also checkout https://github.com/doug-martin/nestjs-query/blob/master/packages/query-graphql/__tests__/types/query/query-args.type.spec.ts In there I use class validator just to ensure everything is getting validated as expected. |
my first approach would have also been to use the Maybe we can just use
in the |
Oh, yeah when developing run |
wait.. i may be an idiot on this one.. lol.. gimme a second |
ok.. i cannot get the first initial proposal of this issue to work in the However, in my own "real application" i am building it works fine with the custom argstype.. in the demo application it wont work.. maybe some kind of dependency problem (version!) |
If you can provide a branch I may be able to help. |
@johannesschobel @doug-martin How's it going, guys? I'm waiting for your PR |
Published under |
oh man.. that's awesome! thanks a lot |
Dear @doug-martin ,
i was trying to validate input for my
CreateX
andUpdateX
mutations. Note that i use the defaultValidationPipe
provided by@nestjs/common
andclass-validator
package.My
ValidationPipe
is defined as follows:This definition, in turn, states, that ONLY defined attributes may pass, unknown properties automatically result in errors and so on. As i register the
ValidationPipe
as aGlobalPipe
it is automatically applied to all endpoints! I can confirm, that this works for me.While this may be better from the securities point of view, this results in the following issue: As the data sent to the backend is "wrapped" in the
input
param, it cannot be validated. Note, that theValidationPipe
will exit with a failure, as it detects an undeclared fieldinput
.As i do not want to miss out on this feature, i digged into your library and found that it is possible to write custom
ArgTypes
forCreate/Update
- which is awesome 👍 .I then created my own Type like this:
What this basically does:
@Type(() => CreateUserInput)
maps the content of the variable to respective class (i.e., it is mapped to theCreateUserInput
class).@ValidateNested()
tells theclass-validator
package, to ALSO validate the rules in theCreateUserInput
class (e.g., the fieldemail
must be a validemail
,username
must be at least 5 chars long, ...)So my question is:
Would it be ok for you to merge such changes into your own code-base? I guess, the
Validation feature
would be a significant improvement for your library. What do you think? I would be willing to make a PR - because it actually would save me a lot of time instead of creating my own customArgsType()
?From the first glimpse, those chances may be directly applied to the
CreateOneArgsType
andUpdateOneArgsType
. For theCreateManyArgsType
andUpdateManyArgsType
we may need to use the@ValidateNested({ each: true })
option, in order to validate each element individually..All the best,
Johannes
The text was updated successfully, but these errors were encountered: