News for hackers. Stuff that matters.
Re:Show HN: Verify JSON using minimal schema by therufa on Thursday April 16, 2020 @02:46AM (#22886815)
There has been a schema specification for json already. it's called conveniently `json-schema` https://json-schema.org/
Re:Show HN: Verify JSON using minimal schema by cuu508 on Thursday April 16, 2020 @06:38AM (#22888006)
I 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/...

Usage examples in tests: https://github.com/healthchecks/healthchecks/blob/master/hc/...

Re:Show HN: Verify JSON using minimal schema by eventreduce on Thursday April 16, 2020 @03:55AM (#22887146)
I think the author knew this but wanted a more minimal type of validation.
Re:Show HN: Verify JSON using minimal schema by arethuza on Thursday April 16, 2020 @04:15AM (#22887254)
Json 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.

Re:Show HN: Verify JSON using minimal schema by xg15 on Thursday April 16, 2020 @06:35AM (#22887979)
> 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)

Re:Show HN: Verify JSON using minimal schema by eventreduce on Thursday April 16, 2020 @05:22AM (#22887574)
Json-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.
Re:Show HN: Verify JSON using minimal schema by arethuza on Thursday April 16, 2020 @05:34AM (#22887639)
Fair 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.
Re:Show HN: Verify JSON using minimal schema by franciscop on Thursday April 16, 2020 @04:19AM (#22887272)
I made a very similar library but based on `json-schema`: 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.