简体   繁体   English

json-schema获取js中给定属性的架构

[英]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. 我想知道,wich可能是检索架构给定属性的架构的好方法。
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) . 如果给定的属性名称架构中的存在properties和它的声明或$ref erenced这是相当简单的,事情就变得复杂时anyOf allOf oneOfdependencies来发挥作用。我想知道如果有一种方法挂钩到在一些验证库(tv4或z模式)检索类似getPossibleSchemaForProperty(propname) any suggestion for browser javascript? 对浏览器JavaScript有任何建议吗?

Disclaimer: I am the lead maintainer for tv4 免责声明:我是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)? 您是否要获取该属性的所有可能模式,或仅获取适用于特定数据实例的模式(例如,仅匹配的oneOf子句)? 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. 关于此, tv4上有一个未解决的问题 ,但是直到现在我还没有听到其他人的兴趣,因此我没有优先考虑它。 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: 这个另一个包 (也是我的 ,更快,但是功能更少)返回一个从JSON指针到模式URL的映射:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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