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

Graphql-upload with validation in shema #182

Closed
romainleduc opened this issue Jan 18, 2020 · 3 comments
Closed

Graphql-upload with validation in shema #182

romainleduc opened this issue Jan 18, 2020 · 3 comments

Comments

@romainleduc
Copy link

romainleduc commented Jan 18, 2020

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 ?

export class UploadImage {
    @IsIn([
        'image/jpeg',
        'image/png',
        'image/gif',
    ])
    @Field()
    public mimetype: string;

    @IsUniqueImage()
    @Field()
    public filename: string;

    @Field()
    public encoding: string;

    @Field(type => Function)
    createReadStream: () => Stream;
}

Thanks you :)

@jaydenseric
Copy link
Owner

jaydenseric commented Jan 18, 2020

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 Upload scalar, the upload scalar itself will validate the value as incorrect and return a GraphQL error. You won't have to validate a value is an upload in resolvers anymore, although you still might like to do your own validation on the file itself once the upload resolves.

@jaydenseric
Copy link
Owner

v10.0.0 has been published 🚀

Upload scalar values are now validated by the scalar, so you shouldn't have to check in resolvers anymore.

Feel free to continue the discussion if your particular problem has not been solved.

@davp00
Copy link

davp00 commented Mar 18, 2021

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
}

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