fluent-json-schema icon indicating copy to clipboard operation
fluent-json-schema copied to clipboard

Error "must match format 'time'" after upgrading to fastify v5

Open AlexisMunozC opened this issue 1 year ago • 8 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have searched existing issues to ensure the bug has not already been reported

Fastify version

5.x.x

Plugin version

No response

Node.js version

22.9.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

22.04

Description

I have an endpoint in my Fastify API where in the body I request a field with the type "string" and format "time", in Fastify v4.x.x I passed the format "HH:MM:SS" and it is accepted but after upgrading to v5 running the same test and without modifying anything else I got the error: { statusCode: 400, error: 'Bad Request', message: 'body/check_in_time must match format "time"' }

I already checked and when I have the ajv dependency in the last version in Fastify v4 works but in Fastify v5, it doesn't, so it's no problem of ajv.

I have created a simple repository with the example, you just need to change the fastify version and make a request like in the test.http file.

Link to code that reproduces the bug

https://github.com/AlexisMunozC/fastify_error

Expected Behavior

I would expect the format "time" has not changed, or at least describe it to know which is the new format expected.

AlexisMunozC avatar Feb 11 '25 22:02 AlexisMunozC

I can reproduce the problem, tho I'm not able to find yet what is exactly the change. The Ajv instance is managed by @fastify/ajv-compiler and automatically adds ajv-formats if not explicitly passed through fastifyoptions.ajv.plugins.

Will try to spend more time to see what might be missing

metcoder95 avatar Feb 12 '25 09:02 metcoder95

Fastify v5 updated AJV compiler which also updated AJV formats which enforces the use of timezone in time and date-time.

This should be mentioned in the migration guide as it broke a lot for us too.

There is a workaround to add backwards comptability using iso-time and iso-date-time but it's been stuck in pre-release since 2021: https://github.com/ajv-validator/ajv-formats/releases

Maybe @mcollina or @epoberezkin can provide some guidance on how to navigate this?

alexcroox avatar Feb 28 '25 11:02 alexcroox

@alexcroox that seems incorrect. We use version of ajv-format 3.0.1, which should include all the fixed in the prerelease version you linked.

@jasoniangreen maybe can you help here?

I don't think we were really aware of any breaking changes in those area. Would you like to send a PR to improve the migration guide?

mcollina avatar Feb 28 '25 12:02 mcollina

@mcollina sorry I missed out a key piece of info, the startup error comes from fluent-json-schema which doesn't have the iso-date-time in the allowed formats:

https://github.com/fastify/fluent-json-schema/blob/main/types/FluentJSONSchema.d.ts#L38-L53

Image

alexcroox avatar Feb 28 '25 12:02 alexcroox

I think the culprit has been already found:

  • fastify v4 uses ajv-format v2: https://github.com/fastify/ajv-compiler/blob/v3.6.0/package.json#L51
  • fastify v5 uses ajv-format v3: https://github.com/fastify/ajv-compiler/blob/v4.0.2/package.json#L81

There is a workaround to add backwards comptability using iso-time and iso-date-time but it's been stuck in pre-release since 2021: https://github.com/ajv-validator/ajv-formats/releases

While the GH Release is in draft, the npm versions tell us another story the v3.0.1 is a thing:

  • https://www.npmjs.com/package/ajv-formats?activeTab=versions

We should definetly mention this change in the migration guide

Eomm avatar Feb 28 '25 13:02 Eomm

@mcollina sorry I missed out a key piece of info, the startup error comes from fluent-json-schema which doesn't have the iso-date-time in the allowed formats:

https://github.com/fastify/fluent-json-schema/blob/main/types/FluentJSONSchema.d.ts#L38-L53

Image

would you like to send a PR to add it?

mcollina avatar Feb 28 '25 15:02 mcollina

Related to https://github.com/fastify/fluent-json-schema/pull/254

climba03003 avatar Mar 04 '25 00:03 climba03003

Actually, this issue was mostly resolved earlier, but there were still a few missing pieces specifically some TypeScript types and dedicated tests. I’ve added those to complete the fix Fix/iso time types tests #277

gulbaki avatar Jun 18 '25 16:06 gulbaki