【问题标题】:How to prohibit certain words in a json value with cerberus如何使用 cerberus 禁止 json 值中的某些单词
【发布时间】:2021-07-15 18:47:08
【问题描述】:

假设我有这个 json:

{"name": "John"}

我想限制 json 包含子字符串“我的名字是”,所以如果我收到:

{"name": "my name is John"}

Cerberus 会告诉我 json 不正确,我可以显示正确的输出,到目前为止我已经尝试了 "forbidden" 字段但它不起作用,因为禁止只使用确切的短语,这就是我的方式地狱犬验证:

{
  "name":{
    "type": "string",
    "minlength":1,
    "forbidden":["my name is"],
  }
}

谢谢!

【问题讨论】:

    标签: python json python-3.x cerberus


    【解决方案1】:

    看起来你会想要使用check_with 和一个简单的函数来禁止你想要排除的内容,例如

    def cleanName(field, value, error):
        if 'my name is' in value:
            error(field, 'error message')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      相关资源
      最近更新 更多