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
Graphql-upload with validation in shema #182
Comments
I'm not very familiar with those tools, but keep in mind things are about to change: #181. If a client tries to send something invalid (like a string or boolean) for the value of an |
v10.0.0 has been published 🚀
Feel free to continue the discussion if your particular problem has not been solved. |
I did the following code to make an attempt to fix and it worked async resolvePromises(value: any) {
for (const key in value) {
if (key && typeof value[key]?.then === 'function') {
value[key] = await value[key];
}
}
return value;
}
And my validation Pipes.
async transform(value: any, { metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
await this.resolvePromises(value);
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
throw new BadRequestException(this.mapErrors(errors));
}
return value;
}
My Image Upload Class
class ImageUpload implements FileUpload {
@IsIn(['image/jpeg', 'image/png', 'image/gif'])
mimetype: string;
encoding: string;
filename: string;
@Exclude() /* Decorator so that the context of the function is not lost */
createReadStream: () => ReadStream;
}
On my Input Class
@ArgsType()
class ExampleInputData {
@Field(GraphqlField.typeOf(GraphQLUpload), { nullable: true })
@ValidateNested()
@Type(() => ImageUpload)
image?: ImageUpload;
} And I get the following response when trying to upload a pdf {
"errors": [
{
"message": "Bad Request Exception",
"statusCode": 400,
"response": [
{
"property": "image",
"children": [
{
"property": "mimetype",
"constraints": [
"isIn"
]
}
]
}
]
}
],
"data": null
} |
Hello im sorry for my bad english im french.
Usually I check the inputs directly in my shemas, with type-graphql and class-validator.
But since the file seems to be injected automatically, I don't see how to do it.
Something like that would be possible ?
Thanks you :)
The text was updated successfully, but these errors were encountered: