小编Nik*_*hil的帖子

类型的 Ajv 自定义错误消息

我正在使用 ajv-errors 探索 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'
  }
 } 
};
Run Code Online (Sandbox Code Playgroud)

输出跟随误差

[
    {
        "keyword": "type",
        "dataPath": "/foo",
        "schemaPath": "#/properties/foo/type",
        "params": {
            "type": "integer"
        },
        "message": "should be integer"
    }, …
Run Code Online (Sandbox Code Playgroud)

javascript validation json ajv

5
推荐指数
1
解决办法
5607
查看次数

标签 统计

ajv ×1

javascript ×1

json ×1

validation ×1