3 ms·
This was my personal pet peeve for a while. I even half-assed a spec language called RELAX JSON that I bet Bray would like. [0] But then I realized that what I
by skrebbel 11y ago
This was my personal pet peeve for a while. I even half-assed a spec language called RELAX JSON that I bet Bray would like. [0]
But then I realized that what I had made up was very similar to type definitions in TypeScript!
Especially if you use `interface` and not `class`, TypeScript gives you a lot of flexilibity in specifying data structures. You get optional values, typed arrays, typed but mixed arrays, references to other named types that you define yourself, and so on. Really, it's all you need and more. The only downside is that you can spec stuff that you can't put in JSON, such as Date objects. As a schema language, that is a bit weird. But just don't spec it like that! :-D
I'll post some examples in a reply when I'm back behind a computer.
[0]https://github.com/eteeselink/relax-json https://github.com/eteeselink/relax-json
- bcherny 11y agoHa! That's the reason I started working on https://github.com/bcherny/json-schema-to-typescript https://github.com/bcherny/json-schema-to-typescript. For the record, JSON Schemas are quite a bit more expressive than TypeScript interfaces, or even Scala Traits. I actually put together a big list of every JSON Schema constraint that isn't checkable at compile time in TS: https://github.com/bcherny/json-schema-to-typescript#not-expressible-in-typescript https://github.com/bcherny/json-schema-to-typescript#not-exp....
- skrebbel 11y agoCool stuff! Fair enough on the constraints. One of the design goals I had at the time was that I didn't want stuff like that to be expressible. Basically, it felt over the top to me - you might as well just allow writing predicates in plain JS then. Even more expressive! My primary goal was a spec language for humans to read and write. Machine validation came second. TypeScript, oddly maybe, appears to have a similar design goal.
- dandelany 11y agoI too used JSON Schema until it grated on me enough to finally abandon it... But I haven't taken the leap into TypeScript yet. Lately I've been specifying schemas with Tcomb[0], which I quite like. It's different from JSON Schema in that it's code and does runtime checking, so it's not a fit for every project. But it's pretty trivial to write a little node-based Tcomb validator if you want to be able to validate from a CLI. It's quite simple, but expressive, and I find that the fact that it's code can be a benefit, since it's simpler to compose complex schemas with code. As a bonus (or downside, depending on your use case), you also get pseudo-immutability on your Tcomb structs for free. [0] https://github.com/gcanti/tcomb https://github.com/gcanti/tcomb
- WorldMaker 11y agoI do find it nicer to write my JSON schemas first as Typescript interfaces. Looks like there's a decent tool to automate that conversion to a JSON schema file [1], but I haven't tried that yet. I also think it would be great if Typescript offered a "JSON strict interface" that could warn/error if you do something like use a Date instead of a string. (Even better if you could use that type information along with something like decorators for run-time checks before trying to `JSON.stringify`, at least in debug runs.) [1] https://github.com/lbovet/typson https://github.com/lbovet/typson