Skip to content

Optimize Ajv refs for external schema branches#853

Open
RelevantShannon wants to merge 1 commit into
fastify:mainfrom
RelevantShannon:optimize-ajv-validation-ref
Open

Optimize Ajv refs for external schema branches#853
RelevantShannon wants to merge 1 commit into
fastify:mainfrom
RelevantShannon:optimize-ajv-validation-ref

Conversation

@RelevantShannon

Copy link
Copy Markdown

Redirect buildOneOf's Ajv ref to a registered ref target when possible.

buildOneOf currently validates oneOf/anyOf branches through location.getSchemaRef(), which points back into the generated root schema even when the branch is only an external ref already registered with Ajv. This lets those branches validate against the exact external ref target instead, so Ajv can use the already-registered schema path rather than resolving through the root document.

The helper is intentionally guarded:

  • only pure branches are redirected
  • inline branches and refs with sibling keywords are left unchanged
  • unresolved refs are left unchanged
  • JSON-pointer and supported fragment refs are redirected only when the resolver can resolve the exact target
  • relative root cases are only redirected when the fragment matches the registered schema id shape

This also applies the same validator-ref selection to if schemas, which use the same runtime validator path.

Verification:

  • npm run lint
  • git diff --check
  • node --test test/oneof.test.js
  • node --test test/oneof.test.js test/anyof.test.js test/if-then-else.test.js
  • npm run test:unit

npm run test:unit passed locally with 488 tests and 0 failures. The local run printed Node inspector auto-attach warnings, but exited successfully.

@RelevantShannon
RelevantShannon marked this pull request as ready for review July 15, 2026 07:33

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Fdawgs Fdawgs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any supporting benchmarks for this optimisation?

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request optimizes how fast-json-stringify selects the Ajv schema reference used for runtime branch validation in oneOf/anyOf and if schemas, allowing validation to target already-registered external schema refs directly when the branch is a “pure” $ref.

Changes:

  • Add getValidatorSchemaRef() to redirect validator refs from root-relative JSON-pointer paths to external registered $ref targets when safely resolvable.
  • Use the new validator-ref selection in buildOneOf and buildIfThenElse.
  • Add regression/behavior tests covering bare refs, $id refs (absolute and relative), named fragments, JSON-pointer subpaths, and non-redirected cases (inline branches and $ref with sibling keywords).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
index.js Introduces getValidatorSchemaRef() and applies it to Ajv validation calls in oneOf/anyOf and if codegen paths.
test/oneof.test.js Adds targeted tests asserting standalone output uses direct external validator refs for eligible oneOf branches (and preserves behavior for ineligible cases).
test/anyof.test.js Adds a test ensuring anyOf branch validator refs resolve directly to registered external targets.
test/if-then-else.test.js Adds tests ensuring if schema validator refs resolve directly to registered external targets (and are not redirected when $ref has siblings).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

4 participants