forked from xiangsx/gpt4free-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
51 lines (48 loc) · 1.26 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Context, Next } from 'koa';
import Joi, { string, ValidationOptions } from 'joi';
import { ComError, retryFunc } from './index';
export const checkBody = (
schema: {
[key: string]: Joi.Schema;
},
options?: {} & ValidationOptions,
) => {
return async (ctx: Context, next: Next) => {
const { body } = ctx.request;
const { error } = Joi.object(schema).validate(body, options);
if (error) {
throw new ComError(error.message, ComError.Status.ParamsError);
}
await next();
};
};
export const checkQuery = (
schema: {
[key: string]: Joi.Schema;
},
options?: {} & ValidationOptions,
) => {
return async (ctx: Context, next: Next) => {
const { query } = ctx.request;
const { error } = Joi.object(schema).validate(query, options);
if (error) {
throw new ComError(error.message, ComError.Status.ParamsError);
}
await next();
};
};
export const checkParams = (
schema: {
[key: string]: Joi.Schema;
},
options?: {} & ValidationOptions,
) => {
return async (ctx: Context, next: Next) => {
const { params } = ctx;
const { error } = Joi.object(schema).validate(params, options);
if (error) {
throw new ComError(error.message, ComError.Status.ParamsError);
}
await next();
};
};