-
Notifications
You must be signed in to change notification settings - Fork 9
Fix/record mapped type schemas #1880
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
031c41f
fix: resolve mapped type properties correctly
mathpirate 47e8fc7
fix: prevent TypeScript lib types from being hoisted to $defs
mathpirate 09bf035
switch to filtering out types from being named based on whether they'…
mathpirate 487a657
fmt
mathpirate c75a637
refactor: cleanup and add edge case tests for Record types
mathpirate ddf64da
fix: handle generic type instantiation and optional properties in saf…
mathpirate bd66996
correct test expectation
mathpirate f68116e
fmt
mathpirate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/schema-generator/test/fixtures/schema/inline-mapped-type.expected.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "stringConfig": { | ||
| "type": "object", | ||
| "properties": { | ||
| "theme": { | ||
| "type": "string" | ||
| }, | ||
| "language": { | ||
| "type": "string" | ||
| }, | ||
| "timezone": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "theme", | ||
| "language", | ||
| "timezone" | ||
| ] | ||
| }, | ||
| "numberLimits": { | ||
| "type": "object", | ||
| "properties": { | ||
| "cpu": { | ||
| "type": "number" | ||
| }, | ||
| "memory": { | ||
| "type": "number" | ||
| }, | ||
| "disk": { | ||
| "type": "number" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "cpu", | ||
| "memory", | ||
| "disk" | ||
| ] | ||
| }, | ||
| "booleanFeatures": { | ||
| "type": "object", | ||
| "properties": { | ||
| "auth": { | ||
| "type": "boolean" | ||
| }, | ||
| "api": { | ||
| "type": "boolean" | ||
| }, | ||
| "ui": { | ||
| "type": "boolean" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "auth", | ||
| "api", | ||
| "ui" | ||
| ] | ||
| }, | ||
| "objectHandlers": { | ||
| "type": "object", | ||
| "properties": { | ||
| "create": { | ||
| "type": "object", | ||
| "properties": { | ||
| "enabled": { | ||
| "type": "boolean" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "enabled" | ||
| ] | ||
| }, | ||
| "update": { | ||
| "type": "object", | ||
| "properties": { | ||
| "enabled": { | ||
| "type": "boolean" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "enabled" | ||
| ] | ||
| }, | ||
| "delete": { | ||
| "type": "object", | ||
| "properties": { | ||
| "enabled": { | ||
| "type": "boolean" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "enabled" | ||
| ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "create", | ||
| "update", | ||
| "delete" | ||
| ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "stringConfig", | ||
| "numberLimits", | ||
| "booleanFeatures", | ||
| "objectHandlers" | ||
| ] | ||
| } |
14 changes: 14 additions & 0 deletions
14
packages/schema-generator/test/fixtures/schema/inline-mapped-type.input.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| interface SchemaRoot { | ||
| stringConfig: { | ||
| [P in "theme" | "language" | "timezone"]: string; | ||
| }; | ||
| numberLimits: { | ||
| [K in "cpu" | "memory" | "disk"]: number; | ||
| }; | ||
| booleanFeatures: { | ||
| [F in "auth" | "api" | "ui"]: boolean; | ||
| }; | ||
| objectHandlers: { | ||
| [H in "create" | "update" | "delete"]: { enabled: boolean }; | ||
| }; | ||
| } |
67 changes: 67 additions & 0 deletions
67
packages/schema-generator/test/fixtures/schema/record-union-keys.expected.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| { | ||
| "type": "object", | ||
| "properties": { | ||
| "settings": { | ||
| "type": "object", | ||
| "properties": { | ||
| "theme": { | ||
| "type": "string" | ||
| }, | ||
| "language": { | ||
| "type": "string" | ||
| }, | ||
| "timezone": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "theme", | ||
| "language", | ||
| "timezone" | ||
| ] | ||
| }, | ||
| "limits": { | ||
| "type": "object", | ||
| "properties": { | ||
| "cpu": { | ||
| "type": "number" | ||
| }, | ||
| "memory": { | ||
| "type": "number" | ||
| }, | ||
| "disk": { | ||
| "type": "number" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "cpu", | ||
| "memory", | ||
| "disk" | ||
| ] | ||
| }, | ||
| "features": { | ||
| "type": "object", | ||
| "properties": { | ||
| "auth": { | ||
| "type": "boolean" | ||
| }, | ||
| "api": { | ||
| "type": "boolean" | ||
| }, | ||
| "ui": { | ||
| "type": "boolean" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "auth", | ||
| "api", | ||
| "ui" | ||
| ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "settings", | ||
| "limits", | ||
| "features" | ||
| ] | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
packages/schema-generator/test/fixtures/schema/record-union-keys.input.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| interface SchemaRoot { | ||
| settings: Record<"theme" | "language" | "timezone", string>; | ||
| limits: Record<"cpu" | "memory" | "disk", number>; | ||
| features: Record<"auth" | "api" | "ui", boolean>; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if we should have
"additionalProperties": falseon these. In the TypeScript world, we don't let you assign an object with extra properties to these.Edit: this isn't meant to be something that should be changed -- just not sure what the best behavior is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was a cool question that caused me to learn some things about typescript, thank you :)
here's an amalgamation of some inputs from claude that make sense to me as a set of takeaways:
Arguments for "additionalProperties": false:
Arguments against (current behavior):
property checking)
What do you think the validation use case is here? Are these schemas primarily for:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merging this for now but happy to keep chatting about this topic and can easily make whatever changes we decide on in a follow-up!