Skip to content

Commit

Permalink
use workerInfo to get the expected value
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jan 14, 2024
1 parent 21307b4 commit 5ab068b
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 17 deletions.
14 changes: 1 addition & 13 deletions src/handlers/route_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ export class RouteHandler {
}
}

static addExpected(type: string, className: string, workerName: string, expectedValue: any) {
static addExpected(type: "body" | "query" | "param", className: string, workerName: string, expectedValue: any) {

for (const prop in expectedValue) {
const propValue = expectedValue[prop];
Expand Down Expand Up @@ -359,18 +359,6 @@ export class RouteHandler {
}
}

static getExpectedQuery(controllerName: string, workerName: string) {
return routerCollection.get(controllerName).workers.get(workerName).expectedQuery;
}

static getExpectedBody(controllerName: string, workerName: string) {
return routerCollection.get(controllerName).workers.get(workerName).expectedBody;
}

static getExpectedParam(controllerName: string, workerName: string) {
return routerCollection.get(controllerName).workers.get(workerName).expectedParam;
}

static addRouteToCache(url: string, route: IRouteMatch) {
routeCache.set(url, route);
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/validate_body_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { executeValidate } from "./execute_validate";
export class ValidateBodyGuard extends Guard {
async check() {
const componentProp = this['componentProp_'];
const expectedBody = RouteHandler.getExpectedBody(componentProp.controllerName, this.workerName);
const expectedBody = componentProp.workerInfo.expectedBody;
if (expectedBody == null) return;
const validationResult = await executeValidate(expectedBody, this.body);
if (validationResult.error) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/validate_param_guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { executeValidate } from "./execute_validate";
export class ValidateParamGuard extends Guard {
async check() {
const componentProp = this['componentProp_'];
const expectedValue = RouteHandler.getExpectedParam(componentProp.controllerName, this.workerName);
const expectedValue = componentProp.workerInfo.expectedParam;
if (expectedValue == null) return;
const validationResult = await executeValidate(expectedValue, this.param);
if (validationResult.error) {
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/validate_query_shield.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Shield } from "../abstracts";
import { RouteHandler } from "../handlers";
import { executeValidate } from "./execute_validate";

export class ValidateQueryShield extends Shield {
async protect() {
const componentProp = this['componentProp_'];
const expectedQuery = RouteHandler.getExpectedQuery(componentProp.controllerName, this.workerName);
const expectedQuery = componentProp.workerInfo.expectedQuery;
if (expectedQuery == null) return;
const validationResult = await executeValidate(expectedQuery, this.query);
if (validationResult.error) {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/worker_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export type IWorkerInfo = {
values: any[];
expectedQuery?: any;
expectedBody?: any;
expectedParam?: any;
cache?: ICacheOptionStored;
};
1 change: 1 addition & 0 deletions src/models/worker_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class WorkerInfo implements IWorkerInfo {
this.values = value.values;
this.expectedQuery = value.expectedQuery;
this.expectedBody = value.expectedBody;
this.expectedParam = value.expectedParam;
this.pattern = value.pattern;
this.cache = value.cache;
}
Expand Down

0 comments on commit 5ab068b

Please sign in to comment.