You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Cerberus禁止JSON值中包含指定子字符串

解决方案

Cerberus的forbidden规则仅校验值是否与列表内的元素完全相等,无法实现子串匹配校验,你可以使用regex正则规则实现禁止包含指定子串的需求:

{
  "name":{
    "type": "string",
    "minlength": 1,
    "regex": "^(?!.*my name is).*$",
    "error_messages": {
      "regex": "name字段不能包含子字符串'my name is'"
    }
  }
}

上述规则里的正则^(?!.*my name is).*$表示字符串任意位置都不允许出现my name is子串,不符合规则时会返回你自定义的错误提示,实际校验效果如下:

  • 输入{"name": "John"}:校验通过
  • 输入{"name": "my name is John"}:校验失败,返回对应错误提示

内容的提问来源于stack exchange,提问作者Casanalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.12 12:45:03