Live data from Hacker News

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

news.ycombinator.com

81–90 of 179 posts

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

#81
post #50
post #34

Earlier quoted context omitted.

> 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.) Use Typescript, then so long as it barks, you can treat it like a dog! And if it knows how to bark and meow, you can treat it as a dog or a cat at your leisure. :-D

The issue is when JSON has to be parsed on the server using Java/C#. How do you know whether to parse it as a Dog or as a Cat (both of which inherits from Animal)? Here's Microsoft's workaround: https://devblogs.microsoft.com/dotnet/system-text-json-in-do...

I must admit I'm a noob in this world so maybe I'm going about it the wrong way, but I used JSON Schema's oneOf[1] along with required[2] for this:

    "properties": {
      "cat": {
        "$ref": "#/$defs/catType"
      }
      "dog": {
        "$ref": "#/$defs/dogType"
      }
    },
    "oneOf": [
      {
        "required": ["cat"]
      },
      {
        "required": ["dog"]
      },
    ]
Usage would then look like:

    {
      "dog": {
        "name": "Charlie"
      }
    }
At least it's explicit and can be easily checked for validity.

[1]: https://json-schema.org/understanding-json-schema/reference/...

[2]: https://json-schema.org/understanding-json-schema/reference/...

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

#82
post #57

This gives you a pretty good feel for where it’s at: https://json-schema.org/blog/posts/future-of-json-schema I’m invested in it. I’m using it to provide implementation-specific validation of requests to/from a third party API. I wish there was a good macOS editor or IDEA plugin for it with autocomplete etc. The static generators from examples are obscure, ugly, minimal, and can’t account for variations. It isn’t ple…

The one out of the box with IntelliJ is pretty decent for checking YAMLs with JSON Schemas, is there something you're missing?

+1 it's great. It just finds the schemas all on its own. Love it. When I've written my own i had to change a setting to point it to my schema but that worked well too.

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

#83
post #59

I considered using it some time back for API payload validations. I found it bit too verbose for my use case then. I ended up writing my own lighter version and it worked pretty well. I later open sourced it here - https://github.com/sleeksky-dev/alt-schema .

I use OpenAPI and JSON Schema at work, but for persobal project ended up with my own alternative too. It's too verbose to be fun. https://github.com/alkun-org/open-kun/tree/main/bayan

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

#84

Earlier quoted context omitted.

Your [0] might be a change that can be made in a backwards compatible manner.. as long as current implementations don't diverge on what they do when the number is not an integer (whether they error or do something)

I think you would have to build in a "integerMultipleOf" filter and slow-deprecate "multipleOf" To be more specific, since I wasn't in GP... It's not clear what you should do when for example you want to see if 0.3f is a "multiple of" 0.1f (due to the whole 0.30000000000004 thing)

That sounds like a limitation of the implementation. Decimal types exist.

If the implementation wants to account for some floating point errors I think that's fine too. If they want to codify it though, maybe add an epsilon param so the user can specify how close they want it to be.

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

#85

Here's my gripe with jsonschema... In most cases where I want to do some validation on JSON I find that I usually have a class/struct/object that represents the payload and I want to unpack JSON into it (or dump that class to JSON). Ultimately, there are already nice tools to do this (eg; marshmallow on the python side). So unless I'm crossing language boundaries writing a separate jsonschema and using that is more w…

You can generate json schemas from other things you know. They have Typescript interface to Json schema compilers. You can probably fine one that generates a schema from python too.

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

#87
post #4

just commentary on JSON Scheme itself: I mainly seem to have no leapfrogged the need for this There was a time when the JSON parsers across different platforms were so finicky that schemas seemed like a solution, but the parsers got better at not breaking on unexpected data types or JSON structures I'm also usually able to do system design to return a smaller subset of data, with consistent data types Or someone else…

Validating JSON parsers is not really what JSON schema solves.

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

#88
post #45
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…

FWIW, I've had good success with Typebox over Zod for exactly this stuff. You don't need to compile out types, you just...have them.

Ditto. It took me a while (coming from C++) to get that types are not the ideal source of truth in TypeScript, due to their disappearance at runtime.

Having a runtime parser (which can consequently express things impossible with TypeScript types alone, like "a positive integer" rather than being satisfied with any instance of `number`) from which types are inferred, is a needed mindset shift made easy with Zod.

I use JSON Schemas for request validation and response serialisation (eg: [1]) in Fastify, derived from Zod parsers via zod-to-json-schema [2]. Some of Zod's runtime validation does not translate to JSON Schema (typically transforms), so YMMV. But this gives a good runtime glue with the static typing of request handlers.

[1] https://github.com/SocialGouv/e2esdk/blob/beta/packages/serv...

[2] https://github.com/StefanTerdell/zod-to-json-schema

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

#90
post #53

Earlier quoted context omitted.

Isn't that a great example of something that would benefit from comments; a generically named int array with no context? Regarding the ability to add comments to JSON, it can be done in a cumbersome manner, such as adding a field to an object called "comment", but it is a hack since this wont work for arrays anyway.

The point is that JSON is a data structure, comment is rather unnatural there. It can be added to JSON though, just like an array could be wrapped in an object with a "comment" field. Usually array name serves as a crude comment. Literate programming isn't very popular.

This does not help if you are integrating against someone else's API.
Post reply on HN