5 ms·
There has been a schema specification for json already. it's called conveniently `json-schema` https://json-schema.org/ https://json-schema.org/
by therufa 6y ago
There has been a schema specification for json already. it's called conveniently `json-schema` https://json-schema.org/ https://json-schema.org/
- eventreduce 6y agoI think the author knew this but wanted a more minimal type of validation.
- arethuza 6y agoJson schema does scale down pretty well (indeed as the link above shows the simplest possible Json schema is just "{}") - it also has pretty good tool support (e.g. VS Code) - there is a Json schema for Json schemas which helps a lot. The main advantage of the approach in the article appears to be that its easy to extend validation with code - which is a nice touch. Pretty much any complex Json validation I have written has to combine both schema based validation and code based checks - usually done completely separately.
- eventreduce 6y agoJson-schema itself scales down pretty well. But the libraries for validating it do not. Even if your schema is only {}, the build size increment because of pulling in a jsonschema validator is enormous because of all these supported features.
- arethuza 6y agoFair enough I guess - don't think I've ever had a need to do client side JSON validation against a 'schema' so I've never had that concern.
- xg15 6y ago> Json schema does scale down pretty well (indeed as the link above shows the simplest possible Json schema is just "{}") I've not used JSON Schema so far, but I think showing the simplest possible definition does not tell much about how complex or easy to use a language is. E.g., it's easy to write C code for a working program that does nothing or just prints "Hello World". The resulting code is very simple and can be written without much experience in C. However that doesn't make C a simple language - and indeed writing programs in C that perform useful tasks is a lot more complex and requires more knowledge about the language. In the same way, it's nice that Json Schema has a compact syntax for the "do nothing" schema, but I think a comparison between practically used examples would be better. (Also the "do nothing" schema would have to compare against not doing schema validation at all - and compared to that, even the two brackets are still pretty costly)
- franciscop 6y agoI made a very similar library but based on `json-schema`: https://github.com/franciscop/json-ok https://github.com/franciscop/json-ok import ok from 'json-ok'; ok(24, { type: "number" }); // No issue ok(24, { type: "string" }); // throws ValidationError: is not of a type(s) string I wanted to use `ok-json` as a tongue-in-cheek name, but apparently since there is already a library called `okjson` npm didn't allow me to use it.
- cuu508 6y agoI wrote a minimal validator in python that supports a subset of JSON Schema. I needed just a few basic validation features for my project, and didn't want to pull in a full implementation as a dependency. I like how compact it turned out: https://github.com/healthchecks/healthchecks/blob/master/hc/lib/jsonschema.py https://github.com/healthchecks/healthchecks/blob/master/hc/... Usage examples in tests: https://github.com/healthchecks/healthchecks/blob/master/hc/lib/tests/test_jsonschema.py https://github.com/healthchecks/healthchecks/blob/master/hc/...