diff --git a/src/routers/post-router.ts b/src/routers/post-router.ts index 66c16a1..74d726c 100644 --- a/src/routers/post-router.ts +++ b/src/routers/post-router.ts @@ -2,7 +2,7 @@ import 'reflect-metadata' import {Container} from "typedi" import {PostService} from "../services/post-service" import express from "express" -import {body, query} from "express-validator" +import {body, oneOf, query} from "express-validator" import * as Validator from "validator" import {hasValidationErrors} from "../includes" import {JWTUserPayload} from "../types" @@ -11,9 +11,13 @@ let postService = Container.get(PostService) let postRouter = express.Router() postRouter.post('/submit', - body('content').notEmpty().withMessage('请输入内容') + oneOf([ + body('content').exists(), + body('image').exists() + ]), + body('content').optional().notEmpty().withMessage('请输入内容') .escape(), - body('image').isURL({host_whitelist: [/.*\.360buyimg\.com/]}).withMessage('仅支援*.360buyimg.com下的图片'), + body('image').optional().isURL({host_whitelist: [/.*\.360buyimg\.com/]}).withMessage('仅支援*.360buyimg.com下的图片'), async (req: express.Request, res: express.Response) => { if (hasValidationErrors(req, res)) return res.json(await postService.submit(req.user as JWTUserPayload, req.body.content, req.body.image))