|
| 1 | +# typescript-schema-generator |
| 2 | + |
| 3 | +This package provides a tool that generates Typescript types and Javascript |
| 4 | +validator functions for the events that are defined in the JSON schemas that |
| 5 | +are built as part of this repository. |
| 6 | + |
| 7 | +## Setup |
| 8 | + |
| 9 | +Use `npm install` to install the necessary dependencies for this application. |
| 10 | +This can be done from either the root of this repository or from this package's |
| 11 | +directory. |
| 12 | + |
| 13 | +## Generating Code |
| 14 | + |
| 15 | +In order for this tool to function, you must first build the JSON schemas for |
| 16 | +the Digital Letters events. The simplest way to build these schemas and |
| 17 | +use this tool to automatically generate both types and validators is to run the |
| 18 | +`npm run generate-dependencies` command from the root of this repository. |
| 19 | + |
| 20 | +This will produce the following: |
| 21 | + |
| 22 | +- Flattened JSON Event schemas in the [`schemas`](../../schemas/) directory |
| 23 | +- A full set of JSON schemas in the [`output`](../../output/) directory |
| 24 | +- A type definition for each event in the [`types`](./types/) directory |
| 25 | +- A validator function for each event in the [`validators`](./validators/) directory |
| 26 | + |
| 27 | +### Generating Types |
| 28 | + |
| 29 | +Once the JSON schemas have been built, types can be generated on their own by |
| 30 | +running the `generate-types` script from this package. This will update the |
| 31 | +type definitions in [`types`](./types/) only. |
| 32 | + |
| 33 | +### Generating Validators |
| 34 | + |
| 35 | +Once the JSON schemas have been built, the validation functions can be |
| 36 | +generated on their own by running the `generate-validators` script from this |
| 37 | +package. This will update the validator functions in [`validators`](./validators/) only. |
| 38 | + |
| 39 | +## Using Generated Code |
| 40 | + |
| 41 | +### Using Generated Types |
| 42 | + |
| 43 | +The generated types can be used by simply installing |
| 44 | +`typescript-schema-generator` as a dependency of your package and the importing |
| 45 | +the desired type: |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { PDMResourceSubmitted } from 'typescript-schema-generator'; |
| 49 | + |
| 50 | +const pdmResourceSubmittedEvent: PDMResourceSubmitted = { |
| 51 | + type: 'uk.nhs.notify.digital.letters.pdm.resource.submitted.v1', |
| 52 | + source: |
| 53 | + '/nhs/england/notify/staging/dev-647563337/data-plane/digitalletters/pdm', |
| 54 | + dataschema: |
| 55 | + 'https://notify.nhs.uk/cloudevents/schemas/digital-letters/2025-10-draft/data/digital-letters-pdm-resource-submitted-data.schema.json', |
| 56 | + specversion: '1.0', |
| 57 | + id: '0249e529-f947-4012-819e-b634eb71be79', |
| 58 | + subject: |
| 59 | + 'customer/7ff8ed41-cd5f-20e4-ef4e-34f96d8cc8ac/75027ace-9b8c-bcfe-866e-6c24242cffc3/q58dnxk5e/4cbek805wwx/yiaw7bl0d/her/1ccb7eb8-c6fe-0a42-279a-2a0e48ff1ca9/zk', |
| 60 | + time: '2025-11-21T16:01:52.268Z', |
| 61 | + datacontenttype: 'application/json', |
| 62 | + traceparent: '00-ee4790eb6821064c645406abe918b3da-3a4e6957ce2a15de-01', |
| 63 | + tracestate: 'nisi quis', |
| 64 | + partitionkey: 'customer-7ff8ed41', |
| 65 | + recordedtime: '2025-11-21T16:01:53.268Z', |
| 66 | + sampledrate: 1, |
| 67 | + sequence: '00000000000350773861', |
| 68 | + severitytext: 'INFO', |
| 69 | + severitynumber: 2, |
| 70 | + dataclassification: 'restricted', |
| 71 | + dataregulation: 'ISO-27001', |
| 72 | + datacategory: 'non-sensitive', |
| 73 | + data: { |
| 74 | + messageReference: 'incididunt Ut aute laborum', |
| 75 | + senderId: 'officia voluptate culpa Ut dolor', |
| 76 | + resourceId: 'a2bcbb42-ab7e-42b6-88d6-74f8d3ca4a09', |
| 77 | + retryCount: 97_903_257, |
| 78 | + }, |
| 79 | + }; |
| 80 | +``` |
| 81 | + |
| 82 | +### Using Generated Validator Functions |
| 83 | + |
| 84 | +Validator functions for an event can be used by importing the default export |
| 85 | +from the relevant JS file in [`validators`](./validators/): |
| 86 | + |
| 87 | +```typescript |
| 88 | +import eventValidator from 'typescript-schema-generator/PDMResourceSubmitted.js'; |
| 89 | + |
| 90 | +const event = {}; |
| 91 | + |
| 92 | +const isEventValid = eventValidator(event); |
| 93 | +if (isEventValid) { |
| 94 | + console.log('Event is valid!'); |
| 95 | +} else { |
| 96 | + console.error('Validation failure!', eventValidator.errors); |
| 97 | + throw new Error('Event validation failed'); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Note: You will need to make sure the |
| 102 | +[`allowJs`](https://www.typescriptlang.org/tsconfig/#allowJs) option is set in |
| 103 | +your package's `tsconfig.json` in order to import the JS files. |
0 commit comments