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

自定义校验如何使用 #44

Open
YDJSZY opened this issue Jul 18, 2018 · 1 comment
Open

自定义校验如何使用 #44

YDJSZY opened this issue Jul 18, 2018 · 1 comment

Comments

@YDJSZY
Copy link

YDJSZY commented Jul 18, 2018

不好意思,实在没看明白自定义规则如何使用。
const rule = { title: 'myRule' };
let r = ctx.validate(rule, ctx.request.body);
r 一直是undefined

@vardonhan
Copy link

vardonhan commented Dec 23, 2020

首先添加一个自定义校验规则:
app.validator.addRule(type, (rule, value) => {...})
注意这里面的type,我被坑了很久,按照你的例子,type应该是myRule
然后就是校验:
const rule = { title: {type: 'myRule'} };
let r = ctx.validate(rule, ctx.request.body); 这种方式没有返回值
可以用try catch
try {
ctx.validate(rule, ctx.request.body)
} catch (error) {
...
}

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

2 participants