d780 / valparams Goto Github PK
View Code? Open in Web Editor NEWweb参数验证工具
License: MIT License
web参数验证工具
License: MIT License
date: { type: 'date', required: false, defValue: null, desc: '收集日期' }
校验后的时间会被格式化成
date :Fri Jan 22 2021 08:00:00 GMT+0800 (China Standard Time)
请问应该怎么处理?
待验证的表单
save2server---form>
{
"id":"29",
"wordsList":[
{
"originTxt":"1, determines
决心
确定
",
"en":"determines",
"lineOne":"1, determines ",
"explain":"决心
确定",
"showExplain":true
},
{
"originTxt":"
2, access ['ækses]
接入
n. 进入;使用权;通路
vt. 使用;存取;接近",
"en":"access",
"lineOne":"2, access ['ækses]",
"explain":"接入
n. 进入;使用权;通路
vt. 使用;存取;接近",
"showExplain":true
}
]
}
验证代码
async addWords() {
console.log('addWords------>')
let { ctx, app } = this;
// 参数验证
ctx.validate({
id: { type: 'string', required: true, range: { min: 1, max: 30 }, desc: '必须要有id' },
wordsList: {
type: 'array',
desc: '数组错误',
},
});
let { id, wordsList, } = ctx.request.body;
console.log( wordsList)
console.log(typeof (wordsList[0]))
}
console.log结果
addWords------>
[ '[object Object]', '[object Object]' ]
string
如果不用验证的时候是正常的
验证代码
async addWords() {
console.log('addWords------>')
let { ctx, app } = this;
// // 参数验证
// ctx.validate({
// id: { type: 'string', required: true, range: { min: 1, max: 30 }, desc: '必须要有id' },
// wordsList: {
// type: 'array',
// desc: '数组错误',
// // items: {
// // type: 'object',
// // properties: {
// // en: {
// // type: 'string',
// // },
// // explain: {
// // type: 'string',
// // },
// // lineOne: {
// // type: 'string',
// // },
// // originTxt: {
// // type: 'string',
// // },
// // showExplain: {
// // type: 'bool',
// // },
// // },
// // }
// },
// });
let { id, wordsList, } = ctx.request.body;
console.log( wordsList)
console.log(typeof (wordsList[0]))
}
console.log结果
addWords------>
[
{
originTxt: '1, determines \n决心 \n确定\n',
en: 'determines',
lineOne: '1, determines ',
explain: '决心 \n确定',
showExplain: true
},
{
originTxt: "\n2, access ['ækses]\n接入\n\nn. 进入;使用权;通路 \nvt. 使用;存取;接近",
en: 'access',
lineOne: "2, access ['ækses]",
explain: '接入\nn. 进入;使用权;通路 \nvt. 使用;存取;接近',
showExplain: true
}
]
object
请问要怎么做才能正确返回数组对象呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.