Egg: `egg-validate`插件的校验规则中`required:false`无效

Created on 15 Aug 2017  ·  6Comments  ·  Source: eggjs/egg

后端校验规则:
image

前端请求:
image

接口返回值:
image

not follow template

All 6 comments

三个月前做一个意见反馈页面,也发现了同类问题。代码如下:

            const createRule = {
                title: {
                    required: false,
                    type: 'string'
                },
                email: {
                    required: false,
                    allowEmpty: true,
                    type: 'email',
                },
                content: {
                    type: 'string'
                },
            };

我希望这个页面的联系方式字段是选填的,如果填写了,则做一下邮箱格式的校验。但是因为required:false无效,我把这个字段限制成必填了😂

过了这么长时间还是存在这个问题,为什么都没人反馈,太让人伤心。。

我是认为不好用,配置不必填相当麻烦。

不必填的话要配置 allowEmpty: true, required: false,

因为 egg-validate 依赖的 parameter 这个库是 node-modules 写的,我没意见。反正我是不用,宁愿自己写一个验证库。

@xjchenhao 提供一个最小可复现代码,这种问题的定位我们一般是会写一个测试用例来复现,https://github.com/eggjs/egg-validate/blob/master/test/validate.test.js#L34

@MinJieLiu 欢迎来到开源世界。

egg-validate 不是内置到 egg 的,使用者可以自由选择用或不用。

就跟在其他项目一样,对于某个类库,如果不好用:

  1. 跟类库的开发者提 issue,讨论改进方式
  2. 如果达成共识,就一起推动类库的进化
  3. 如果达不成共识,就选择其他的同类类库,如 egg-validate-schema
  4. 如果没有同类类库,或也不符合需求,就自己造一个咯。

这就是开源世界,express,koa,vue,react 都是这样的模式,egg 跟它们没啥区别。

设计如此

新的 RFC 里面有提到会做

发自我的 iPhone

在 2017年11月20日,17:02,Measy notifications@github.com 写道:

@atian25 egg-validate的readme关于自定义添加验证规则是不是应该详细一些,比如addRule的第二个参数需要是个fun,fun的返回值有什么要求,什么返回情况下是验证通过,什么情况下是验证不通过


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Was this page helpful?
0 / 5 - 0 ratings