Skip to content

fix(spec): pre-compile JSON schema validators at build time#5039

Merged
mergify[bot] merged 1 commit intomainfrom
mrgrain/refactor/spec/precompile-validators
Feb 12, 2026
Merged

fix(spec): pre-compile JSON schema validators at build time#5039
mergify[bot] merged 1 commit intomainfrom
mrgrain/refactor/spec/precompile-validators

Conversation

@mrgrain
Copy link
Copy Markdown
Contributor

@mrgrain mrgrain commented Feb 12, 2026

The @jsii/spec package currently compiles JSON schemas into validators at runtime using ajv. This happens every time the validators are called, which adds unnecessary overhead during jsii compilation and when loading assemblies.

Every now and then we are also seeing users getting the following error. While I am not entirely clear on what is causing it (maybe some confused dependency tree?), getting rid of ajv completely will solve this.

TypeError: ajv_1.default is not a constructor
  at loadAssemblyFromFile (node_modules\@jsii\spec\lib\assembly-utils.js:139:15)

This change pre-compiles the validators during the build process using Ajv's standalone code generation feature. The generated JavaScript code is a self-contained validator that doesn't require the ajv library at runtime. This approach has two benefits: it eliminates the schema compilation overhead at runtime, and it allows us to move ajv from a runtime dependency to a dev dependency, reducing the package's footprint.

While making these changes, I also noticed that fs-extra was only used in tests for convenience methods like readJsonSync and removeSync. Since Node.js 14+ provides fs.rmSync with recursive support, and JSON parsing is trivial with JSON.parse(fs.readFileSync(...)), the fs-extra dependency is no longer needed and has been removed entirely.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Feb 12, 2026
@mrgrain mrgrain changed the title refactor(spec): pre-compile JSON schema validators at build time fix(spec): pre-compile JSON schema validators at build time Feb 12, 2026
Move ajv from runtime to dev dependency by generating standalone
validation code during the build process. This eliminates the need
to compile schemas at runtime, improving startup performance.

Also removes fs-extra dependency in favor of Node.js built-in fs module.
@mrgrain mrgrain force-pushed the mrgrain/refactor/spec/precompile-validators branch from 274f1f1 to 3bcdd82 Compare February 12, 2026 15:31
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 12, 2026

Thank you for contributing! ❤️ I will now look into making sure the PR is up-to-date, then proceed to try and merge it!

@mergify mergify bot added the pr/ready-to-merge This PR is ready to be merged. label Feb 12, 2026
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 12, 2026

Merging (with squash)...

@mergify mergify bot added the queued label Feb 12, 2026
@mergify mergify bot merged commit 43f0ec3 into main Feb 12, 2026
61 of 63 checks passed
@mergify mergify bot deleted the mrgrain/refactor/spec/precompile-validators branch February 12, 2026 16:28
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Feb 12, 2026

Merge Queue Status

Rule: default-squash


  • Entered queue2026-02-12 16:28 UTC
  • Checks passed · in-place
  • Merged2026-02-12 16:28 UTC · at 3bcdd826b9fb34dd9c96bf9eaeec4fcb9f7d469a

This pull request spent 7 seconds in the queue, with no time running CI.

Required conditions to merge
  • #approved-reviews-by >= 1 [🛡 GitHub branch protection]
  • #changes-requested-reviews-by = 0 [🛡 GitHub branch protection]
  • status-success=Integration test (jsii-pacmak)
  • status-success=Unit Tests
  • any of [🛡 GitHub branch protection]:
    • check-success = Integration test (jsii-pacmak)
    • check-neutral = Integration test (jsii-pacmak)
    • check-skipped = Integration test (jsii-pacmak)
  • any of [🛡 GitHub branch protection]:
    • check-success = Build
    • check-neutral = Build
    • check-skipped = Build
  • any of [🛡 GitHub branch protection]:
    • check-success = Unit Tests
    • check-neutral = Unit Tests
    • check-skipped = Unit Tests

@mergify mergify bot removed pr/ready-to-merge This PR is ready to be merged. queued labels Feb 12, 2026
mergify bot pushed a commit that referenced this pull request Feb 16, 2026
In #5039 we implemented Ajv Standalone validators and moved `ajv` from a runtime dependency to a dev dependency in `@jsii/spec`. However, the generated validators [still require ajv runtime utilities](https://ajv.js.org/standalone.html#requirement-at-runtime), causing the package to fail when used.

This change fixes the issue by using esbuild to bundle the generated validator code at build time, inlining all ajv runtime dependencies directly into `lib/validators.js`. The result is a fully self-contained validators file that works without ajv installed at runtime.

Manually verified that the package works correctly after this change.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution/core This is a PR that came from AWS.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants