Giter VIP home page Giter VIP logo

Comments (5)

ljlm0402 avatar ljlm0402 commented on May 21, 2024 1

@MuathAmer

Oh thank you very much. We will test and review your suggestions,
but I think your suggestions are really good. If you leave a pull request, we will improve it quickly.

from typescript-express-starter.

ljlm0402 avatar ljlm0402 commented on May 21, 2024

@MuathAmer

We apologize for not understanding the delivery of the content due to a translation problem.
Do you care about the reason why the interfaces and dto objects are separated?

from typescript-express-starter.

MuathAmer avatar MuathAmer commented on May 21, 2024

@ljlm0402
Here is what I am currently doing in validation.middleware.ts:

const validationMiddleware = (
  type: any,
  value: string | "body" | "query" | "params" = "body",
  skipMissingProperties = false,
  whitelist = true,
  forbidNonWhitelisted = true
): RequestHandler => {
  return (req: ValidatedRequest<any>, res, next) => {
    const validated = plainToClass(type, (req as any)[value]);
    req.validatedBody = validated;  // Attach the validated body to the request
    validate(validated, {
      skipMissingProperties,
      whitelist,
      forbidNonWhitelisted,
    }).then( ...... )

And in request.interface.ts I have:

export interface ValidatedRequest<T> extends Request {
  validatedBody: T;
}

And in one of my routes I do like this:

signupRouter.post(
  "/",
  validationMiddleware(SignupDto),
  async (req: ValidatedRequest<SignupDto>, res, next) => { .....

I think that this is a better way to use the validated type in the request instead of the current approach, which casts the req.body to the dto type, which is not the proper way to use validated bodies.
It's not a big problem but just a small improvement.

from typescript-express-starter.

ljlm0402 avatar ljlm0402 commented on May 21, 2024

@MuathAmer

As we test based on your issue, I have a question.

Validate the required dto elements while setting the validationMiddleware parameter skipMissingProperties of validation.middleware.ts to true and false.

However, if I do it that way, I wonder if there is a way to set skipMissingProperties.

For example, there is an updateUser in users.route.ts.

from typescript-express-starter.

MuathAmer avatar MuathAmer commented on May 21, 2024

@ljlm0402
There is no reason it won't work as far as I see. Is your concern about the type safety of validatedBody: CreateUserDto when setting skipMissingProperties = true? If so, I think that's fine as long as the programmer realizes that some properties in the DTO (validatedBody) are optional, and this can also apply to the implementation as so:

export class CreateUserDto {
  @IsEmail()
  public email?: string;

  @IsString()
  public password?: string;
}

Notice the ? optional operator above.

Or you can have separate CreateUserDto and UpdateUserDto, and the programmer might use class-validator's IsOptional() decorator or setting skipMissingProperties = true, both are valid options for UpdateUserDto, but at this point that's class-validator's knowledge.

from typescript-express-starter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.