json-schema-traverse
遍历JSON模式,将每个模式对象传递给回调
安装
npm install json-schema-traverse
用法
const traverse = require('json-schema-traverse');
const schema = {
properties: {
foo: {type: 'string'},
bar: {type: 'integer'}
}
};
traverse(schema, {cb});
// cb is called 3 times with:
// 1. root schema
// 2. {type: 'string'}
// 3. {type: 'integer'}
// Or:
traverse(schema, {cb: {pre, post}});
// pre is called 3 times with:
// 1. root schema
// 2. {type: 'string'}
// 3. {type: 'integer'}
//
// post is called 3 times with:
// 1. {type: 'string'}
// 2. {type: 'integer'}
// 3. root schema
回调函数 cb 在预顺序遍历中对每个模式对象(不包括draft-06布尔模式),包括根模式调用。模式引用($ref)不被解析,它们被原样传递。或者,您可以通过a {pre, post} 对象作为 cb,然后 pre 将在遍历子元素之前调用,并且 post 将在遍历所有子元素后调用。
Callback传递这些参数:
- 模式:当前模式对象
- JSON指针:从根模式到当前模式对象
- 根模式:传递给的模式
traverse对象 - 父JSON指针:从根模式到父模式对象(见下文)
- 父母的关键字:这个模式出现在其中的关键字(例如:
properties,anyOf等)。 - 父模式:不一定是父对象/数组;在上面的示例中,父模式为
{type: 'string'}是根模式 - 索引/属性:包含多个模式的数组/对象中的索引或属性名称;在上面的例子中
{type: 'string'}属性名称为'foo'
遍历所有未知关键字中的对象
const traverse = require('json-schema-traverse');
const schema = {
mySchema: {
minimum: 1,
maximum: 2
}
};
traverse(schema, {allKeys: true, cb});
// cb is called 2 times with:
// 1. root schema
// 2. mySchema
没有选项 allKeys: true 回调只会被根模式调用。
企业支持
Json-schema-traverse包是 Tidelift企业订阅 -除了软件维护者提供的支持外,它还为开源软件用户提供集中的商业支持。
安全联系
要报告安全漏洞,请使用 Tidelift安全联系人。 Tidelift将协调修复和披露。请不要通过GitHub问题报告安全漏洞。