json-schema获取js中给定属性的模式

时间:2014-10-24 14:28:34

标签: javascript jsonschema

我想知道如何检索模式的给定属性的模式是一种很好的方法 如果给定的属性名称出现在架构的properties中,并且它已被声明或$ref表示非常简单, 当anyOf allOf oneOfdependencies发挥作用时,事情会变得复杂。 我想知道是否有办法以某种方式挂钩进入验证库(tv4或z-schema)以检索类似getPossibleSchemaForProperty(propname)的内容。 有关浏览器javascript的任何建议吗?

1 个答案:

答案 0 :(得分:2)

免责声明:我是tv4的主要维护者

您是否希望获取该属性的所有可能模式,或仅查找适用于特定数据实例的模式(例如,仅匹配的oneOf子句)?假设它是后者(我通常称之为“架构分配”):

这有一个open issue on tv4,但直到现在我还没有听到其他人的兴趣,所以我没有优先考虑它。如果人们想要它,我可以添加它。

This other package(也是我的,速度更快但功能更少)会将JSON指针中的地图返回到架构网址:

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