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

如何使用JSON Pointer访问JSON Schema中的minLength属性?

解决动态访问JSON Schema中minLength属性的问题

针对你遇到的无法在errorMessage里动态获取minLength值的问题,核心是要根据你使用的验证库(大概率是AJV)的版本来用正确的模板变量:

  • 如果用的是AJV v8及以上版本:
    在minLength的错误消息里直接用${limit}变量,它会自动对应当前字段的minLength值。修改后的schema如下:

    export const schema = {
      type: 'object',
      properties: {
        inputControl: {
          type: 'string',
          format: 'email',
          minLength: 10,
          errorMessage: {
            minLength: 'String must contain at least ${limit} character(s)',
            format: 'Please enter valid email',
          }
        },
      },
    }
    
  • 如果是AJV v7及更早版本:
    需要通过${params.minLength}来访问规则参数,写法如下:

    errorMessage: {
      minLength: 'String must contain at least ${params.minLength} character(s)',
      // ...其他错误消息
    }
    

你之前试的${1/minLength}是JSON Pointer语法,但AJV的错误消息模板并不支持用这种方式引用当前字段的schema属性,而是提供了limit(针对长度类规则)、params这类内置变量来获取规则参数值。而${0#}能返回inputControl,是因为它是AJV模板里代表当前字段键名的变量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 12:22:35