Skip to content

feat: inline validators in standalone output#854

Open
RelevantShannon wants to merge 1 commit into
fastify:mainfrom
RelevantShannon:feature/inline-validators
Open

feat: inline validators in standalone output#854
RelevantShannon wants to merge 1 commit into
fastify:mainfrom
RelevantShannon:feature/inline-validators

Conversation

@RelevantShannon

@RelevantShannon RelevantShannon commented Jul 17, 2026

Copy link
Copy Markdown

Add an opt-in mode that embeds Ajv's generated validators into fast-json-stringify standalone output.

Standalone mode currently restores an Ajv-backed validator from serialized schema state. Schemas using anyOf, oneOf, or if/then/else therefore still compile their branch validators at runtime. The new inlineValidators option asks Ajv's standalone compiler for the exact fragment validators used by the generated serializer and includes them in the same output module.

The inline path is intentionally scoped:

  • it is only enabled for standalone mode when inlineValidators is true
  • only schema fragments referenced by generated validator calls are emitted
  • schemas that do not use Ajv-backed branches emit no validator code
  • omitting or disabling the option preserves the existing state-restoration path
  • generated validator code is forced to CommonJS while other caller-provided Ajv code options are preserved

This also makes the internal fjs_type keyword code-generating so Date and other toJSON values remain compatible with Ajv standalone output. External schema refs and built-in formats are included through Ajv's normal standalone dependency handling.

The output remains one generated module, but still uses fast-json-stringify's standalone serializer dependency and may reference small Ajv runtime helpers when required by the generated validation code.

Verification:

  • npm run lint
  • git diff --check
  • node --test test/standalone-mode.test.js
  • npm test

npm test passed locally with 479 tests and 0 failures.

@mcollina

Copy link
Copy Markdown
Member

This is awesome. Why is it still a draft?

@RelevantShannon
RelevantShannon marked this pull request as ready for review July 20, 2026 03:39
@RelevantShannon

Copy link
Copy Markdown
Author

This is awesome. Why is it still a draft?

Ah sorry, it was a Friday, and I wasn't sure if it was ready yet. I think it is :D

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.

2 participants