Skip to content

CCM-12896: Automatically Generate Typescript Types and Validator Code from Event JSON Schemas#138

Merged
gareth-allan merged 41 commits intomainfrom
feature/CCM-12896_ts_event_type_generation
Dec 8, 2025
Merged

CCM-12896: Automatically Generate Typescript Types and Validator Code from Event JSON Schemas#138
gareth-allan merged 41 commits intomainfrom
feature/CCM-12896_ts_event_type_generation

Conversation

@gareth-allan
Copy link
Copy Markdown
Contributor

@gareth-allan gareth-allan commented Nov 28, 2025

Description

This PR adds a new application in src/typescript-schema-generator that takes the JSON event schemas built from the docs/src/cloudevents directories and produces TypeScript type definitions for each event (using json-schema-to-typescript) as well as a JS validator function for each event (produced using Ajv's option to build standalone validation code).

In order to generate the necessary code when it is needed as part of building/running tests, I've updated the generate-dependencies script in the root package.json so that it runs the minimal set of make commands to build the "flattened" JSON schema for each event and then runs the generate-dependencies script in the src/typescript-schema-generator workspace (it actually runs it in all workspaces, but this is the only one with it defined) to generate types and validator code. I've also added a generate target to the root Makefile that calls this NPM script and updated the config target to call it so that our standard make config setup command now generates the code for you.

I've also updated the mesh-poll placeholder lambda, just as an example of how we might use these types/validators, and to prove that the package being build can be imported and used from the lambdas. #104 will replace this lambda with a Python version, so this isn't intended as a long-term change, but I also don't think there's any harm in merging it in.

Context

This change means that we will now always have TypeScript types and validation code for each event that we have a schema defined for in the digital letters domain, and that any changes to the event schemas will automatically result in updated code.

Validation

My nhs-notify-digital-letters directory before running make config:
Screenshot 2025-12-03 at 14 41 23

My nhs-notify-digital-letters directory after running make config:
Screenshot 2025-12-03 at 14 43 06

A valid ResourceSubmitted event is accepted by the mesh-poll lambda:
Screenshot 2025-12-01 at 17 09 59

An invalid event causes an error:
Screenshot 2025-12-01 at 17 10 39
Screenshot 2025-12-01 at 17 10 46

Compared build times on this branch to another recently-built branch:
Screenshot 2025-12-02 at 14 18 47
Screenshot 2025-12-02 at 14 18 52

We can see that the build takes around two minutes longer, due to a slower "Test Stage" and slower deployment of the dynamic environment. This is because the auto-generated code is built in each of these stages.

Dev Container

To verify the changes work in the dev container.

Ran make clean outside of a container and verified there were no generated files in src/digital-letters-events:
Screenshot 2025-12-08 at 10 03 00

Then reopened the workspace in a new Notify Loaded 1.0.17 dev container and verified the files had been generated (make config is run as part of the post-create command for the dev container:
Screenshot 2025-12-08 at 10 46 59

Type of changes

  • Refactoring (non-breaking change)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would change existing functionality)
  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • I am familiar with the contributing guidelines
  • I have followed the code style of the project
  • I have added tests to cover my changes
  • I have updated the documentation accordingly
  • This PR is a result of pair or mob programming

Sensitive Information Declaration

To ensure the utmost confidentiality and protect your and others privacy, we kindly ask you to NOT including PII (Personal Identifiable Information) / PID (Personal Identifiable Data) or any other sensitive data in this PR (Pull Request) and the codebase changes. We will remove any PR that do contain any sensitive information. We really appreciate your cooperation in this matter.

  • I confirm that neither PII/PID nor sensitive data are included in this PR and the codebase changes.

Comment thread lambdas/mesh-poll/package.json
Comment thread lambdas/mesh-poll/src/index.ts
Comment thread src/cloudevents/tools/generator/manual-bundler/schema-bundler.ts
Comment thread src/typescript-schema-generator/package.json
@gareth-allan gareth-allan marked this pull request as ready for review December 3, 2025 14:44
@gareth-allan gareth-allan requested review from a team as code owners December 3, 2025 14:44
Comment thread src/typescript-schema-generator/src/__tests__/generate-types.test.ts Outdated
sidnhs
sidnhs previously approved these changes Dec 4, 2025
Comment thread infrastructure/terraform/components/dl/pre.sh
tdroza-nhs
tdroza-nhs previously approved these changes Dec 4, 2025
Comment thread lambdas/mesh-poll/src/index.ts Outdated
Copy link
Copy Markdown
Contributor

@tdroza-nhs tdroza-nhs left a comment

Choose a reason for hiding this comment

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

Re-reviewed ✅

@gareth-allan gareth-allan merged commit 4e58f88 into main Dec 8, 2025
36 checks passed
@gareth-allan gareth-allan deleted the feature/CCM-12896_ts_event_type_generation branch December 8, 2025 10:51
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.

7 participants