Skip to content

@QueryParam enum support #208

Closed
Closed
@gempain

Description

@gempain

Hello,

@QueryParam('type') type: MyEnum will always set type to NaN. Otherwise, doing @QueryParam('type', {type: OperationType}) type: OperationType throws an error, since enums do not have constructors.

My current workaround is:

@UseBefore(RequestValidationUtils.isEnumMiddleware('type', MyEnum))
get(...)

and

import {Validator} from "class-validator";

export class RequestValidationUtils {

  public static isEnumMiddleware(queryParam, entity): Function {
    return (req, res, next): void => {
      const validator: Validator = new Validator();
      const queryParamValue = req.query[queryParam];
      if (!queryParamValue || validator.isEnum(req.query[queryParam], entity)) {
        next();
      } else {
        res.status(400).send('Bad request');
      }
    };
  }
}

Thanks for the amazing work !

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions