Live data from Hacker News

Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

news.ycombinator.com

131–140 of 179 posts

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#131

> Do you use JSON Schema? No, but I don’t do much backend stuff, anymore. I’m totally anal about Quality, so I want to do things like jsonSchema. I used to deliberately publish APIs in both JSON and XML, so that I could use XML Schema to validate the data, but JSON, to actually use it. XML Schema is a huge PItA. I don’t like auto-generated Schema (see “anal,” above), so I tend to hand-tune (or write code to dynamical…

> XML Schema is a huge PItA

Yes. In the past I used to use Schematron; it's quite fun and extremely straightforward, although it's better described as a set of rules / tests than a tool to describe the structure of a document.

https://en.wikipedia.org/wiki/Schematron

A Schematron equivalent in JS would be cool. Maybe it already exists?

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#133
post #36

JSON Schema is awesome. I wish Typescript had better support for it though, having to do stuff in Zod and JSON Scheme sucks. I have a system I built that compiles TS types to JSON schema, which then validates data coming into my endpoints, this way I am typesafe at compile time (Typescript API) but if someone hits my REST endpoint w/o using my library, I still get runtime goodness. The number of different ways that J…

Maybe a better solution (not necessarily for your exact your use case) would be to generate the Typescript types from JSON schema? The schema feels more like it should be the real source of truth. That's how protobuf (for example) works - there's a language-independent schema and you can generate types for any languages you wish (but don't have to depend on any particular language if you don't need it).

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#135
post #105
post #58

Earlier quoted context omitted.

Most validation has to be done in custom code after deserialization (e.g. maybe a schema can enforce that the "collection ID" is in the right format, but it can't enforce that that collection actually exists in the datastore). There's definitely value in having a library/code generator/etc. to mechanically do the bytes -> structured value deserialization, but the cost/benefit of doing more complex validations at that…

For simple enough APIs it's possible to have all the validation in the form of JSON Scheme. Surely the id may be missing, but that could be not a "client error" - the input is syntactically valid, and for some APIs that's enough - but will cause the server to return the empty result set.

You can't accept a write if you're going to break a foreign key constraint when you insert the row. And I struggle to imagine a case where you want to do complex validation like "multiple of x" or "matches this regex" but don't want to actually check if the thing exists.

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#136

Earlier quoted context omitted.

> It really needs comments. Unfortunately it'll never be backwards compatible. It's very helpful for debugging If you want to have comments, consider using YAML for definitions where possible as it is a superset of JSON[0]: YAML is also a superset of JSON, so JSON files are valid in YAML. 0 - https://www.redhat.com/en/topics/automation/what-is-yaml

Just for fun, I wondered if YAML '#' comments could exist within a multi-line formatted JSON file. I cannot say the below holds for all YAML libraries, but can say the below works. Assume there is a file named "foo.yaml" which has the following content: { # this is a comment "hello" : "world", array : [ # first number 1, # second number 2 ] } Running: ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))'…

you can reduce further this by running:

    cue export foo.yaml --out json
If you have an existing JSON file, add // comments wherever and run:

    cue export cue: - --out json 
The `cue: -` tells cue how to interpret stdin and has many options

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#137
post #131

> Do you use JSON Schema? No, but I don’t do much backend stuff, anymore. I’m totally anal about Quality, so I want to do things like jsonSchema. I used to deliberately publish APIs in both JSON and XML, so that I could use XML Schema to validate the data, but JSON, to actually use it. XML Schema is a huge PItA. I don’t like auto-generated Schema (see “anal,” above), so I tend to hand-tune (or write code to dynamical…

> XML Schema is a huge PItA Yes. In the past I used to use Schematron; it's quite fun and extremely straightforward, although it's better described as a set of rules / tests than a tool to describe the structure of a document. https://en.wikipedia.org/wiki/Schematron A Schematron equivalent in JS would be cool. Maybe it already exists?

In some sense, JSON Schema does lean in that direction. In JSON Schema anything is valid except what you constrain. For example, the schema {} will accept any valid JSON. Anything you add to the schema is kind of like a test that further constraints the schema.

If I say {"type": "object"}, now my schema will accept any valid object. If I add {"type": "object", "properties": {"foo": "string"}}, now if there is a property "foo", it must have type string. I can still have whatever other properties I want of whatever type.

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#138
post #11

Is this the right place to complain about lack of a native Date datatype in JSON? Also lack of inheritance support. (For example I want a way to specify that my json object should be deserialized as Dog not as Animal.)

This doesn't deal with what type of object you create on deserialization, but you can have some form of inheritance in JSON Schema. You create a definition for the parent class and then reference that definition in the subclass and add whatever other attributes you need. JSON Schema doesn't have any understanding of classes, but this will allow you to do something similar.

Re: Ask HN: Do you use JSON Schema? Help us shape its future stability guarantees

#139
post #14
post #10

Earlier quoted context omitted.

I legit saw a team asking for a tool that does JSON -> JSON transformations at work. The XSLT for JSON vibes were almost too strong.

That's jq. At least useful for a lot of transformations.

yes, jq is XSLT (and more) for JSON
Post reply on HN