Description
This issue proposes a set of keywords that would let schemas reference external assertions about the permitted range of instances/values.
It is a different way of solving many of the same problems that $data and similar keywords hope to address, but in a more flexible fashion.
The basis of the proposal is a keyword that lets authors define the relationship of the value to other data:
{
type: "object",
title: "Purchase Order"
properties: {
"order_id": {
type: "number"
},
"customer_uid": {
type: "integer",
valueRange: "http://example.com/type/user/uid"
},
}
This specifies that the "customer_id"
property must be an integer, and must be within some externally defined range of legal values (identified by a URI).
How this is implemented would vary between validators. Validators might allow coders to implement custom handlers, and do something along the lines of:
validator.register("http://example.com/type/user/uid", function(uid){
return db.select("SELECT * FROM user WHERE uid=@0", uid).count() > 0;
});
Other keywords would automatically keep track of which values become legal to use:
{
type: "object",
title: "Purchase Order",
valueDefine: "http://example.com/type/user",
valueUniqueKeys: "http://example.com/type/user/uid",
properties: {
"uid": {
type: "number",
valueDefine: "http://example.com/type/user/uid",
},
"name": {
type: "string"
},
}
Here, the valueDefine
keyword registers the instance value as legal, when other instances assert a value must be within the given range.
Similarly, the valueUniqueKeys
keyword specifies that any values defined under the range name can be used to uniquely identify the whole, larger object they're found within. With these two keywords, I can ask the validator "Find me the document where uid = 3
" and it returns an entire instance which has the property {uid: 3, name:"Alice"}
.
I'm going to continue refining this, and maybe take some inspiration from JSON-LD; however I think this is in a good state to ask for feedback.
Related issues: json-schema-org/json-schema-spec#340