-
Notifications
You must be signed in to change notification settings - Fork 4
Add: Logger and ValidationPipe #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
doylefermi-kv
commented
May 18, 2021
- Using the Logger -
- Added validation pipe.
Move validationSchema to separate file
…ponding type declarations.
| export const UserSchema = Joi.object({ | ||
| email: Joi.string().email({ tlds: { allow: false } }), | ||
| firstName: Joi.string(), | ||
| middleName: Joi.string(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it throw error if middleName is optional and not passed in request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, MiddleName will remain as an optional field. No error will be thrown. However, this will remain as an additional validation over the existing graphql validation that MiddleName is a string.
src/validation/validation.pipe.ts
Outdated
| transform(value: any, metadata: ArgumentMetadata): any { | ||
| const { error } = this.schema.validate(value); | ||
| if (error) { | ||
| throw new BadRequestException('Validation failed', error.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will it be handled in exception filter and sent the custom error response?
Also if validation fails for multiple fields, will it send all error messages in single response?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jinilcs , yes the validation thrown will be handled by the custom error response. Also, all the validations will be collated together -
Example:
{
"errors": [
{
"message": "Failed to validate the input payload",
"path": [
"createUser"
],
"extensions": {
"statusCode": 400,
"error": "\"email\" must be a valid email. \"firstName\" is not allowed to be empty"
}
}
],
"data": {
"createUser": null
}
}```
- All validations are returned in a single response - Handled by the global exception filter - Reword the validation message