本文整理汇总了TypeScript中express.Request.check方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Request.check方法的具体用法?TypeScript Request.check怎么用?TypeScript Request.check使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类express.Request
的用法示例。
在下文中一共展示了Request.check方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: RouterError
const validateParams = (params: { entity: ISong, admin: IAdmin }, req: Request, log: Log) => {
if (!params.entity) {
log.error('params', params)
return ERRORS.PARAMS
}
req.check('entity.name')
.notEmpty()
.isLength({ max: 100 }).withMessage('存储 name 不正确')
req.check('entity.name')
.notEmpty()
.isLength({ max: 100 }).withMessage('存储 key 不正确')
req.check('entity.title').notEmpty().isLength({ max: 100 }).withMessage('歌曲名称不正确')
req.check('entity.artist').notEmpty().isLength({ max: 100 }).withMessage('歌手名称不正确')
req.check('entity.album').notEmpty().isLength({ max: 100 }).withMessage('专辑名称不正确')
req.check('entity.size').notEmpty().isLength({ max: 100 }).withMessage('歌曲文件信息不正确')
req.check('entity.mimeType').notEmpty().isLength({ max: 100 }).withMessage('歌曲文件类型不正确')
req.check('entity.duration').notEmpty().isLength({ max: 100 }).withMessage('歌曲时长不正确')
req.check('entity.image._id').notEmpty().isLength({ max: 100 }).withMessage('歌曲封面信息不正确')
const errors = req.validationErrors()
if (errors) {
log.error('params', { params, errors })
return new RouterError(2, errors[0].msg)
}
if (isNaN(+params.entity.duration)) {
log.error('params', params)
return new RouterError(3, '歌曲时长信息不正确')
}
if (isNaN(+params.entity.size)) {
log.error('params', params)
return new RouterError(4, '歌曲文件信息不正确')
}
return null
}
示例2: RouterError
const validateParams = (params: { entity: SimpleBlog, admin: IAdmin }, req: Request, log: Log) => {
if (!params.entity) {
log.error('params', params)
return ERRORS.PARAMS
}
req.check('entity.title')
.notEmpty().withMessage('文章标题必须填写')
.isLength({ max: 120 }).withMessage('文章标题必须在 120 个字符内')
req.check('entity.context').notEmpty().withMessage('文章内容不允许为空')
req.check('entity.summary').notEmpty().withMessage('文章描述不能为空')
req.check('entity.category').notEmpty().withMessage('分类不能为空')
const errors = req.validationErrors()
if (errors) {
log.error('params', { params, errors })
return new RouterError(2, errors[0].msg)
}
if (!Array.isArray(params.entity.tags)) {
log.error('params', params)
return new RouterError(2, '标签信息不正确')
}
return null
}
示例3: RouterError
const validateParams = (params: { entity: SimpleArticle, admin: IAdmin }, req: Request, log: Log) => {
if (!params.entity) {
log.error('params', params)
return ERRORS.PARAMS
}
req.check('entity.title')
.notEmpty().withMessage('文章标题必须填写')
.isLength({ max: 120 }).withMessage('文章标题必须在 120 个字符内')
req.check('entity.context').notEmpty().withMessage('文章内容不允许为空')
req.check('entity.summary')
.notEmpty().withMessage('文章描述不能为空')
.isLength({ max: 200 }).withMessage('文章描述只能在 200 字以内')
req.check('entity.songId').notEmpty().isLength({ max: 100 }).withMessage('歌曲信息不正确')
req.check('entity.posterId').notEmpty().isLength({ max: 100 }).withMessage('文章图片不正确')
const errors = req.validationErrors()
if (errors) {
log.error('params', { params, errors })
return new RouterError(2, errors[0].msg)
}
return null
}
示例4: async
export const comment = async (req: Request, res: Response) => {
const params = Object.assign({}, req.query, req.body)
log.info('comment.call', params)
// 验证 blog ID
const blogId = req.body.blogId
if (!blogId || !regMongodbId.test(blogId)) {
return res.json(ERRORS.BLOGNOTFOUND.toJSON())
}
// 校验昵称和 email
req.check('nickname')
.trim()
.notEmpty().withMessage('昵称不能为空')
.isLength({ max: 30 }).withMessage('昵称不允许超过 30 个字符')
req.check('email')
.trim()
.notEmpty().withMessage('email 不能为空')
.isEmail().withMessage('email 格式不正确')
.isLength({ max: 60 }).withMessage('email 不允许超过 60 个字符')
req.check('context')
.trim()
.notEmpty().withMessage('评论内容不能为空')
.isLength({ max: 140 }).withMessage('评论内容不允许超过 140 个字符')
const errors = req.validationErrors()
if (errors) {
log.error('comment.params', { params, errors })
// 不需要返回太详细的信息,因为前端已经做过校验
return res.json(ERRORS.COMMENTERROR.toJSON())
}
/**
* 站点
*/
let site = params.site
// 传入了站点,则校验站点
if (site && site.trim() && !checkUri(site)) {
return res.json(ERRORS.COMMENTERROR.toJSON())
} else {
// 修正站点信息
site = site ? resolveHTTPUri(site) : site
}
const nickname = params.nickname.trim()
const email = params.email.trim()
if (!checkEmail(email)) {
return res.json(ERRORS.COMMENTERROR.toJSON())
}
/**
* 用户评论内容
*/
const context = params.context.trim()
/**
* 查找对应的 blog
*/
const blog = await queryBlogById(blogId)
if (!blog) {
return res.json(ERRORS.BLOGNOTFOUND.toJSON())
}
// 看看用户信息是否有更新,如果有更新,则对整个用户信息进行更新
const userInfo = await diffUserAndUpdate(res.locals.user, {
// 修正管理员数据
role: res.locals.admin ? UserRole.ADMIN : UserRole.NORMAL,
nickName: nickname,
email,
site,
})
// 有修改,则修正全局引用
if (userInfo.updated) {
res.locals.user = userInfo.user
}
/**
* hash
*/
// ===== 回复逻辑
if (params.commentId) {
let newReplyModel: IReply
if (!regMongodbId.test(params.commentId)) {
return res.json(ERRORS.COMMENTERROR.toJSON())
}
// 进入回复逻辑分支
const commentModel = await queryCommentById(params.commentId)
if (!commentModel) {
return res.json(ERRORS.COMMENTNOTFOUND.toJSON())
}
if (params.replyId) {
// ==== 回复类型 1:针对评论中回复的回复
if (!regMongodbId.test(params.replyId)) {
return res.json(ERRORS.COMMENTERROR.toJSON())
}
// 查找对应的回复对象
// const comment = await queryCommentByReplyId(commentModel._id, params.replyId)
let reply: IReply
// 可以直接遍历上面的 commentModel 对象...不用再查数据库了
if (commentModel.replys.length) {
reply = commentModel.replys.find(reply => {
//.........这里部分代码省略.........