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

support convert object #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlbertE1990
Copy link

项目使用egg开发,使用内置的egg-validate作为参数验证,有幸接触到本项目。由于参数验证中不能使用自定义函数对对象进行转换,所以会造成一些不便。
例如前端用户get方式传递参数{ids:[1,2,3]},url编码后url为 http://xxx.com?ids[]=1&ids[]=2&ids[]=3,后端接受到的参数{ids:['1','2','3']},数组中每个项目均为字符串。此时如果使用以下验证对象会验证失败。

const rules = {
    ids:{
        type:'array',
        itemType:'int',
    }
}

已经实现以下转换方式,只有在自定义convertType为函数的情况下,才支持对对向的转换。且兼容之前任何操作

const p = new Parameter({
    convert:true
})
const data = {
    ids:['1','2','3']
}
const rules = {
    ids:{
        type:'array',
        itemType:'int',
        convertType:(val) => {
            return val.map(item => Number(item))
        },
    }
}
p.validate(rules,data)

@AlbertE1990 AlbertE1990 closed this Feb 4, 2021
@AlbertE1990 AlbertE1990 reopened this Feb 4, 2021
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

Successfully merging this pull request may close these issues.

1 participant