Error "must match format 'time'" after upgrading to fastify v5
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.
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
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 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 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
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
@mcollina sorry I missed out a key piece of info, the startup error comes from
fluent-json-schemawhich doesn't have theiso-date-timein the allowed formats:https://github.com/fastify/fluent-json-schema/blob/main/types/FluentJSONSchema.d.ts#L38-L53
would you like to send a PR to add it?
Related to https://github.com/fastify/fluent-json-schema/pull/254
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