|
I once braindumped a similar idea on https://github.com/eteeselink/relax-json (inspired by RELAX NG, one of the few great things to come out of the XML world). A core difference between it and your JSchema is that it, itself, is not JSON - just like XML, I don't think JSON makes for a good format to write down schema definitions. In fact, I don't think JSON is very human friendly at all[0], which is OK for a data interchange format, occasionally read by humans but hardly ever written by hand. I did not further develop RELAX JSON, however, when I realized that TypeScript interface definitions[1] are a great JSON schema language: enum Color { Yellow, Brown };
interface Banana {
color: Color;
length: number;
}
interface FruitBasket {
bananas: Array<Banana>;
apples: Array<{color: Color; radius: number}>?;
}
It's best to use interfaces and not classes, because interfaces allow for optional fields (with the `?` suffix), which is pretty common in APIs and wherever JSON is used.I will write a validate-JSON-with-TypeScript-type-definitions checker as soon as I find a need for it. Open to ideas here, guys! (or alternatives) [0] Compare gruntfiles to webpack configs (tl;dr: they're JSON and JS-that-returns-an-object, respectively. the latter allows code, comments, unquoted keys, etc etc). [1] http://www.typescriptlang.org/Handbook#interfaces |