Comments (11)
If you don't use additionalProperties: false
on the anyOf
schema, but instead use it in each of the components of the anyOf, does that work?
Agreed on the lack of expressible inheritance: that's a real point of weakness.
from ts-json-schema-generator.
The end of this page is probably relevant:
https://json-schema.org/draft-06/json-schema-release-notes.html
from ts-json-schema-generator.
If you don't use additionalProperties: false on the anyOf schema, but instead use it in each of the components of the anyOf, does that work?
Actually, the problem is when you have additionalProperties: false
on all of the components. Imagine one with {a: number, b: number}
and one with {c: number, d: number}
. Now, an object with c
will violate the first and one with a
will violate the second (each object individually does not allow additional properties).
from ts-json-schema-generator.
Maybe I'm not understanding... here's a concrete example where instances pass even with additionalProperties: false
:
import jsonschema
schema = {
'anyOf': [
{'$ref': '#/definitions/A'},
{'$ref': '#/definitions/B'}
],
'definitions': {
'A': {
'properties': {
'a': {'type': 'number'},
'b': {'type': 'number'}
},
'additionalProperties': False
},
'B': {
'properties': {
'c': {'type': 'number'},
'd': {'type': 'number'}
},
'additionalProperties': False
}
}
}
jsonschema.validate({'a': 1, 'b': 2}, schema) # passes
jsonschema.validate({'c': 1, 'd': 2}, schema) # passes
from ts-json-schema-generator.
hmm, I tried the schema below in https://www.jsonschemavalidator.net/.
{
"$ref": "#/definitions/BC",
"$schema": "http://json-schema.org/draft-06/schema#",
"definitions": {
"B": {
"additionalProperties": false,
"properties": {
"b": { "type": "number" },
"x": { "type": "number" }
},
"type": "object"
},
"BC": {
"allOf": [{"$ref": "#/definitions/B" },
{ "$ref": "#/definitions/C" }]
},
"C": {
"additionalProperties": false,
"properties": {
"c": { "type": "number" },
"x": { "type": "number" }
},
"type": "object"
}
}
}
from ts-json-schema-generator.
Difference is allOf
vs anyOf
from ts-json-schema-generator.
Ahh, I missed that. The thing is that intersection types need allOf
.
from ts-json-schema-generator.
I have to admit I'm not using Vega or Vega-Lite, I just use the schema generator. But i want to comment on it anyway: In my opinion it would be very sad if you abandon JSON schemas because I guess we would then loose an important driving force in the typescript-to-json-schema world. So out of selfish reasons let me use my Jedi powers and tell you this blunt lie: There IS no alternative to JSON schema out there, don't look for it! Instead keep up the good work on the schema generator, please! :-)
from ts-json-schema-generator.
Thank you for the kind words @kayahr. I don't think you have anything to worry about.
Btw, I think the issue we are discussing here with intersection types will be resolved with draft 8: json-schema-org/json-schema-spec#656.
from ts-json-schema-generator.
As of draft 2019-09 unevaluatedProperties
was officially added.
Example schema for (A | B) & (C | D)
:
{
"$ref": "#/$defs/root",
"$defs": {
"root": {
"allOf": [
{
"oneOf": [
{ "$ref": "#/$defs/a" },
{ "$ref": "#/$defs/b" },
],
},
{
"oneOf": [
{ "$ref": "#/$defs/c" },
{ "$ref": "#/$defs/d" },
]
},
],
"unevaluatedProperties": false,
},
"a": {
"type": "object",
"properties": { "a": { "type": "string" } },
"required": ["a"],
},
"b": {
"type": "object",
"properties": { "b": { "type": "string" } },
"required": ["b"],
},
"c": {
"type": "object",
"properties": { "c": { "type": "string" } },
"required": ["c"],
},
"d": {
"type": "object",
"properties": { "d": { "type": "string" } },
"required": ["d"],
},
}
}
You can test this schema on https://json-everything.net/json-schema/.
Official json-schema blog post mentioning unevaluatedProperties
: https://json-schema.org/blog/posts/modelling-inheritance.
from ts-json-schema-generator.
Oh cool. Thanks for the link. It would be nice to support that pattern for inheritance but it also looks tricky as we would need to enumerate all implementing classes. Definitely a bigger refactor.
from ts-json-schema-generator.
Related Issues (20)
- Approach for renaming definition names HOT 1
- bug in vercel production, but development environment is OK HOT 1
- Issue when attempting to generate a schema from a type, conflict with @types/glob dependencies HOT 2
- External reference breaks arrays HOT 1
- Get return type from ArrowFunction HOT 2
- Allow --expose option to be a list of named types
- Add support for HTML-like (Vue, Svelte) files that have <script lang="ts"> elements HOT 1
- Handle const like enum with only one option HOT 2
- Add LiteralType as array accessor
- Lowercase type breaks with a comment HOT 1
- Bug: `ReferenceError: structuredClone is not defined` HOT 4
- constant number is converted to a generic `number` type HOT 4
- How to exclude underscore properties from json schema ? HOT 1
- Issues with generation of Union Type of Union Type on a Record HOT 1
- Argument of type 'Config' is not assignable to parameter of type 'CompletedConfig' in ts-json-schema-generator 2.0.0 HOT 2
- "foo" | "bar" | string; should not only map to a string HOT 9
- Improved Promise handling to support packages like Prisma HOT 1
- Literal and Record<never, never> crates empty object HOT 1
- Cannot migrate to version 2 due to typescript error type CompletedConfig HOT 2
- [Bug]: Error when using the binary (v2.1.0) HOT 9
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.