6

To give some background, I am trying to add proper schema validation error message formatting (to sugercoat the validation errors) for AJV JSON Schema Validation. I am using Fastify middleware. My purpose is to wrap the default schema error validation messages to my own messages, as per my functional requirements to make it user friendly.

Now as I am using Fastify, I am adding it as a part of my plugin as follows:

const fastify = require('fastify')({
    ajv: {
        customOptions: { allErrors: true, jsonPointers: true },
        plugins: [
            require('ajv-merge-patch'),
            require('ajv-errors'),
        ]
    },
    requestIdHeader: 'x-service-request-id',
    requestIdLogLabel: 'requestId',
    genReqId: function (req) { return random.generate(10) }
});

I am using

"ajv-errors": "^3.0.0" "ajv-merge-patch": "^4.1.0", (both are latest from npm)

Now I am getting this error in yarn start:

λ yarn start
yarn run v1.22.10
warning ..\..\..\..\package.json: No license field
$ node src/server.js
node:internal/modules/cjs/loader:930
  throw err;
  ^

Error: Cannot find module 'ajv/dist/compile/codegen'
Require stack:
- service-infra\persistance\node_modules\ajv-errors\dist\index.js
- service-infra\persistance\src\server.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
    at Function.Module._load (node:internal/modules/cjs/loader:772:27)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:93:18)
    at Object.<anonymous> (service-infra\persistance\node_modules\ajv-errors\dist\index.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1095:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1124:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:816:12)
    at Module.require (node:internal/modules/cjs/loader:999:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'service-infra\\persistance\\node_modules\\ajv-errors\\dist\\index.js',
    'service-infra\\persistance\\src\\server.js'
  ]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

My node and npm versions are:

Node: 16.5.0, npm:7.19.1

My fastify version is:

"fastify": "^3.19.2",

During yarn install, this is the message I got as warning:

Linking dependencies... warning "ajv-merge-patch@4.1.0" has unmet peer dependency "ajv@>=6.0.0". warning " > ajv-errors@3.0.0" has unmet peer dependency "ajv@^8.0.1".

My plan is to write a validator as schemaErrorFormatter in the fastify registration itself for the sugercoating of the error.

However as I am not able to integrate the ajv-errors, I am unable to proceed.

Any help in this regard will be appreciated.

Pradip
  • 509
  • 1
  • 5
  • 22

1 Answers1

4

As you spotted, the issue are the plugin versions.

Fastify v3 uses ajv v6 under the hood, so you must install&use ajv's plugins that support that version.

So you need to run:

npm install ajv-errors@1 

Instead, ajv-merge-patch is fine to the last version as written in the docs

Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73