Skip to content

Commit

Permalink
Raplaced validator with Deno module validator4oak (#31)
Browse files Browse the repository at this point in the history
* Raplaced validator with Deno module validator4oak
* Fixed wrong dep path - was using local ^^
  • Loading branch information
petruki committed Mar 9, 2024
1 parent edd7096 commit 6ce5d38
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 202 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ It uses [Oak](https://deno.land/x/oak) as middleware framework and [Switcher4Den
## Running (local)

```bash
deno task run:dev
deno task run
```

## Running (docker)
Expand Down
5 changes: 3 additions & 2 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@switcherapi/switcher-management-feature",
"version": "1.0.1",
"version": "1.0.2",
"description": "Feature Flag Service for Switcher Management",
"tasks": {
"run": "deno run --allow-net --allow-env --allow-read --allow-write src/index.ts",
Expand All @@ -12,5 +12,6 @@
"lcov": "deno coverage coverage --lcov --output=coverage/report.lcov",
"clean": "rm -rf ./npm ./coverage",
"cover": "deno task clean && deno task test && deno task lcov && genhtml -o coverage/html coverage/report.lcov"
}
},
"lock": true
}
332 changes: 229 additions & 103 deletions deno.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.projectKey=switcherapi_switcher-management-feature
sonar.projectName=switcher-management-feature
sonar.organization=switcherapi
sonar.projectVersion=1.0.1
sonar.projectVersion=1.0.2

sonar.javascript.lcov.reportPaths=coverage/report.lcov

Expand Down
2 changes: 1 addition & 1 deletion src/api-docs/swagger-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
title: 'Switcher Management Feature',
version: 'v1.0.1',
version: 'v1.0.2',
description: 'Feature Flag Service for Switcher Management.',
contact: {
name: 'Roger Floriano (petruki)',
Expand Down
7 changes: 4 additions & 3 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { Application, Router } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export type { Middleware, Next } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export { Context } from 'https://deno.land/x/oak@v14.1.1/context.ts';
export { Application, Router } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export type { Middleware, Next } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export { Context } from 'https://deno.land/x/oak@14.2.0/context.ts';
export { load } from 'https://deno.land/std@0.218.2/dotenv/mod.ts';
export { checkValue, Switcher } from 'https://deno.land/x/switcher4deno@v1.0.7/mod.ts';
export { bold, cyan, green } from 'https://deno.land/std@0.218.2/fmt/colors.ts';
export { ValidatorFn, ValidatorMiddleware } from 'https://deno.land/x/validator4oak@v1.0.0/mod.ts';
4 changes: 2 additions & 2 deletions src/dto/feature-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface FeatureRequestDto {
export type FeatureRequestDto = {
feature: string;
value: string;
}
};
8 changes: 4 additions & 4 deletions src/dto/feature-response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export interface ResponseDto {
export type ResponseDto = {
code: number;
message: string;
}
};

export interface FeatureResponseDto extends ResponseDto {
export type FeatureResponseDto = ResponseDto & {
status: boolean;
}
};
69 changes: 0 additions & 69 deletions src/middleware/validator.ts

This file was deleted.

20 changes: 12 additions & 8 deletions src/routes/feature.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { Context, Router } from '../deps.ts';
import { Context, Router, ValidatorFn, ValidatorMiddleware } from '../deps.ts';
import { toFeatureRequestDto, toFeatureResponseDto } from '../dto/mapper.ts';
import FeatureService from '../services/feature.ts';
import { responseError, responseSuccess } from '../utils.ts';
import Validator from '../middleware/validator.ts';

const router = new Router();
let service: FeatureService;

const { checkBody, hasLenght, required } = Validator;
const { body, useErrorHandler } = ValidatorMiddleware.createMiddleware();
const { hasLenght } = ValidatorFn.createValidator();

useErrorHandler((context: Context, error: string) => {
return responseError(context, new Error(error), 422);
});

router.post(
'/',
checkBody([
{ key: 'feature', validators: [required()] },
{ key: 'parameters.value', validators: [hasLenght({ max: 100 })] },
body([
{ key: 'feature' },
{ key: 'parameters.value', validators: [hasLenght({ max: 100 })], optional: true },
]),
async (context: Context) => {
try {
Expand All @@ -26,12 +30,12 @@ router.post(
},
);

const getService = () => {
function getService() {
if (!service) {
service = new FeatureService();
}

return service;
};
}

export default router;
4 changes: 2 additions & 2 deletions src/services/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FeatureService {
}
}

interface Param {
type Param = {
value: string;
}
};

export default FeatureService;
4 changes: 2 additions & 2 deletions test/deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { testing } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export type { Middleware } from 'https://deno.land/x/oak@v14.1.1/mod.ts';
export { testing } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export type { Middleware } from 'https://deno.land/x/oak@14.2.0/mod.ts';
export { assert, assertEquals, assertFalse, assertObjectMatch } from 'https://deno.land/std@0.218.2/assert/mod.ts';
export { superoak } from 'https://deno.land/x/superoak@4.8.1/mod.ts';
export { Switcher } from 'https://deno.land/x/switcher4deno@v1.0.7/mod.ts';
10 changes: 6 additions & 4 deletions test/routes/feature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Deno.test({
const request = await superoak(app);
const res = await request.post('/')
.send()
.expect(400);
.expect(422);

assertObjectMatch(res.body, { error: 'Invalid request body' });
},
Expand All @@ -65,9 +65,9 @@ Deno.test({
const request = await superoak(app);
const res = await request.post('/')
.send({ feature: '' })
.expect(400);
.expect(422);

assertObjectMatch(res.body, { error: 'Invalid feature input. Cause: it is empty.' });
assertObjectMatch(res.body, { error: 'Invalid feature input. Cause: it is required.' });
},
});

Expand All @@ -81,6 +81,8 @@ Deno.test({
.send({ feature: 'FEATURE_NAME', parameters: { value: 'VALUE'.repeat(100) } })
.expect(422);

assertObjectMatch(res.body, { error: 'Invalid parameters.value input. Cause: it is greater than 100 characters.' });
assertObjectMatch(res.body, {
error: 'Invalid parameters.value input. Cause: it exceeds the maximum length of 100.',
});
},
});

0 comments on commit 6ce5d38

Please sign in to comment.