Javascript 类型的Ajv自定义错误消息

Javascript 类型的Ajv自定义错误消息,javascript,json,validation,ajv,Javascript,Json,Validation,Ajv,我正在探索带有Ajv错误的Ajv,以验证json模式并生成自定义错误消息。目前一切正常,但我无法为单个值的类型设置自定义错误消息 const emailSchema = { type: 'object', required: ['foo', 'bar', 'car'], properties: { foo: { type: 'integer' }, bar: { type: 'string' }, car: { type: 'string' } }, errorMessag

我正在探索带有Ajv错误的Ajv,以验证json模式并生成自定义错误消息。目前一切正常,但我无法为单个值的类型设置自定义错误消息

const emailSchema = {
 type: 'object',
 required: ['foo', 'bar', 'car'],
 properties: {
  foo: { type: 'integer' },
  bar: { type: 'string' },
  car: { type: 'string' }
 },
 errorMessage: {
  type: 'should be an object',
  required: {
  foo: 'foo field is missing',
  bar: 'bar field is missing',
  car: 'car field is missing'
  }
 } 
};
输出跟踪误差

[
    {
        "keyword": "type",
        "dataPath": "/foo",
        "schemaPath": "#/properties/foo/type",
        "params": {
            "type": "integer"
        },
        "message": "should be integer"
    },
    {
        "keyword": "errorMessage",
        "dataPath": "",
        "schemaPath": "#/errorMessage",
        "params": {
            "errors": [
                {
                    "keyword": "required",
                    "dataPath": "",
                    "schemaPath": "#/required",
                    "params": {
                        "missingProperty": "bar"
                    },
                    "message": "should have required property 'bar'"
                }
            ]
        },
        "message": "bar field is missing"
    },
    {
        "keyword": "errorMessage",
        "dataPath": "",
        "schemaPath": "#/errorMessage",
        "params": {
            "errors": [
                {
                    "keyword": "required",
                    "dataPath": "",
                    "schemaPath": "#/required",
                    "params": {
                        "missingProperty": "car"
                    },
                    "message": "should have required property 'car'"
                }
            ]
        },
        "message": "car field is missing"
    }
]
第一个错误对象显示消息“应该是整数”,我可以像foo必须是整数一样对其进行自定义吗。 我期待下面这样的东西,但它给出了一个模式错误

type : {
  foo : "foo must be an Integer"
}

谢谢。

您必须在每个属性中声明
errorMessage
为关键字,请参见以下示例:

const-emailSchema={
类型:“对象”,
必需:['foo','bar','car'],
特性:{
傅:{
键入:“整数”,
错误消息:{
//这里必须是errorMessage而不是errorMessages
键入:“foo必须是整数”,//自定义错误消息
},
},
条:{type:'string'},
汽车:{type:'string'},
},
错误消息:{
//从errorMessage更改为errorMessages
类型:“应该是对象”,
所需:{
foo:“foo字段丢失”,
bar:'缺少bar字段',
汽车:“汽车场不见了”,
},
},
}

对于有一些自定义错误消息或任何其他数据的用例,我们必须使用模式路径。 当我们得到验证错误时,我们还得到了
error.keyword
,在我的例子中,if和else块中有额外的验证,如下所示

schema.allOf= Object.keys(bankCodes).map((key: any) => ({
    if: {
      properties: {
        routingCodeType1: { const: bankCodes[key].code },
      },
    },
    then: {
      properties: {
        routingCodeValue1: {
          pattern: bankCodes[key].pattern, //<-- this was cause of validation fail
          errorMessage: bankCodes[key].errorMessage,
        },
      },
    },
  }))
通过这种方式,我们可以提取get custom errorMessage或我们想要的任何其他数据

要点是使用schemaPath属性

const getPatternMessage = (error: any, schema: any) => {
  if (error.keyword === 'pattern') {
    const fieldName = error.dataPath.substring(1); // routingCodeValue1
    const keyArr = error.schemaPath.split('/'); // ['#','allOf','2'..,'pattern']
    keyArr.pop(); // remove '#'
    keyArr.shift(); // remove 'pattern'
    const prop = keyArr.reduce((acc, key) => acc[key], schema);
/** 
prop contains  {
          pattern: '^[a-z]{9}$',
          errorMessage:'routingCodeValue1 should be 9 characters'
        },
*/
    return {
      [fieldName]: prop.errorMessage,
    };
  }
};