Socket
Socket
Sign inDemoInstall

json-schema-traverse

Package Overview
Dependencies
0
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.0 to 0.3.0

44

index.js
'use strict';
var traverse = module.exports = function (schema, cb) {
_traverse(schema, cb, '', schema);
var traverse = module.exports = function (schema, opts, cb) {
if (typeof opts == 'function') {
cb = opts;
opts = {};
}
_traverse(opts, cb, schema, '', schema);
};

@@ -31,4 +35,24 @@

traverse.skipKeywords = {
enum: true,
const: true,
required: true,
maximum: true,
minimum: true,
exclusiveMaximum: true,
exclusiveMinimum: true,
multipleOf: true,
maxLength: true,
minLength: true,
pattern: true,
format: true,
maxItems: true,
minItems: true,
uniqueItems: true,
maxProperties: true,
minProperties: true
};
function _traverse(schema, cb, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
function _traverse(opts, cb, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) {
if (schema && typeof schema == 'object' && !Array.isArray(schema)) {

@@ -41,9 +65,11 @@ cb(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex);

for (var i=0; i<sch.length; i++)
_traverse(sch[i], cb, jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
_traverse(opts, cb, sch[i], jsonPtr + '/' + key + '/' + i, rootSchema, jsonPtr, key, schema, i);
}
} else if (key in traverse.keywords) {
_traverse(sch, cb, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
} else if (key in traverse.propsKeywords && sch && typeof sch == 'object') {
for (var prop in sch)
_traverse(sch[prop], cb, jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
} else if (key in traverse.propsKeywords) {
if (sch && typeof sch == 'object') {
for (var prop in sch)
_traverse(opts, cb, sch[prop], jsonPtr + '/' + key + '/' + escapeJsonPtr(prop), rootSchema, jsonPtr, key, schema, prop);
}
} else if (key in traverse.keywords || (opts.allKeys && !(key in traverse.skipKeywords))) {
_traverse(opts, cb, sch, jsonPtr + '/' + key, rootSchema, jsonPtr, key, schema);
}

@@ -50,0 +76,0 @@ }

{
"name": "json-schema-traverse",
"version": "0.2.0",
"version": "0.3.0",
"description": "Traverse JSON Schema passing each schema object to callback",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -25,3 +25,3 @@ # json-schema-traverse

}
}
};

@@ -48,4 +48,24 @@ traverse(schema, cb);

## Traverse objects in all unknown keywords
```javascript
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
```
Without option `allKeys: true` callback will be called only with root schema.
## License
[MIT](https://github.com/epoberezkin/json-schema-traverse/blob/master/LICENSE)

@@ -7,5 +7,9 @@ 'use strict';

describe('json-schema-traverse', function() {
var calls;
beforeEach(function() {
calls = [];
});
it('should traverse all keywords containing schemas recursively', function() {
var calls = [];
var schema = require('./fixtures/schema').schema;

@@ -16,8 +20,85 @@ var expectedCalls = require('./fixtures/schema').expectedCalls;

assert.deepStrictEqual(calls, expectedCalls);
});
function callback() {
calls.push(Array.prototype.slice.call(arguments));
}
describe('allKeys option', function() {
var schema = {
someObject: {
minimum: 1,
maximum: 2
}
};
it('should traverse objects with allKeys: true option', function() {
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
var expectedCalls = [
[schema, '', schema, undefined, undefined, undefined, undefined],
[schema.someObject, '/someObject', schema, '', 'someObject', schema, undefined]
];
traverse(schema, {allKeys: true}, callback);
assert.deepStrictEqual(calls, expectedCalls);
});
it('should NOT traverse objects with allKeys: false option', function() {
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
var expectedCalls = [
[schema, '', schema, undefined, undefined, undefined, undefined]
];
traverse(schema, {allKeys: false}, callback);
assert.deepStrictEqual(calls, expectedCalls);
});
it('should NOT traverse objects without allKeys option', function() {
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
var expectedCalls = [
[schema, '', schema, undefined, undefined, undefined, undefined]
];
traverse(schema, callback);
assert.deepStrictEqual(calls, expectedCalls);
});
it('should NOT travers objects in standard keywords which value is not a schema', function() {
var schema2 = {
const: {foo: 'bar'},
enum: ['a', 'b'],
required: ['foo'],
another: {
},
patternProperties: {}, // will not traverse - no properties
dependencies: true, // will not traverse - invalid
properties: {
smaller: {
type: 'number'
},
larger: {
type: 'number',
minimum: {$data: '1/smaller'}
}
}
};
// schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex
var expectedCalls = [
[schema2, '', schema2, undefined, undefined, undefined, undefined],
[schema2.another, '/another', schema2, '', 'another', schema2, undefined],
[schema2.properties.smaller, '/properties/smaller', schema2, '', 'properties', schema2, 'smaller'],
[schema2.properties.larger, '/properties/larger', schema2, '', 'properties', schema2, 'larger'],
];
traverse(schema2, {allKeys: true}, callback);
assert.deepStrictEqual(calls, expectedCalls);
});
});
function callback() {
calls.push(Array.prototype.slice.call(arguments));
}
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc