当前位置:网站首页>nodejs 定义错误验证机制
nodejs 定义错误验证机制
2022-07-16 21:35:00 【你的美,让我痴迷】
//专门验证机制
/schema/user.js 用户验证机制
//导入定义规则的包
const joi=require('joi')
//定义用户名或密码的验证规则
const username=joi.string().alphanum().min(1).max(10).required().error(new Error('用户名最少1位,最多10位,只能是字母或者数字'))
const password=joi.string().pattern(/^[\S]{6,12}$/).required().error(new Error('密码最少6位,最多12位'))
//参考
// username:Joi.string().min(2).max(12).required().error(new Error('用户名不符合验证规则')),
// email:Joi.string().email().required().error(new Error('邮箱格式不符合要求')),
// password:Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/).required().error(new Error('密码格式不符合要求')),
// role:Joi.string().valid('normal','admin').required().error(new Error("角色值非法")),
// state:Joi.number().valid(0,1).required().error(new Error('状态值非法'))
//定义注册或登录表单数据的规则对象
exports.reg_login_schema={
body:{
username,
password
},
}
使用:
//2.导入需要的验证规则对象
const {
reg_login_schema}=require('../schema/user')
//注册新用户
router.post('/reguser',expressJoi(reg_login_schema), user_handler.regUser)
//登录
router.post('/login',expressJoi(reg_login_schema),user_handler.login)
全局定义:
//导入定义规则的包
const joi=require('@hapi/joi')
//定义错误级别文件
app.use((err,req,res,next)=>{
//验证失败导致的错误
if(err instanceof joi.ValidationError) return res.cc(err)
//未知错误
res.cc(err)
})
边栏推荐
- 广州铁骑霸气“公主抱”,安全感拉满!
- pytest接口自动化测试框架 | 对requests进行二次封装
- 10、摸清JVM运行状况
- 绝地求生 吃鸡 98k 不自动关镜子
- 微信小程序_14,组件的创建与引用
- There is no simpler and more practical single person pose estimation than this
- Comparative investigation of three commonly used time series databases - incluxdb, Prometheus, iotdb
- ImportError: cannot import name ‘Imputer‘ from ‘sklearn. preprocessing‘
- Compose Modify如何添加到后面
- 基于STM32的DHT11温湿度传感器设计
猜你喜欢
![[remote desktop] rustdesk open source remote desktop, a substitute for TeamViewer and sunflower](/img/f3/fff734a05fa543710e6582e3acbea1.png)
[remote desktop] rustdesk open source remote desktop, a substitute for TeamViewer and sunflower
![[development tutorial 3] crazy shell arm function mobile phone - Introduction to the whole board resources](/img/ca/9f5357fc09f5b71c626bc7f7cf36f3.png)
[development tutorial 3] crazy shell arm function mobile phone - Introduction to the whole board resources

ACM board

Introduction to sqlmap syntax

7、常见的垃圾回收器

Developers must see | devweekly issue 1: what is time complexity?

微信小程序_14,组件的创建与引用

Comparative investigation of three commonly used time series databases - incluxdb, Prometheus, iotdb

T-infinite Road

【C语言】strlen函数的讲解和模拟实现
随机推荐
Design of DHT11 temperature and humidity sensor based on stm32
单元测试界的高富帅,Pytest框架 (完) 测试报告篇
Jvm-sandbox leads to the investigation of the target service JVM Metaspace oom
Differences between user-defined hook and ordinary functions and function components
广州铁骑霸气“公主抱”,安全感拉满!
canal-deployer canal-adapter镜像构建,部署
【百度飞桨】手写数字识别模型部署Paddle Inference
作为一款时序数据库,TDengine 是如何实现并开源其分布式集群功能?
开户需要注意的是什么?请问手机开户股票开户安全吗?
leetcode-两数之和
OSPF comprehensive experiment
There is no simpler and more practical single person pose estimation than this
Cloud native: docker's practical experience (IV) deploying redis three master and three slave clusters on docker
Dameng database table SQL statement
10、摸清JVM运行状况
[remote desktop] rustdesk open source remote desktop, a substitute for TeamViewer and sunflower
(pytorch advanced road IV) vision transformer
Preliminary test of ZVS circuit
Compose 渐变色
【开发教程3】疯壳·ARM功能手机-整板资源介绍