Style validation and TypeScript #1851
Replies: 8 comments 62 replies
-
@maxammann, @ntadej your feedback would be much welcomed here for native and rust stuff that I might not know how they work. There are cons, don't get me wrong, but I think @smellyshovel wrote it well and I generally agree. |
Beta Was this translation helpful? Give feedback.
-
If I understood the proposal, I believe there are significant disadvantages to using javascript as part of the style:
|
Beta Was this translation helpful? Give feedback.
-
Not sure how eliminating v8.json in favor of hand-writing types.g.ts (or its equivalent) eliminates the need for run-time validation. The style spec allows for constraints and multi-property relationships that go beyond basic type checking. Ultimately, the map control consumes a style JSON that needs to be parsed and validated - this is always going to be a run-time step. |
Beta Was this translation helpful? Give feedback.
-
Having attempted some work on the style spec package, I generally agree that the way things are set up are a little hard to work with. My suggestions would be:
|
Beta Was this translation helpful? Give feedback.
-
TLDR: I don't think typescript is a usable replacement for the JSON file due to extra spec information and code gen for other languages. List of things in the style spec JSON that can't be represented with TS types, and are only usable at runtime because the whole JSON object is exported at runtime:
You could include this information as a separate const JS object or separate JSON, but that kind of defeats the purpose of removing the style spec JSON and probably makes the status quo worse. Of these the default values and (I think) expression capabilities/requirements are critical information when it comes to generating the property evaluators, which means it's needed in every language that wants to evaluate styles at runtime. Overall while I agree that the current system is a pain to work with when you're working in TS, having that JSON file is critical to the already existing TS code (for defaults) and also critical for other platforms, where it's used for code generation. While it may be limiting, it also doesn't force those other platforms to have to support deserializing extremely complex TS types. For example, I've had to work on a Java tool that converts a different styling language into the Mapbox styling language, and being able to relatively easily generate a strongly typed style spec was very nice. If the spec was in typescript I'm not even sure if there is a good library for parsing and reasoning about typescript definitions outside the typescript compiler itself. |
Beta Was this translation helpful? Give feedback.
-
Schema definitionUsing something like JSON Schema could definitely be worth it to move away from e.g. the The TypeScript type system is Turing complete. So it actually should be possible to perform arbitrary checks. Though, that would result in very very ugly TypeScript type definitions. JSON Schema is exactly made for that purpose so it should be preferred. Schema ValidationSchema Validation should ideally happen at compile time of an application which uses maplibre (native or web). Schema validation must happen to a certain extent at runtime. The extent can vary though. It can be on a syntax level, and just let the application crash if a property is missing. Alternatively, we can so semantic checks at runtime which would catch any errors during the parsing stage of maplibre. There is no reason why checks at compile time could not also be done at runtime. We just need to include the validator in the runtime (maybe only optionally). TL;DR: There are different types of validation:
Styling LanguageFor maplibe-rs I actually have a much greater change in mind. I actually would love to move away from JSON as a language completely. After all the style specification is nothing more or less than a language. |
Beta Was this translation helpful? Give feedback.
-
After some conversations in the TSC meetings and some googling it seems that JSON Schema is the right tool for the job, after all the style.json file is a json file and it makes sense to validate it using json schema, I think that some IDEs also support auto complete for json schema defined json files (I've seen this for angular I believe). |
Beta Was this translation helpful? Give feedback.
-
Should the style spec definition move to its own repo? This was a point of discussion during the last TSC meeting. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TL;DR
Original text
During the discussion in #1805 (namely this one) it became clear to me that it's not only me who dislikes the current state of things with validation of syles (and the way the spec is defined in general).
To describe in short how it works (correct me if I'm wrong at something):
I feel like such an approach is... a bit of an overkill.
So I was wondering whether it would be possible to get rid of the first 2 steps and instead define all the style spec manually, writing a .d.ts file by hand.
Also, I suggest to remove all the runtime validation and rely purely on TypeScript. The users of GL JS who care use TypeScript in their consumer-projects anyway. For those who don't, we might release a separate tool to validate the whole style json or even separate parts of it. It could be either a web-based solution (something like TS playground), where you manually copy-paste your style and it tells you what's wrong about it. Or it could be a CLI-tool of some sort.
The pros I can see:
The cons:
At the time being it's purely an idea. Would like to discuss this with the community, and any feedback is highly appreciated. Especially, taking into account what Harel has mentioned: the spec in its current version is now used not only in GL JS, but also in Native and Docs, so I'm not even sure how realistic is my idea at all.
Beta Was this translation helpful? Give feedback.
All reactions