Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support multiple schema types #203

Merged
merged 6 commits into from
Jul 17, 2024
Merged

Conversation

DafyddLlyr
Copy link
Contributor

@DafyddLlyr DafyddLlyr commented Jul 16, 2024

What does this PR do?

  • Refactors repository to support multiple schemas instead of a single schema
    • Type Schema is now DigitalPlanningApplication
  • Simplifies build scripts by using a shared bash script
  • Simplifies example generation to walk directories to generate JSON files instead of having to manually import each one
  • Tests: Walk /examples to import all JSON files, avoid manual imports

Changes to folder structure

The changes made are as follows -

  • /schema/schema.json/schemas/${SCHEMA}.json

  • /examples/someExample.jsonexamples/${SCHEMA}/someExample.json

  • /types/Schema.ts/types/${SCHEMA}.index.ts

  • /types/utils.ts/types/shared/utils.ts

    • I think more will end up in the /shared folder (maybe some enums?). The main motivation for moving utils there now (before it's actually shared) was just to be sure that these types would still be accessible to ts-json-schema-generator when they were "outside" / "above" the scope of the type being converted. Good news is that it works!

Tree view

schemas/
└── ${SCHEMA}/
    └── ${SCHEMA}.json

examples/
└── ${SCHEMA}/
    ├── data/
    │    └── example.ts
    └── example.json

types/
├── schemas/
│   └── ${SCHEMA}/
│       └── index.ts
└── shared/
    └── utils.ts

@@ -1,7 +1,7 @@
{
"extends": "./node_modules/gts/",
"rules": {
"node/no-unpublished-import": ["error", {
"n/no-unpublished-import": ["error", {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Required by gts 5.3.1 - https://github.com/google/gts/releases/tag/v5.3.1

Bug Fixes

deps: replace dependency eslint-plugin-node with eslint-plugin-n (google/gts#865) (efbe3a8)

@@ -38,7 +38,7 @@ jobs:
run: |
VERSION=v${{steps.version_check.outputs.version}} pnpm build
mkdir -p "$GITHUB_WORKSPACE/v${{ steps.version_check.outputs.version }}"
mv schema/* "$GITHUB_WORKSPACE/v${{ steps.version_check.outputs.version }}"
mv schemas/* "$GITHUB_WORKSPACE/v${{ steps.version_check.outputs.version }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is the only change needed here!

This has the nice benefit of preserving the old URLs in the format -

https://theopensystemslab.github.io/digital-planning-data-schemas/<VERSION>/schema.json

and generating new URLs in the format -

https://theopensystemslab.github.io/digital-planning-data-schemas/<VERSION>/schemas/<SCHEMA>.json

@@ -1,9 +1,9 @@
import {Schema} from '../../types/Schema';
import {BaseProposal} from '../../types/schema/data/Proposal';
import {DigitalPlanningApplication} from '../../../types/schemas/digitalPlanningApplication';
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Jul 16, 2024

Choose a reason for hiding this comment

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

I'll have a look (in another PR) at path aliases in our tsconfig.json - it could be nice to import from @types, @utils etc now that these import paths are more deeply nested.

Comment on lines +10 to +11
dirs=("digitalPlanningApplication")
types=("DigitalPlanningApplication")
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Jul 16, 2024

Choose a reason for hiding this comment

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

When we get new schemas, we'll just need to add them to this list, e.g.

dirs=("digitalPlanningApplication", "mySchemaDirectory")
types=("DigitalPlanningApplication", "MySchemaType")

* @title Digital Planning Application
* @description The root specification for a planning application in England generated by a digital planning service
*/
export interface DigitalPlanningApplication {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something to consider - is DigitalPlanningApplication actually the right name? Is the DigitalPlanning prefix redundant given the context, will all schemas end up with the prefix?

Copy link
Member

Choose a reason for hiding this comment

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

Very on board with simplifying at this stage ⚡ agree dropping prefix and keeping Application only is less redundant and going to be easier to extend (eg PreApplication, Assessment, Consultation, SiteNotice etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed! Will pick this up in a follow up PR shortly.

Comment on lines +39 to +45
{
name: 'DigitalPlanningApplication',
schema: digitalPlanningApplicationSchema,
examples: getJSONExamples<DigitalPlanningApplication>(
'digitalPlanningApplication'
),
},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New schemas will also need to be added here, and then all their examples will get picked up by tests automatically 👍

Comment on lines +70 to +71
const ajv = addFormats(new Ajv({allowUnionTypes: true}));
const validate = ajv.compile(schema);
Copy link
Contributor Author

@DafyddLlyr DafyddLlyr Jul 16, 2024

Choose a reason for hiding this comment

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

Moving the instantiation of ajv and jsonschema up to the describe() block and not repeating in it each test speeds tests up from ~55s to ~3s locally.

Copy link
Member

Choose a reason for hiding this comment

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

Huge ! Thanks for spotting that misconfiguration

@DafyddLlyr DafyddLlyr requested a review from a team July 17, 2024 06:34
@DafyddLlyr DafyddLlyr marked this pull request as ready for review July 17, 2024 06:34
Copy link
Member

@jessicamcinchak jessicamcinchak left a comment

Choose a reason for hiding this comment

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

This looks great ! Reviewed as much as one can for 65+ changed files, let's get to main & fix forward anything else we spot 🙂

(async () => {
await walkDirectory('./examples');
console.log('All example files converted to JSON');
})();
Copy link
Member

Choose a reason for hiding this comment

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

Much appreciated dev workflow improvement 👍

@DafyddLlyr
Copy link
Contributor Author

Thanks @jessicamcinchak! Certainly a larger and noisier PR than intended, really appreciate the speedy review 🙌

@DafyddLlyr DafyddLlyr merged commit c7d6578 into main Jul 17, 2024
3 checks passed
@DafyddLlyr DafyddLlyr deleted the dp/support-multiple-schemas branch July 17, 2024 08:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants