Giter VIP home page Giter VIP logo

Comments (10)

domoritz avatar domoritz commented on May 11, 2024

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.

marcoqu avatar marcoqu commented on May 11, 2024

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.

marcoqu avatar marcoqu commented on May 11, 2024

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.

domoritz avatar domoritz commented on May 11, 2024

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.

marcoqu avatar marcoqu commented on May 11, 2024

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.

domoritz avatar domoritz commented on May 11, 2024

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.

marcoqu avatar marcoqu commented on May 11, 2024

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.

domoritz avatar domoritz commented on May 11, 2024

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.

marcoqu avatar marcoqu commented on May 11, 2024

from ts-json-schema-generator.

domoritz avatar domoritz commented on May 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.