AJV validation: inconsistent dataPath

Viewed 838

I have an AJV schema like this:

// 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
};

When I run my validation test against this schema, the result for missing object_city is:

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

But the result for a zip code being shorter than minLength is:

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

Please note the differences:

  • failed validation on required returns a blank dataPath, but failed validation on minLength returns '.object_zip' as dataPath
  • failed validation on required returns '#/required' as schemaPath and failed validation on minLength returns '#/properties/object_zip/minLength' as schemaPath

So here is my question: how can I get a consistent error handling?

0 Answers
Related