Live data from Hacker News

JSON Schema bundling formalised

json-schema.org

41–50 of 188 posts

Re: JSON Schema bundling formalised

#41

I haven't stumbled upon a better schema for JSON than the Typescript definition files. If only I could use them in non Typescript contexts. https://www.typescriptlang.org/docs/handbook/declaration-fil...

This is why I originally started a project to convert ts to json schema; https://github.com/YousefED/typescript-json-schema (and also check out a good alternative https://github.com/vega/ts-json-schema-generator)

Re: JSON Schema bundling formalised

#42

I haven't stumbled upon a better schema for JSON than the Typescript definition files. If only I could use them in non Typescript contexts. https://www.typescriptlang.org/docs/handbook/declaration-fil...

you might like https://jsontypedef.com/ if you haven't seen it

Re: JSON Schema bundling formalised

#43
Article:

> "Developers of platforms and libraries that use OpenAPI haven't had such a shake up before, and my feeling is it may take more than a few releases to correctly implement all the new shiny features full JSON Schema has to offer."

Dear OpenAPI, please avoid the shiny features.

Re: JSON Schema bundling formalised

#44
post #38

I generally prefer using JSON in my interactions, where possible. That's because it is lightweight, and 99.9% of the data I'm transferring is scalar. JSON is basically "implied" for scalar types. The good thing (if you want to call it "good") about XML, is Schema. Schema is a "rock hard" contract. It is definite, empirical, unambiguous. When I am looking at an API, and it has a Schema, then I know that I can figure o…

One thing I'm guilty of is designing a document format as JSON, thus the need to formally verify it. Passing data works well as JSON but documents works better as XML where you can use XML Schema.

This is where I would also develop an XML variant, even if it is never to be used, simply so that I can have a Schema to go along with it. I'll start with a data structure/class, in the code, then use a built-in transformer, to turn it into JSON or XML. That way, I know that the "kernel" of the data is the same, between them.

Even though JSON is a "natural" for scalar types, it can get weird, there. For example, let's say we have a float:

    { "kids": 1.0 }
It needs to be parsed as a float, because we could have:

    { "kids": 2.3 }
But I often see these sent as:

    { "kids": 1 }
or

    { "kids": "1.0" }
or even

    { "kids": "1" }
And I have differing results, based on the parser.

Re: JSON Schema bundling formalised

#45

Earlier quoted context omitted.

There are things you cannot accept. What do you do if an order has a negative quantity, if you get a string instead of a number, if a value does not fit in your DB column, etc ? Having this as part of your interface definition, along with generated representations of the possible data in the language your choice[0] and automatic (so you can't forget it) validation that rejects invalid input with HTTP 400 bad request…

What do you do if an order has a negative quantity, if you get a string instead of a number, if a value does not fit in your DB column, etc ? Isn't that all just basic data validation and sanitation that we all do anyway? And wouldn't it still have to be done, even if the JSON came with all the schemabloat? I don't think I'm suddenly going to accept that the data presented to my function is valid and fit for purposes…

"That's just passing the responsibility on to a third party, which feels lazy and dangerous to me."

You trust the memory manager, the SSL library and countless others to do their jobs, data validation is no different.

Whether a particular library is up to scratch, is, ofcourse, debatable.

Any library worth it's salt, like asp.net, allows you to define custon data type, like SKU, with whatever validation your heart desires.

Re: JSON Schema bundling formalised

#47

I haven't stumbled upon a better schema for JSON than the Typescript definition files. If only I could use them in non Typescript contexts. https://www.typescriptlang.org/docs/handbook/declaration-fil...

You can write validation schemas using zod library [1]. They end up looking pretty similar to typescript definitions (same vocabulary, same methods for combining/intersecting/filtering). And if you migrate to typescript you'll get type definitions for free out of those schemas.

[1] https://github.com/colinhacks/zod

Re: JSON Schema bundling formalised

#48
post #3

I might be exceptionally dense, but it's hard for me to see practical applications for something like this. In the end, if you implement this in your application, you will have a mechanism to say "this input document is invalid". AND THEN WHAT? Your only option is to discard it. I'd rather live by the old maxim "be liberal in what you accept, and strict in what you produce". But perhaps I'm overlooking an important u…

Why do you need to be liberal in what you accept, regarding to API? It just invites for subtle bugs. It makes sense for user-generated input, like phone numbers or addresses. But for software? You're encouraging buggy incomplete software that'll bite you or your client one way or another with that liberal approach.

API must be absolutely strict and unambiguous. It helps everyone in the end. Or you'll end up with parseInt abomination.

And for strict API you need strict validation. Probably better than schema can provide, but schema is a good start with declarative description and proven battle-tested libraries. After request was validated by the schema, you can be sure in some facts, like which properties are not null, which values are numbers and so on. This already helps to eliminate lots of unnecessary code.

Re: JSON Schema bundling formalised

#49
> "There are several libraries which offer bundling solutions, however they all have caveats, and I haven't seen any to date which are fully JSON Schema aware."

But what's wrong with just stuffing the schemas in a flat array or object exactly?

Re: JSON Schema bundling formalised

#50

I'd like to offer a contrasting opinion to all of the other currently negative comments: I've used JSON schema in the past to validate outside input and it was a pleasant and straightforward experience. There are unfortunately no standard type declarations that I'm aware of, which is a pain, but tools exist to translate schema.org schemas[1] (I have not used this myself). [1]: https://github.com/charlestati/schema-or…

I agree with you, I didn’t expect the amount of negative comments. Used in the past and it was a great tool to “avoid representing invalid states” and also as documentation to share between teams.
Post reply on HN