Open jsommr opened 3 years ago
Many thanks for your comments.
Indeed annotate() is only a temporary function. We are actively working on static typing as well as supporting user-defined types right now, first with JSound, and we may consider other schema languages at a later point.
So your request confirms that this is a useful feature and that the timing is good. Thank you for this!
Just looked at some of your recently closed pull requests on this topic and I gotta say, I'm really, really excited about this!
Thank you, Jan!
The next release will include a limited user-defined-type system with the JSound compact syntax. The type names can be reused as variable/parameter types and instance of/treat as expressions.
Example:
declare type local:id-and-sentence as { "id": "integer", "sentence": "string" }; let $local-data := ( {"id": 1, "sentence": "Hi I heard about Spark"}, {"id": 2, "sentence": "I wish Java could use case classes"}, {"id": 3, "sentence": "Logistic regression models are neat"} ) let $validated-data := validate type local:id-and-sentence* { $local-data }
return $validated-data instance of local:id-and-sentence+
The functionality will then continue to be expanded in subsequent releases.
Having a way to create custom types would enable auto generation of eg. OpenAPI 3 specifications based on static code analysis.
Say we have a query like this:
We know that the input is an optional string, and that string? is returned. But what if we have
Then we loose the rest of the type - say that
{ "test": 123 }
is passed into the query, we can't analyze it and know if that's valid or not.I see there is a branch about static types, which seem to do some pretty cool improvements on the builtin types, and as part RumbleML,
annotate
was introduced, but are there any plans to support something like how TypeScripts annotates objects and arrays, or even better, JSON Schema?Edit: The example in listing 1.23 on this guided tour for XQuery seems like a pretty good candidate, just with JSON Schema instead:
And ideally, one could create the schema like this:
Examples
https://example.com/address.json
https://example.com/query1.jq
https://example.com/person.json
https://example.com/query2.jq
Example with definitions
https://example.com/order.json
https://example.com/query3.jq
JSON Schema to JSONiq types
RumbleDB should turn types in schemas to JSONiq types, such as:
Other constraints such as
maxProperties
should be ignored, and are only used by linters.OpenAPI 3 example
JSound support
If JSound schemas has a way of identifying itself like JSON Schema
$schema: "..."
OpenAPIopenapi: "3.0.2"
, one should be able to import JSound schemas as well.