82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
const Joi = require('joi');
|
||
|
||
/**
|
||
* Joi 输入校验中间件
|
||
*/
|
||
const validate = (schema) => {
|
||
return (req, res, next) => {
|
||
const { error } = schema.validate(req.body, { abortEarly: true });
|
||
if (error) {
|
||
return res.status(400).json({
|
||
success: false,
|
||
message: '输入校验失败: ' + error.details[0].message
|
||
});
|
||
}
|
||
next();
|
||
};
|
||
};
|
||
|
||
module.exports = { validate };
|
||
|
||
// ─── 常用校验 Schema ───────────────────────────────────────────
|
||
|
||
const schemas = {
|
||
// 门店登录(用 username)
|
||
storeLogin: Joi.object({
|
||
username: Joi.string().min(1).max(50).required(),
|
||
password: Joi.string().min(6).max(20).required()
|
||
}),
|
||
|
||
// 骑手登录(用 phone)
|
||
login: Joi.object({
|
||
phone: Joi.string().pattern(/^1[3-9]\d{9}$/).required(),
|
||
password: Joi.string().min(6).max(20).required()
|
||
}),
|
||
|
||
// 车辆创建/更新
|
||
vehicle: Joi.object({
|
||
storeId: Joi.string().allow(''),
|
||
frameNumber: Joi.string().allow(''),
|
||
plateNumber: Joi.string().allow(''),
|
||
brand: Joi.string().allow(''),
|
||
vehicleType: Joi.string().allow(''),
|
||
color: Joi.string().allow(''),
|
||
batteryType: Joi.string().allow(''),
|
||
batteryCapacity: Joi.number().min(0).max(200),
|
||
batteryStatus: Joi.string().valid('正常', '老化', '待更换'),
|
||
status: Joi.string().valid('空闲', '在租', '维修中', '已报废', '待回收')
|
||
}),
|
||
|
||
// 订单
|
||
order: Joi.object({
|
||
customer: Joi.string().allow(''),
|
||
vehicle: Joi.string().allow(''),
|
||
contractMonths: Joi.number().min(0).allow(''),
|
||
startDate: Joi.date().allow(''),
|
||
endDate: Joi.date().allow(''),
|
||
rentalFee: Joi.number().min(0).allow(''),
|
||
deposit: Joi.number().min(0).allow(''),
|
||
orderType: Joi.string().allow('')
|
||
}),
|
||
|
||
// 客户
|
||
customer: Joi.object({
|
||
name: Joi.string().min(1).max(50).required(),
|
||
phone: Joi.string().pattern(/^1[3-9]\d{9}$/).required(),
|
||
idCard: Joi.string().allow(''),
|
||
address: Joi.string().allow(''),
|
||
email: Joi.string().email().allow('')
|
||
}),
|
||
|
||
// 门店
|
||
store: Joi.object({
|
||
name: Joi.string().min(1).max(100).required(),
|
||
address: Joi.string().allow(''),
|
||
phone: Joi.string().allow(''),
|
||
manager: Joi.string().allow(''),
|
||
status: Joi.string().valid('营业中', '休息中', '已关闭')
|
||
})
|
||
};
|
||
|
||
module.exports.schemas = schemas;
|