Skip to content

feat: order schemas correctly#37

Merged
mnahkies merged 2 commits intomasterfrom
mn/feat/order-schemas-correctly
Apr 16, 2023
Merged

feat: order schemas correctly#37
mnahkies merged 2 commits intomasterfrom
mn/feat/order-schemas-correctly

Conversation

@mnahkies
Copy link
Owner

previously we output schemas in alphabetically sorted order, same as we
do for types.

this is problematic as the schemas are runtime code, and if the order is
incorrect this causes "use before declaration" type errors.

after this change we instead group the scheams based on their
dependencies and sort each group such that depended on schemas are
output before the schemas that depend on them.

the implementation isn't perfect - there are a few edge cases that it
doesn't cover, but it's a big improvement on the previous behavior.

as a bonus, also adjusted things such that more scheams will be
extracted to the schemas.ts file rather than inlined, making them
reusuable and the code easier to read.

this will fix the build problems on #31 as it was failing due to the ordering issue

previously we output schemas in alphabetically sorted order, same as we
do for types.

this is problematic as the schemas are runtime code, and if the order is
incorrect this causes "use before declaration" type errors.

after this change we instead group the scheams based on their
dependencies and sort each group such that depended on schemas are
output before the schemas that depend on them.

the implementation isn't perfect - there are a few edge cases that it
doesn't cover, but it's a big improvement on the previous behavior.

as a bonus, also adjusted things such that more scheams will be
extracted to the schemas.ts file rather than inlined, making them
reusuable and the code easier to read.

this will fix the build problems on #31
as it was failing due to the ordering issue
@mnahkies mnahkies enabled auto-merge (squash) April 16, 2023 09:50
@mnahkies mnahkies merged commit 9f899dd into master Apr 16, 2023
@mnahkies mnahkies deleted the mn/feat/order-schemas-correctly branch April 16, 2023 09:50
mnahkies added a commit that referenced this pull request Apr 16, 2023
Was failing due to the zod schema's being output in sorted order and
referencing each other:

```
typescript-koa: src/api.github.com.yaml/schemas.ts(13399,11): error TS2448: Block-scoped variable 's_simple_user' used before its declaration.
typescript-koa: src/api.github.com.yaml/schemas.ts(13399,11): error TS2454: Variable 's_simple_user' is used before being assigned.
typescript-koa: src/api.github.com.yaml/schemas.ts(13400,14): error TS2448: Block-scoped variable 's_simple_user' used before its declaration.
typescript-koa: src/api.github.com.yaml/schemas.ts(13400,14): error TS2454: Variable 's_simple_user' is used before being assigned.
```

Fixed via #37
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.

1 participant