Savory / Danet

The most mature backend framework for Deno. Create awesome HTTP and WebSocket server as well as KVQueue workers !
https://danet.land
Other
269 stars 18 forks source link

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

Closed talis-fb closed 2 years ago

talis-fb commented 2 years ago

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


Checklist:

codecov-commenter commented 2 years ago

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%) :arrow_up:
src/deps.ts 100.00% <100.00%> (ø)
src/exception/http/exceptions.ts 45.29% <100.00%> (+2.34%) :arrow_up:
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

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