fastify / ajv-compiler

Build and manage the AJV instances for the fastify framework
Other
18 stars 9 forks source link

fix: do not overwrite customOptions.formats #122

Closed floratmin closed 4 months ago

floratmin commented 4 months ago

Checklist

Issue: Custom format for ajv gets overwritten by ajv-formats

floratmin commented 4 months ago

I solved this issue for me with the current fastify version by using a function with the name formatsPlugin. But it seems hacky to me. As an user I would expect that my preferences are not overwritten. On the other side a user who did not realize that their own formats are not used could get a broken system whit this change.

Eomm commented 4 months ago

As an user I would expect that my preferences are not overwritten. On the other side a user who did not realize that their own formats are not used could get a broken system whit this change.

How do you implement the proposed behaviour in pure AJV starting from this use case?

const ajv = new AJV({ formats: { date: /foo/ } })
addFormats(ajv)

I see these options:

  1. configure the addFormats properly
  2. do not use the formats options and write ajv.addFormat() method

For this example, I think the best approach to this issue is not to force the if()do logic here, but we can do a step forward and:

  1. give to the user the possibility to customize the plugin's option (DONE)
  2. add a new callback function where the user can run ajv.addFormat by themself (MISSING)

My rationale here is that a callback is generic and easy to use compared to check all the conflict that we can face in the ajv's plugins and its configuration.

floratmin commented 4 months ago

Thank you very much for your support. But I think that I do not want to implement any design decisions. I can live with my solution.