Giter Club home page Giter Club logo

Comments (10)

sramam avatar sramam commented on July 18, 2024 1

I see. That intent wasn't fully clear - from a reading of the code.

I now see how current behavior is actually a better approach to
determining when additionalProperties should be included and when not.
It's a more natural extension of excess-property-checking in TypeScript.

Nicely done. Thanks much.

from ts-json-schema-generator.

yliu-d avatar yliu-d commented on July 18, 2024 1

@domoritz that's the workaround we are using today, yes we can continue using this workaround.

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024 1

I'd consider a pull request with tests if the changes to the library code are small (which I expect them to be).

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

Usually, you wouldn't set it to true but define what types you expect additional properties to be. This is useful for mapsas in https://github.com/vega/typescript-to-json-schema/blob/master/test/valid-data/type-maps/schema.json#L38. Does this make sense?

from ts-json-schema-generator.

sramam avatar sramam commented on July 18, 2024

ah!
Two questions:

  1. Is there a way to specify this in the TypeScript definition? Ideally, @additionalProperties annotation on an object.
  2. Currently, this is being set to false. Which seems to trigger a no additionalProperties allowed check from a validator I am using. Per the spec, http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.10, additionalProperties being absent is treated as empty schema, which should validate arbit additional props.

Basically, providing for 1 above will solve 2. Current behavior seems to be amiss from the standard.

Or did I miss something else again?! 😱

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024
  1. I haven't added anything but I don't see why we wouldn't be able to add it
  2. That seems to be the behavior you want from a json schema that is generated from typescript. https://github.com/YousefED/typescript-json-schema has an option to not automatically output additionalProperties: false but I haven't bothered adding this to this generator because I always want it.

I don't agree that what I'm doing is not a according to the standard. What this library does is try to create a correspondence between the behavior in typescript and the behavior in json schema. Can you give me an example where you would want a different behavior?

Note that you can a write in typescript (roughly, I don't have a compiler right now)

interface A {
  foo: number
  [key: string]: any
}

which will allow additional properties on the interface A.

from ts-json-schema-generator.

yliu-d avatar yliu-d commented on July 18, 2024

@domoritz we are using this great library but we found we need to allow customers sending JSON files with additional properties anywhere they want in JSON. Can I create a PR to add a --allowAdditionalProperties flag? Thanks!

from ts-json-schema-generator.

domoritz avatar domoritz commented on July 18, 2024

@yliu-d would it make sense to just iterate over the generated JSON to change the property instead of adding support to the library itself?

from ts-json-schema-generator.

tamj0rd2 avatar tamj0rd2 commented on July 18, 2024

Would you accept a PR to add a flag for this?

My use case is that I'm writing a library that lets people check if their typescript types match up with a real piece of JSON (for example, the JSON you'd get back from an API call). The reason for it is so that people don't end up using operations in typescript on properties that may not exist in the actual JSON.

In this case, consumers don't care whether or not there are additional properties, they only care that the returned JSON has all of the properties that were specified in their type. https://github.com/tamj0rd2/ncdc

It seems like adding a flag may satisfy other people's use cases also. This is the only roadblock I'm facing switching from typescript-json-schema to this package

from ts-json-schema-generator.

tamj0rd2 avatar tamj0rd2 commented on July 18, 2024

Do you have any guidance on this? Should it be as easy as adding an allowAdditional flag to the different Object parser constructors? e.g InterfaceAndClassNodeParser and ObjectLiteralExpressionNodeParser

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.