uwazi icon indicating copy to clipboard operation
uwazi copied to clipboard

Improve ajv validation error reporting

Open LaszloKecskes opened this issue 2 years ago • 1 comments

If an ajv validator is failing, the error message will be a nondescript "validation failed", with no information about exactly which validation(s) fail. We do not use the ajv validators directly, but wrap it with wrapValidator from app/shared/tsUtils.ts. This function is supposed to catch the validator errors and wrap them into an error with more detailed information. This function doesn't work, since in the try branch the returned value is Promise, which will never get caught by the catch. If one tries to await, the catch works, and provides the more detailed output. Example (failing a thesaurus validation):

    ValidationError {
      message: 'validation failed',
      errors: [
        {
          keyword: 'uniqueName',
          dataPath: '',
          schemaPath: '#/uniqueName',
          params: { keyword: 'uniqueName' },
          message: 'should pass "uniqueName" keyword validation'
        }
      ],
      validation: true,
      ajv: true
    }

We should decide whether we would like to have these more detailed messages or not. They are very useful development purposes (debugging, testing).

If not: the issue is to remove the wrapValidator function, as without the await it is dead code that does nothing (except causing the minimal overhead of an always passing try-catch on object validation).

If yes:

  • decide how the reported errors should be formatted, also keeping possible frontend reporting in mind
  • fix and change wrapValidator
  • implement the frontend reporting
  • make sure that the ajv validators across the application are using it

LaszloKecskes avatar May 13 '22 11:05 LaszloKecskes

There are already pretty message formatter for AJV, this could be a quick win. Research.

txau avatar May 18 '22 16:05 txau