Skip to content
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

Feat: Make a validation in body of request (DTO) #49

Merged
merged 12 commits into from
Oct 7, 2022

Conversation

talis-fb
Copy link
Contributor

@talis-fb talis-fb commented Oct 6, 2022

Issue Ticket Number

Closes Issue #12 .

Description

Now, it's possible make a DTO to assign for parameters that implement @Body() in Controllers methods.
The DTO needs to be a class, which each field uses Validation decorator of the repository Savory/validatte

Example

import { IsNumber, IsString } from 'https://deno.land/x/validatte/mod.ts';

class DTO {
	@IsString()
	name!: string;

	@IsNumber()
	age!: number;
}

@Controller('/test')
class AppController {
	constructor() {}
	@Post('')
	sayHello(@Body() body: DTO) { 
                // If the body received doens't follow the DTO a 400 status code is returned
		return 'Hello');
	}
}

I wrote some tests to this feature too, but didn't do the documentation yet. I let it for another Issue, but I can write in this one if you guys prefer.

It was also my first time with Deno and handle decorators internally. I'm open to listening to any note, suggestion or something wrong =D


Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I have run deno lint AND deno fmt AND deno task test and got no errors.
  • I have followed the contributing guidelines of this project as mentioned in CONTRIBUTING.md
  • I have checked to ensure there aren't other open Pull Requests for the same update/change?
  • I have performed a self-review of my own code
  • I have made corresponding changes needed to the documentation

Copy link
Collaborator

@Sorikairox Sorikairox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice PR overall as it adds a lot of value. A few details need to change but overall it's nice ! Thank you !

The only thing missing is a way to de-activate body validation. But it can be another PR !

Documentation can be in another PR if you want, once we are done with this one !

src/router/controller/params/decorators.ts Outdated Show resolved Hide resolved
src/router/controller/params/decorators.ts Outdated Show resolved Hide resolved
spec/validate-body-decorator.spec.ts Outdated Show resolved Hide resolved
spec/validate-body-decorator.spec.ts Outdated Show resolved Hide resolved
spec/validate-body-decorator.spec.ts Outdated Show resolved Hide resolved
src/router/controller/params/decorators.ts Outdated Show resolved Hide resolved
@Sorikairox Sorikairox linked an issue Oct 6, 2022 that may be closed by this pull request
Copy link
Collaborator

@Sorikairox Sorikairox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

@codecov-commenter
Copy link

codecov-commenter commented Oct 7, 2022

Codecov Report

Merging #49 (2e33a63) into main (123e508) will increase coverage by 0.50%.
The diff coverage is 89.83%.

@@            Coverage Diff             @@
##             main      #49      +/-   ##
==========================================
+ Coverage   84.60%   85.10%   +0.50%     
==========================================
  Files          26       38      +12     
  Lines        1104     1182      +78     
  Branches       90       98       +8     
==========================================
+ Hits          934     1006      +72     
- Misses        168      174       +6     
  Partials        2        2              
Impacted Files Coverage Δ
src/router/controller/params/decorators.ts 76.13% <88.00%> (+1.13%) ⬆️
src/deps.ts 100.00% <100.00%> (ø)
src/exception/http/exceptions.ts 45.29% <100.00%> (+2.34%) ⬆️
src/exception/filter/mod.ts 100.00% <0.00%> (ø)
src/router/controller/mod.ts 100.00% <0.00%> (ø)
src/router/controller/params/mod.ts 100.00% <0.00%> (ø)
src/injector/mod.ts 100.00% <0.00%> (ø)
src/utils/constructor.ts 100.00% <0.00%> (ø)
src/router/mod.ts 100.00% <0.00%> (ø)
src/mod.ts 100.00% <0.00%> (ø)
... and 5 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validate body
3 participants