Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJV验证:数据路径不一致_Javascript_Node.js_Jsonschema_Json Schema Validator_Ajv - Fatal编程技术网

Javascript AJV验证:数据路径不一致

Javascript AJV验证:数据路径不一致,javascript,node.js,jsonschema,json-schema-validator,ajv,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:

我有这样一个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
};
当我对此架构运行验证测试时,缺少对象城市的结果是:

{ 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的验证失败将返回一个空白的数据路径,但对minLength的验证失败将返回'。object_zip'作为数据路径
  • 对作为schemaPath返回的必需返回“#/required”的验证失败,而对作为schemaPath返回的minLength“#/properties/object\u zip/minLength”的验证失败

因此,我的问题是:如何获得一致的错误处理?

同样的问题。也许你应该在他们的bugtracker上把这个作为一个问题提交。如果我知道它是否是一个bug,我会提交的。但由于当时似乎没有人有同样的问题,我得出结论,这很可能是我的错,而不是AJV的错误。因此,我选择编写自己的验证并放弃AJV。我尽量小心地声称某个东西是错误,因为大多数时候,这代表我是一个错误。
必需的
验证存在于根架构
#/
上,即当
对象(u zip
丢失时,根对象缺少此属性,因此被认为验证失败。另一方面,
minLength
的验证存在于子架构
#/properties/object_-zip
上,即当长度不足时,
object_-zip
属性被视为验证失败。根据json模式规范,这是正确的行为。如果您想要不同的行为,您应该考虑使用不同的库,例如JUI,而不是基于JSON模式。