Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

验证数组的时候出来的是'[object Object]',而不是数组的值 #5

Open
issmile opened this issue Apr 9, 2020 · 3 comments

Comments

@issmile
Copy link

issmile commented Apr 9, 2020

待验证的表单
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

请问要怎么做才能正确返回数组对象呢?

@D780
Copy link
Owner

D780 commented Jun 19, 2020

其实之前这个 array 是专门指用英文逗号分隔的字符串数组,因为这个参数验证主要考虑了接口的参数验证,这个是设计给 query 参数用的。
你这个其实更适合用 JSON, 在 range 中指定 { schema: { type: 'array' } }

ctx.validate({
    id: { type: 'string', required: true, range: { min: 1, max: 30 }, desc: '必须要有id' },
    wordsList: {
        type: 'json',
        desc: '数组错误',
        range: {
          schema: {
            type : 'array',
          }
        }
    },
});

我也想了下这个确实容易引起歧义
现在也加了兼容处理,现在使用

ctx.validate({
    id: { type: 'string', required: true, range: { min: 1, max: 30 }, desc: '必须要有id' },
    wordsList: { type: 'array', desc: '数组错误' },
});

也能识别处理你这样的参数传入(即支持 英文逗号分隔的字符串数组、数组对象、JSON 数组对象)

@KokoTa
Copy link

KokoTa commented Nov 17, 2020

@D780 对于 ids: [1,2,3] 如何验证参数为 int 类型?不是特别理解 希望获得解答。

@D780
Copy link
Owner

D780 commented Nov 24, 2020

@D780 对于 ids: [1,2,3] 如何验证参数为 int 类型?不是特别理解 希望获得解答。

针对这个一般有两种方式

  1. 参数传 1,2,3,4 这种用英文逗号分隔的字符串数组

    参数配置 { type: 'array', range: { reg: /(\d+,)*\d+/ } }
    通过正则表达式去验证字符串的格式
    但是最终处理后的参数值是 [ '1', '2', '3', '4' ],即里面内容仍然是字符串
    像在数据库中使用一般也没问题,有需要的话还需执行转换成整型

  2. 参数传 对象 或 JSON 字符串 如 [1,2,3,4] 或 '[1,2,3,4]'

    参数配置

    {
        type: 'json',
        range: {
            schema: {
                type: 'array',
                items: {
                    type: 'number'
                }
            }
        }
    }

    通过 JSONSchema 去进行 JSON 参数格式的验证
    这种就写起来会多一点
    但是能直接得到 [ 1, 2, 3, 4 ] 这样的结果

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants