-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mongoose id validation service and custom decorator
- Loading branch information
1 parent
8281ab0
commit 7285507
Showing
8 changed files
with
94 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
import { IsString } from 'class-validator'; | ||
import { MongooseId } from 'src/shared/mongoose-id.decorator'; | ||
|
||
// TODO: add validation | ||
export class CartDto { | ||
id: string; // TODO: validation for mongooseId | ||
@IsString() | ||
@MongooseId() | ||
id: string; | ||
quantity: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
// TODO: add validation | ||
import { IsString } from 'class-validator'; | ||
import { MongooseId } from 'src/shared/mongoose-id.decorator'; | ||
|
||
export class ProductIdDto { | ||
@IsString() | ||
@MongooseId() | ||
id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { HttpException } from '@nestjs/common'; | ||
import { registerDecorator } from 'class-validator'; | ||
import { GlobalExceptionKeys } from 'src/enums'; | ||
import mongoose from 'mongoose'; | ||
|
||
export function MongooseId() { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'mongooseId', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
//constraints: [property], | ||
//options: validationOptions, | ||
validator: { | ||
validate(value: string) { | ||
if (mongoose.isValidObjectId(value)) { | ||
return true; | ||
} else { | ||
const httpExceptionObject: { | ||
status: number; | ||
error: string; | ||
message: string[]; | ||
} = { | ||
status: 400, | ||
error: 'Invalid mongoose object ID', | ||
message: [GlobalExceptionKeys.IncorrectMongooseID], | ||
}; | ||
throw new HttpException(httpExceptionObject, 400); | ||
} | ||
}, | ||
}, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ExceptionService } from './exception.service'; | ||
import { ExceptionStatusKeys, GlobalExceptionKeys } from 'src/enums'; | ||
import mongoose from 'mongoose'; | ||
|
||
@Injectable() | ||
export class MongooseValidatorService { | ||
constructor(private exceptionService: ExceptionService) {} | ||
|
||
isValidObjectId(...ids: string[]) { | ||
ids.forEach((id) => { | ||
mongoose.isValidObjectId(id) || this.throwInvalidId(); | ||
}); | ||
} | ||
|
||
private throwInvalidId() { | ||
this.exceptionService.throwError( | ||
ExceptionStatusKeys.BadRequest, | ||
'Ivalid mongoose object id', | ||
GlobalExceptionKeys.IncorrectMongooseID, | ||
); | ||
} | ||
} |