-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
56 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |