-
-
Notifications
You must be signed in to change notification settings - Fork 276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$data #51
Comments
I feel that there are several use cases here, and it might be best to split them up. URI Template resolutionFor hypermedia interactions, where the instance data must be referenced in order for hyperlinking to work at all, the extended templating syntax from issue #52 covers the necessary use cases. It narrowly targets hyperlinking and does not involve instance data in any other aspect of JSON Schema, as it applies only in situation where we were already referencing instance data. Link titleI can see how a link title might reference data in the same way as the URI Template. The template may include the id of a related thing, while the title may include the related thing's name. Either way, the URI and the title are both things that are presented back to the user, which should be affected by the instance data as they are describing a relation involving that data. Therefore I would prefer to see the URI Template extended syntax (with "vars") be used here rather than a more generic approach that applies to more than just hypermedia values. rel and mediaTypeThe use cases for mediaType and rel are not immediately obvious to me. The relation type should not, in my mind, change based on the instance data. Only the specific instance to which the relation points should change. A mediaType specified at runtime from instance data would not be of use in planning what a program can do with different representations. It's not clear to me why you wouldn't just list out the possibilities. If some media type links may only be present some of the time, there are other ways to express that using "oneOf" (or possibly "switch") to associate links with only certain variations of the content. Interactions during validationI feel like this should somehow express the constraints in terms of the relations among the fields rather than pulling in data that will produce the desired result. So somehow explicitly saying that "larger" should be strictly greater than "smaller" without loading the data into the schema before validation. I'm not 100% sure what that would look like. In this case, possibly very much like the I don't feel like I'm articulating this well, but I'm going to go ahead and post this comment in the interest of provoking discussion :-) |
@handrews I think I have a basic understanding of what you're trying to say - you'd prefer a solution where JSON Schema validators use $data (or some other similar feature) as a pointer whose value is checked during validation, instead of recompiling the schema before validation even begins, where it inserts the value of those pointers directly into the schema. Would it be possible to modify the proposal to remove the portions about modifying the schema directly, and include language elsewhere in v6+ that validation shouldn't modify schemas for any reason? Then, $data would be functionally identical to the way it works now, but doesn't encourage "bad behavior" among the various JSON Schema validators. |
@HotelDon this is what I mean by "I'm not articulating this well" It's not so much the reading/loading of the data (which likely has to be done lazily because of $refs), it's what you can do with it after it is loaded. Although thinking about this more I may be OK with it. The way the proposal is written, with a list of allowed properties that trails off with "more..." left me very concerned about the scope. However what's not explicitly called out, but I think would be better than listing the fields, is that all of the fields proposed for $data take a literal value, and not a schema. We really need to make sure that data is never interpreted as a schema- that's a security nightmare- just use it to shove in links to all of your favorite malware sites! But I think the intent here is that I still think it is valuable to separate the hypermedia template resolution cases out and use |
I had never considered the possibility of someone trying to load schema's with $data, so I guess that is what got me confused. So maybe fix it to sound more like this:
I'm still having a hard time wrapping my head around the hypermedia/LDO portion of this proposal, so I don't have much of an opinion on it. It might be helpful if @geraintluff chimed in to defend his original proposal a bit, assuming he still has any interest in doing so. |
My 2¢: people seem to use it a lot with Ajv, judging by the questions. So it must be useful. I think relative JSON pointer should be extended to allow navigating array items (see #115) |
I've become more receptive of this proposal while working with some of the more difficult hyper-schema problems such as discussed in #108 |
I'm solidly of the opinion that checking data consistency is solidly out of the scope of JSON Schema. Although it's certainly an option for validators that do want to offer the feature. And if it's a popular feature then... maybe it's something we have to look into, perhaps as a separate document though. |
@awwright |
I can see this could be useful. A few clear usecases might be helpful if anyone has the time or inclination. |
@Relequestual I'd like to see whether PR #179 is accepted or not before digging into use cases here. If it is accepted, that will clarify how to present the future use cases. If it is not, I'll need to come up with a different approach anyway. |
I'm moving this out of draft-07/wright-*-02. It is a huge topic that has seen no progress and almost no real discussion in the past year. And there is no clear advocate with time available to move it forward. |
Random thought: Would it make sense to define If we went this route, it would also add to the use cases for #314 for understanding multiple vocabularies in use simultaneously. |
Separate vocabulary seems overkill... |
See @awwright 's Scope of JSON Schema Validation document for why this is unlikely to be taken up. |
Can we close this out since the general consensus is against this? |
I can't understand why the scope document is against this proposal. |
@yww325 Find this paragraph:
|
Just a note from perspective of me as an AJV user. I've been using AJV for 3 years and only today I discovered So as a user of JSON Schema I'm totally up for this (or an at least equally powerful alternative) being considered as a part or at least an extension of some sort. |
While commenting for the already closed issues might not be. a good practice, I must also say that $data in ajv has been extremely beneficial for us. I don't know how I could otherwise have done cross-references between the values in validated data. Leaving it out makes JSON Schema much less expressive to us, and we need to do more validation in application code that we could have left for schema. |
The sentiment is shared @laurisvan. However it's disagreement about how it should work that has kept it out of the spec. AJV's implementation is how that author thought it should be done. I also have implemented my own idea (and put it in a vocabulary) for my library JsonSchema.Net. While AJV may be in wider use, my implementation is arguably more in line with the spec's current state since it defines a vocabulary. Until alignment on how it should work can be achieved, we can't put it in the spec. If you would like to open this conversation again, I would suggest opening a new issue that:
|
For those in the future who will read @gregsdennis's last comment, here's the updated link to the vocabulary he mentioned. |
🤦 It's changed again. I've updated the link above. Hopefully this one will stick. |
Sorry @gregsdennis but the documentation doesn't bring me to the point of understanding how to use the "data" keyword in the context of Schema.NET. I'm looking at https://docs.json-everything.net/schema/vocabs/data-2022/ and there's only an example for minValue and that data MUST be an object and may use a JSON pointer. I'm trying to express the (apparently often requested) constraint that an item within an array must be one of the names laid out in a different array in the instance. |
Hey there @flq. So that's the vocab spec itself. There's a subfolder in that "Prebuilt Vocabularies" folder called "Examples". In there you'll find examples for instance and external data. I suspect you want instance data. The link to that doc page is https://docs.json-everything.net/schema/examples/data-ref. If you have any other issues you find with my site or libraries, please feel free to open an issue in my repo. |
Thank you for the response - I've joined the slack channel to elaborate. |
Originally written by @geraintluff at https://github.com/json-schema/json-schema/wiki/%24data-(v5-proposal)
NOTE: JSON Relative Pointer is defined as an extension of JSON Pointer, which means that an absolute JSON pointer is legal anywhere that a relative pointer is mentioned (but not vice versa).
Absolute JSON Pointers always begin with
/
, while relative JSON pointers always begin with a digit. Resolving a pointer beginning with/
behaves the same whether it is being resolved "relative" to a specific location or not, just as resolving a URI "/foo/bar" is resolved the same whether there is an existing path component to the URI or not.Proposed keywords
$data
This keyword would be available:
{"$data": ...}
) for the following schema properties:minimum
/maximum
exclusiveMinimum
/exclusiveMaximum
minItems
/maxItems
,enum
{"$data": ...}
) for the following LDO properties:href
rel
title
mediaType
Purpose
This keyword would allow schemas to use values from the data, specified using Relative JSON Pointers.
This allows more complex behaviour, including interaction between different parts of the data.
When used inside LDOs, this allows extraction of many more link attributes/parameters from the data.
Values
Wherever it is used, the value of
$data
is a Relative JSON Pointer.Behaviour
If the
$data
keyword is defined in a schema, then before any further processing of the schema:$data
is interpreted as a Relative JSON Pointer.When used in one of the permitted schema/LDO properties, then before any further processing of the schema/LDO:
$data
is interpreted as Relative JSON Pointer.Example
In the above example, the
"larger"
property must be strictly greater than the"smaller"
property.Concerns
Theoretical purity
Currently, validation is "context-free", meaning that one part of the data has minimal effect on the validation of another part. This has an effect on things like referencing sub-schemas. Changing this is a big issue, and should not be done lightly.
Some interplay of different parts of the data can currently be specified using
oneOf
(and the proposedswitch
) - but crucially, these constraints are specified in the schema for a common parent node, meaning that sub-schema referencing is still simple.The use of
$data
also (in some cases) limits the amount of static analysis that can be done on schemas, because their behaviour becomes much more data-dependent. However, the expressive power it opens up is quite substantial.Not available for all keywords
It's also tempting to allow its use for all schema keywords - however, not only is that a bad idea for keywords such as
properties
/id
, but it also might present an obstacle to anybody extending the standard.Not available inside
enum
valuesIt should be noted that while
{"enum": {"$data":...}}
would extract a list of possible values from the data,{"enum": [{"$data":...}]}
would not - it would in fact specify that there is only one valid value:{"$data":...}
.Similar concerns would exist with an extra keyword like
constant
- what if you want the constant value to be a literal{"$data":...}
? However, perhapsconstant
could be given this data-templating ability, and if you want a literal{"$data":...}
, then you can still useenum
.Describing using the meta-schema
The existing mechanics of
$ref
can be nicely described using arel="full"
link relation.The mechanics of
$data
, however, would be impossible to even approach in the meta-schema. We could describe the syntax, but nothing more. Is this a problem?The text was updated successfully, but these errors were encountered: