3

我有一个这样的 AJV 架构:

// schema.js
module.exports = {
  title: 'task',
  description: 'A Task Schema',
  type: 'object',
  properties: {
    object_city: {
      title: 'City',
      type:'string'
    },
    object_zip: {
      title: 'Zip Code',
      type: 'string',
      maxLength: 5,
      minLength: 5
    }
  },
  required: ['object_zip', 'object_city'],
  additionalProperties: false
};

当我针对此架构运行验证测试时,缺少 object_city 的结果是:

{ keyword: 'required',
  dataPath: '',
  schemaPath: '#/required',
  params: { missingProperty: 'object_city' },
  message: 'should have required property \'object_city\'' }

但是邮政编码比 minLength 短的结果是:

{ keyword: 'minLength',
  dataPath: '.object_zip',
  schemaPath: '#/properties/object_zip/minLength',
  params: { limit: 5 },
  message: 'should NOT be shorter than 5 characters' }

请注意差异:

  • required 验证失败返回空白 dataPath,但 minLength 验证失败返回'.object_zip'作为dataPath
  • required 验证失败返回“#/required”作为schemaPath,minLength 验证失败返回“#/properties/object_zip/minLength”作为schemaPath

所以这是我的问题:如何获得一致的错误处理?

4

0 回答 0