Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
nestjs-yup-validator
Advanced tools
Nest js is an awesome ndoejs framework for building scalable aplications with clean and reusable code ( my favorite). However it has some problems with its current validation system. While it can be easy to use an intuitive for some people, for other it
Nest js is an awesome ndoejs framework for building scalable aplications with clean and reusable code ( my favorite). However it has some problems with its current validation system. While it can be easy to use an intuitive for some people, for other it can be frustrating o limiting. The actual validation system is really good ( im my opinion ), but nestjs lack of alternatives for requisition validation.
Yup is a schema builder for validating and parsing objects. Its simple, lightweight and easy to use. Even that it may not be so powerfull as class-validator, it may be helpfull in some cases, making easy and fast to validate query string and body objects from requisitions.
Ofer an alternative to class-validator with a package to integrate NestJS and Yup, wich may not be as powerfull as the current validation system but can it can be really handfull and simple to use. Also offer some manual validation option, with in some cases may be usefull.
In you project directory
$ npm install nestjs-yup-validator --save
// Creating a yup schema
export const SaveUserValidation = yup.object({
username: yup.string().required().min(3).max(15),
mail: yup.string().required().email(),
password: yup.string().required().min(8).max(16),
age: yup.number().min(1)
})
Optional DTO usage (for typescript)
// request dto
export class SaveUserReqDTO {
username: string
mail: string
password: string
age: number
}
// validation schema
export const SaveUserValidation = yup.object<SaveUserReqDTO>({
username: yup.string().required().min(3).max(15),
mail: yup.string().required().email(),
password: yup.string().required().min(8).max(16),
age: yup.number().min(1)
})
export function SaveUserValidation(body) {
validateUserObject(body)
const validatedUserObject = transformUserObject()
// the returned value will be passed to the object
return {
requestTime: new Date().toString(),
...validatedUserObject
}
}
You may apply the validation using a Decorator on the controller. Use BodyValidator (for post, put, fetch and delete) or QueryValidator (for get) with the schema, a function or even both of them.
See the example bellow:
@Post('post')
createPost(@BodyValidator([SavePostValidation]) body: SaveUserReqDTO) {
const newPost = this.postService.save(body)
return newPost
}
@Get('querystring-validation')
queryValidation(@QueryValidator([ListThisValidation]) listFilter: ListThigsDTO) {
return this.thisgsService.find(listFilter)
}
@Post('some-complex-request-validation')
someComplexValidation(@QueryValidator([ValidationSchema, validationFunction, anotheValidationFunction]) listFilter: ListThigsDTO) {
return this.thisgsService.find(listFilter)
}
MIT
FAQs
Nest js is an awesome ndoejs framework for building scalable aplications with clean and reusable code ( my favorite). However it has some problems with its current validation system. While it can be easy to use an intuitive for some people, for other it
We found that nestjs-yup-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.