dongni9825 2018-05-17 12:31
浏览 17

尊重验证可选密钥

Sometimes users can send some missing key/value pairs. So in that situation I need to validate optional keys if they exists.

User biography is an optional field. If user leaves it empty I don't want it to be posted.

v::key('biography', v::optional(v::stringType()->length(10, 1000)))

Abode code validates if biography is not null or empty, if posted object does not contain biography key it raise exception, because validator expect biography. I couldn't find the way to check "if the key exists continue validation chain"; I can add not existing keys into the posted data before validation but I believe there is a better way to do this in the library.

I am looking a solution that should like this:

v::key('biography', v::keyExist(v::optional(v::stringType()->length(10, 1000))))
  • 写回答

2条回答 默认 最新

  • doubang4881 2018-05-19 15:19
    关注

    The documentation of Key states:

    Third parameter makes the key presence optional:

    v::key('lorem', v::stringType(), false)->validate($dict); // true

    That said, if "lorem" does not exists, Validation won't apply the StringType validation.

    See: http://respect.github.io/Validation/docs/key.html

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?