Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
nestjs-yup-validator
Advanced tools
Readme
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
The npm package nestjs-yup-validator receives a total of 7 weekly downloads. As such, nestjs-yup-validator popularity was classified as not popular.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.