Reputation: 1699
I was wondering wich could be a good approach to retrieve the schema for a given property of a schema.
if the given property name is present in schema's properties
and it's declared or $ref
erenced that's quite straightforward,
things get complicate when anyOf
allOf
oneOf
or dependencies
come into play..
i was wondering if there's a way to hook into a validation library in some way (tv4 or z-schema) to retrieve something like a getPossibleSchemaForProperty(propname)
.
any suggestion for browser javascript?
Upvotes: 0
Views: 680
Reputation: 12863
Disclaimer: I am the lead maintainer for tv4
Are you looking to get all possible schemas for that property, or only the ones that apply to a specific data instance (e.g. only the oneOf
clause that matches)? Assuming it's the latter (which I generally refer to as "schema assignment"):
There is an open issue on tv4 for this, but I haven't heard any interest from other people until now, so I didn't prioritise it. If people want it, I can add it.
This other package (also mine, faster but fewer features) returns a map from JSON Pointers to schema URLs:
// Creates a validator for that schema
var validator = JsonModel.validator('https://example.com/schema1');
// Runs the validator on the data
var result = validator({
"foo": {
"bar": [1, 2, 3]
}
});
// pass or fail
console.log(result.valid);
// list of schemas describing that sub-sub-property
var subSubSchemas = result.schemas['/foo/bar']);
Upvotes: 2