Comments (5)
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.
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.
@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.
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.
@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)
- TypeORM settings missing for cli to work
- T HOT 1
- Prisma template crashes on start HOT 1
- Graceful close
- how to stop the logging to the logs/error ? HOT 2
- Cannot launch express-typescript-mongoose project on my local HOT 4
- Sensitive case for '@exceptions/httpException' HOT 1
- Alias error in test folder HOT 3
- Argument of type 'string' is not assignable to parameter of type 'boolean'
- Typo import error
- TSOA with decorator doesn't work. HOT 2
- Is there any plan to support email verification for registration?
- Conflict when use AdminJS version 7
- Enable ESM for routing-controller template
- choosen sequlize postgres
- Prisma connections
- Prisma Connection Pool
- Template TypeScript + Routing Controllers + Express API Server :[nodemon] app crashed
- Knex migration using initial migration example not working
- Unable to Start the APP
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-express-starter.