Skip to content

Commit

Permalink
fix(#20): validator middleware returns 400 res
Browse files Browse the repository at this point in the history
  • Loading branch information
barthofu committed Aug 19, 2022
1 parent 36be0ca commit 1f21a03
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/api/controllers/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Delete, Get, Middleware, Post, Router } from "@discordx/koa"
import { Client } from "discordx"
import { Context } from "koa"
import { injectable } from "tsyringe"
import validator, { Joi } from 'koa-context-validator'
import { Joi } from 'koa-context-validator'

import { BaseController } from "@utils/classes"
import { authenticated, botOnline } from "@api/middlewares"
import { authenticated, botOnline, validator } from "@api/middlewares"
import { BaseGuildTextChannel, BaseGuildVoiceChannel, Channel, ChannelType, Guild as DGuild, GuildTextBasedChannel, NewsChannel, PermissionsBitField, User as DUser } from "discord.js"
import { generalConfig } from "@config"
import { isInMaintenance, setMaintenance } from "@utils/functions"
Expand Down
4 changes: 2 additions & 2 deletions src/api/controllers/database.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Get, Middleware, Post, Router } from "@discordx/koa"
import { injectable } from "tsyringe"
import { Context } from "koa"
import validator, { Joi } from "koa-context-validator"
import { Joi } from "koa-context-validator"

import { Database } from "@services"
import { BaseController } from "@utils/classes"
import { formatDate } from "@utils/functions"

import { databaseConfig } from "@config"
import { authenticated } from "@api/middlewares"
import { authenticated, validator } from "@api/middlewares"

@Router({ options: { prefix: "/database" } })
@Middleware(
Expand Down
6 changes: 3 additions & 3 deletions src/api/controllers/stats.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { injectable } from "tsyringe"
import { Get, Middleware, Router } from "@discordx/koa"
import { Joi } from "koa-context-validator"
import { Context } from "koa"

import { BaseController } from "@utils/classes"
import { injectable } from "tsyringe"
import { Stats } from "@services"
import { authenticated } from "@api/middlewares"
import validator, { Joi } from "koa-context-validator"
import { authenticated, validator } from "@api/middlewares"

@Router({ options: { prefix: '/stats' }})
@Middleware(
Expand Down
3 changes: 2 additions & 1 deletion src/api/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './log'
export * from './botOnline'
export * from './authenticated'
export * from './authenticated'
export * from './validator'
47 changes: 47 additions & 0 deletions src/api/middlewares/validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Joi, { ValidationOptions } from "joi";
import { Context, Next } from "koa";

type InputSchema = {
query?: Joi.ObjectSchema
headers?: Joi.ObjectSchema
body?: Joi.ObjectSchema
params?: Joi.ObjectSchema
}

const isKeyOnContext = (key:string) => ['params'].includes(key)

export const validator = (inputSchema: InputSchema, options: ValidationOptions = {}) => (ctx: Context, next: Next) => {

const validateOptions = {
...options,
context: {
...ctx,
...options.context
},
}
const keys = Object.keys(inputSchema)

for (const key of keys) {

const targetSchema = inputSchema[key as keyof typeof inputSchema]
if (!targetSchema) continue

let source, value: string
if (isKeyOnContext(key)) {
source = ctx
value = ctx[key]
} else {
source = ctx.request
value = ctx.request[key as keyof typeof ctx.request]
}

const validatedValue = targetSchema.validate(value, validateOptions)
if (validatedValue.error) {
ctx.status = 400
ctx.body = validatedValue.error.message
return
}
}

next()
}

0 comments on commit 1f21a03

Please sign in to comment.