Skip to content

chore(test): replace is-my-json-valid with ajv - #885

Open
appsir2016-lab wants to merge 1 commit into
fastify:mainfrom
appsir2016-lab:chore/tests-use-ajv
Open

appsir2016-lab wants to merge 1 commit into
fastify:mainfrom
appsir2016-lab:chore/tests-use-ajv

Conversation

@appsir2016-lab

@appsir2016-lab appsir2016-lab commented Sep 15, 2026

Copy link
Copy Markdown

Replaces is-my-json-valid in the tests with ajv + ajv-formats, the validator the library already depends on, via a small helper in test/utils/validator.js. is-my-json-valid is removed from devDependencies.

Switching validators surfaced three places where the old one silently accepted output that ajv does not. None of them changes library behaviour; the tests now pin what the serializer does today:

  1. format: 'time' is rendered as HH:mm:ss with no timezone (asTime in lib/serializer.js). RFC 3339 requires a timezone, so ajv-formats rejects it in both full and fast mode. The helper registers a time format that matches the documented shape.
  2. { type: ['string', 'null'], const: 'baz' } with input null serializes to null. In JSON Schema const applies regardless of type, so ajv reports this as invalid. test/const.test.js now asserts the mismatch explicitly instead of asserting validity.
  3. { nullable: true, const: 'baz' } cannot be compiled by ajv at all ("nullable" cannot be used without "type"), so that test no longer validates the output against the schema.

Items 2 and 3 may be worth a separate discussion about whether the serializer should honour const before type: null. This PR does not change that.

is-my-json-valid did not implement const, which is why these went unnoticed. The old validator also ignored the fast-json-stringify specific format: 'unsafe'; the helper registers it as always valid to avoid ajv's unknown-format warning.

Checks run locally: npm run test (516 unit tests, 100% coverage, TypeScript tests) and npm run lint, all passing on Node 26.

Checklist

  • run npm run test && npm run benchmark --if-present (benchmark not run: test-only change)
  • tests and/or benchmarks are included
  • documentation is changed or added (not needed)
  • commit message and code follows the Developer's Certification of Origin and the Code of conduct

Closes #581

Closes fastify#581

Signed-off-by: P & A Dawn works <appsir2016@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

replace is-my-json-valid with ajv in tests

1 participant