Comments (10)
Yes, that's a bug. I don't know how easy it is to get this information from the AST but I'd love to get a PR for this.
from ts-json-schema-generator.
I could have a look, but it looks like the additionalItems property was explicitely added, while I think tuples should not allow additionalItems. But maybe there's something I'm missing.
return {
type: "array",
items: tupleDefinitions,
minItems: tupleDefinitions.length,
...(tupleDefinitions.length > 1 ? {additionalItems: {anyOf: tupleDefinitions}} : {}),
};
could become
return {
type: "array",
items: tupleDefinitions,
minItems: tupleDefinitions.length,
maxItems: tupleDefinitions.length,
};
And then one would need to look into the undefined
items in the tail of the tuple to lower the minItems count?
from ts-json-schema-generator.
I looked into it.
Apparently "When accessing an element outside the set of known indices, a union type is used instead".
So, while this const tuple:[string, number] = ["", 1, 2]
gives an error. This tuple[4] = 99
is valid. Link
Still, I believe it goes against the expected behavior of a schema generator. Maybe you could want an option for this? --strictTuples
?
With regard to the lowering the value of minItems if the trailing elements of the tuple include undefined
, it appears the in typescript there is no way of having optional arguments to a tuple. Nonetheless, lowering the value of minItems would have the expected effect of getting undefined
if the item is not set...
Not sure how you want to go about this.
from ts-json-schema-generator.
Thanks for looking into this!
In this library, I chose not to add many options and have the most correct defaults. If you want more options, they could be added to https://github.com/YousefED/typescript-json-schema. I hope that makes sense.
from ts-json-schema-generator.
Ok, thanks!
Since I see you are involved in both, can i ask you what's the difference between this project and YousefED/typescript-json-schema?
from ts-json-schema-generator.
Good question! YousefED/typescript-json-schema uses typeAtLocation
while this project uses the AST itself. Using the AST is more complicated but tracks aliases better. I use this project for Vega-Lite and that's my main use case. I'm not going to fix anything unless it is relevant for Vega-Lite. YousefED/typescript-json-schema has a lot more customization options and the code is much shorter. However, since it is less complex, it sometimes does not work as well. Both projects benefit from each other. For instance, I try to share test cases where I can and if there is a fix that applies to both projects, I usually copy it over.
from ts-json-schema-generator.
Got it.
I'm using this for a vscode extension for generating json schemas from types. I believe for the moment I'll maintain a close fork of your version since it seems to be the most complete.
Thanks!
from ts-json-schema-generator.
I see. Well, let me ask then, would you be willing to help me maintain this library? that is, respond to issues about tuples and help resolve issues around tuples when there is a new typescript version? The changes you made could be useful in Vega-Lite as well.
from ts-json-schema-generator.
from ts-json-schema-generator.
Sweet. Can you send a PR with --strictTuples
? Make sure to add some tests and update the docs. I will test it against Vega-Lite and if it makes sense, I'm happy to make strictTuples
the default. Sounds good?
from ts-json-schema-generator.
Related Issues (20)
- Using with in-memory source files HOT 1
- deno compatibility? HOT 2
- The `@discriminator` tag does not handle `enum` type discriminators
- Expose parsing of a symbol HOT 1
- Publish NPM release HOT 1
- Imported types are converted into strings in literal expression
- The @discriminator tag does not handle `ref` type discriminators
- TypeError: Invalid value used as weak map key HOT 2
- Missing $id prefix in $ref HOT 3
- Bun.sh support HOT 2
- Type override when compile same type name from different ts file to one json schema HOT 2
- Prevent simplification of UnionType HOT 1
- Add patterns to object property names
- Duplicate (nested) discriminator union values not allowed
- Invalid schema generated with circular dependencies - minimal case
- Errors out with mentions of @types/mocha HOT 1
- Function with generic keyof param crashes HOT 1
- Issue Generating JSON Schema for Types with Indexed Access References
- Support @required and $data annotations. HOT 5
- Error: Unknown node "Primitive" (ts.SyntaxKind = 162) in specific Type example HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ts-json-schema-generator.