by peterkelly · 8y ago · 0 comments
For example this:
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
can be expressed like this: interface Person {
firstName: string;
lastName: string;
age?: number; // Age in years
}
I can see that there are additional constraints expressible, such as the fact that age has to be an integer and has a minimum value. However those could all conceivably comprise a superset of TypeScript's interface definitions. JSON Schema just feels to me like XML Schema all over again.JSON-schema provides a type assertion system.
Both serve as type documentation systems.
TS says "if you create objects in my world, I will use my type system to verify that you're creating the right kind of objects, and you can look up their definitions to see what's in them". JSON-schema says "if you give me external, un-annotated objects, I can tell you whether they comply with a type definition, and you can look at that definition to see what's in them".
TS has a limited assertion facility, but it's not nearly as capable as JSON-schema. Projects like io-ts (https://github.com/gcanti/io-ts) add more functionality to those assertions, but they're still much less easy to use, in my opinion, than JSON-schema for this purpose. If you're using TS types to assert, though, you can definitely describe more complex constraints, so it may make sense as an alternative to JSON-schema for some people.
As others have pointed out, JSON-schema is also much easier to use on non-TypeScript platforms than TS assertions, which, even if you use an assertion library like io-ts, will still be limited in use to JavaScript (and JS-to-$other_language runtimes).
Would you expand on how dependent type systems are relevant to the differences between TypeScript/JSON-Schema, or how the use of a DTS could help ameliorate the issues people have in those areas?
The reason for having the schema are that they are language agnostic and there is wide tool support. For example, vscode supports autocomplete and tooltips when you use json schema.
- it can do checks at runtime (to validate untrusted input, for example)
- there are libraries for many other programming languages
- it can express constraints on values as well as on types (size of arrays, string regex matching, integer value ranges, ...)
Of course there are use cases that JSON Schema works better for (eg most other replies you got) but I fully agree that TypeScript type definitions are a great way to specify the structure of JSON data.
To make it feel more language-agnostic, you could consider using type aliases instead:
type Person = {
firstName: string,
lastName: string,
age?: number // Age in years
}
This is equivalent to the interface you described, but potentially less confusing to readers who don't know what an "interface" is (eg because they mostly used Ruby or C++). You don't even need to tell people that it's TypeScript - this could be a perfectly sensible schema language for JSON. A bit like what RELAX NG[0] is for XML.At my company we actually use typescript-json-schema[1] so we can specify the structure of our API payloads in TS instead of JSON Schema and still benefit from JSON Schema's better tool support.
Additionally, what you've chosen is a very simple example, and even in that case you're having trouble correctly mapping the types to what is expected. What about a slightly more complex one[1]?
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"/": {}
},
"patternProperties": {
"^(/[^/]+)+$": {}
},
"additionalProperties": false,
"required": [ "/" ]
}
1: http://json-schema.org/example2.htmlAlso you can't really hand a third party a typescript interface and tell them to conform with that.
And finally, I would imagine a good schema could enforce more things like the length of identifiers, whether a number can be negative, etc.
That's mostly true, but some projects exist that allow a subset of interfaces to be used at runtime:
- https://github.com/gcanti/io-ts
- https://github.com/fabiandev/ts-runtime
- https://github.com/codemix/flow-runtime
- Or just manual assertions: https://gist.github.com/JohnWeisz/beb7b4dadc512be30ce6c7c1e4...
Why? It's not a particularly difficult task to write a parser that could convert between the two representations. The TypeScript compiler even provides an API that lets you access its AST.
At my job, we're currently running JS on the back-end and TS on the front-end. JSON-Schema can be used in both. We're also rebuilding our core functionality in Elixir, and guess what, JSON Schema works there, too, out of the box.