AJV验证:dataPath不一致

时间:2017-07-22 09:16:56

标签: javascript node.js jsonschema json-schema-validator ajv

我有一个像这样的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' }

请注意区别:

  • 对所需返回的验证失败空白 dataPath ,但minLength验证失败,将&#39; .object_zip&#39; 返回为< EM>数据路径
  • 无法验证所需的退货&#39;#/ required&#39; 作为 schemaPath ,并且minLength上的验证失败,返回&#39;#/ properties / object_zip / minLength&#39; as schemaPath

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

0 个答案:

没有答案