简体   繁体   中英

json-schema get the schema for given property in js

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?

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 (eg 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']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM