一个表字段多时候,直接校验对象
来源:4-1 参数获取与LinValidator校验器

慕粉4333725
2019-11-14
老师,您好。我对服务端开发经验不多,想向您请教一个问题:因为前端一个接口需要上传的数据较多,所以我直接传递了一个obj到服务端。那这个时候我使用lin-Validator校验,不知道如何处理,请您指导。代码如下:
// 新增商品接口
router.post('add_goods', new Auth().m, async (ctx, next)=>{
const v = await new AddGoodsValidator().validate(ctx)
const goods = await Goods.addGoods(v.get('body'))
// art.exclude = ['index','like_status']
ctx.body = goods
})
// 新增商品校验器
class AddGoodsValidator extends LinValidator {
constructor(){
super()
this.data.name,this.data.desc,this.data.specs = [
new Rule('isNotEmpty','内容不能为空'),
new Rule("isLength", "长度必须大于1", 1)
]
this.data.cost_price,this.data.sel_price,this.data.dis_price = [
new Rule('isFloat','金额不符合规范',{
min:0.01
})
]
}
}
目前使用这种方式,貌似没有取到数据。
补充代码:
data() {
return {
// 商品信息对象
goodsInfo:{
name:'',
image:'',
desc:'',
specs:'',
cost_price:'',
sel_price:'',
dis_price:'',
goods_type:''
},
// module实例化
goodsModel:new GoodsModel()
}
},
/****************************************************/
// 新增商品请求
this.goodsModel.addGoods(
this.goodsInfo
)
.then(res => {
// 商品保存成功
if(res && res.error_code == 0) {
uni.showToast({
title: '商品保存成功',
icon: 'success',
duration: 2000
})
setTimeout(function () {
uni.navigateBack({
delta: 1,
animationType: 'pop-out',
animationDuration: 200
});
}, 2000);
}
console.log(res)
})
/****************************************************/
// 新增商品API
addGoods(obj){
return this.request({
url:'goods/add_goods',
data:obj,
method:'POST'
})
}
写回答
2回答
-
慕粉4333725
提问者
2019-11-15
// 对象 data() { return { goodsInfo:{ name:'', image:'', desc:'', specs:'', cost_price:'', sel_price:'', dis_price:'', goods_type:'' }, goodsModel:new GoodsModel() } }, /****************************************************/ // 新增商品请求 this.goodsModel.addGoods( this.goodsInfo ) .then(res => { // 商品保存成功 if(res && res.error_code == 0) { uni.showToast({ title: '商品保存成功', icon: 'success', duration: 2000 }) setTimeout(function () { uni.navigateBack({ delta: 1, animationType: 'pop-out', animationDuration: 200 }); }, 2000); } console.log(res) }) /****************************************************/ // 新增商品API addGoods(obj){ return this.request({ url:'goods/add_goods', data:obj, method:'POST' }) }
00 -
7七月
2019-11-15
你应该直接把post的数据结构给我贴出来看下
022019-11-16
相似问题