You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.6 KiB

4 years ago
  1. # json-schema-traverse
  2. Traverse JSON Schema passing each schema object to callback
  3. [![Build Status](https://travis-ci.org/epoberezkin/json-schema-traverse.svg?branch=master)](https://travis-ci.org/epoberezkin/json-schema-traverse)
  4. [![npm version](https://badge.fury.io/js/json-schema-traverse.svg)](https://www.npmjs.com/package/json-schema-traverse)
  5. [![Coverage Status](https://coveralls.io/repos/github/epoberezkin/json-schema-traverse/badge.svg?branch=master)](https://coveralls.io/github/epoberezkin/json-schema-traverse?branch=master)
  6. ## Install
  7. ```
  8. npm install json-schema-traverse
  9. ```
  10. ## Usage
  11. ```javascript
  12. const traverse = require('json-schema-traverse');
  13. const schema = {
  14. properties: {
  15. foo: {type: 'string'},
  16. bar: {type: 'integer'}
  17. }
  18. };
  19. traverse(schema, {cb});
  20. // cb is called 3 times with:
  21. // 1. root schema
  22. // 2. {type: 'string'}
  23. // 3. {type: 'integer'}
  24. // Or:
  25. traverse(schema, {cb: {pre, post}});
  26. // pre is called 3 times with:
  27. // 1. root schema
  28. // 2. {type: 'string'}
  29. // 3. {type: 'integer'}
  30. //
  31. // post is called 3 times with:
  32. // 1. {type: 'string'}
  33. // 2. {type: 'integer'}
  34. // 3. root schema
  35. ```
  36. Callback function `cb` is called for each schema object (not including draft-06 boolean schemas), including the root schema, in pre-order traversal. Schema references ($ref) are not resolved, they are passed as is. Alternatively, you can pass a `{pre, post}` object as `cb`, and then `pre` will be called before traversing child elements, and `post` will be called after all child elements have been traversed.
  37. Callback is passed these parameters:
  38. - _schema_: the current schema object
  39. - _JSON pointer_: from the root schema to the current schema object
  40. - _root schema_: the schema passed to `traverse` object
  41. - _parent JSON pointer_: from the root schema to the parent schema object (see below)
  42. - _parent keyword_: the keyword inside which this schema appears (e.g. `properties`, `anyOf`, etc.)
  43. - _parent schema_: not necessarily parent object/array; in the example above the parent schema for `{type: 'string'}` is the root schema
  44. - _index/property_: index or property name in the array/object containing multiple schemas; in the example above for `{type: 'string'}` the property name is `'foo'`
  45. ## Traverse objects in all unknown keywords
  46. ```javascript
  47. const traverse = require('json-schema-traverse');
  48. const schema = {
  49. mySchema: {
  50. minimum: 1,
  51. maximum: 2
  52. }
  53. };
  54. traverse(schema, {allKeys: true, cb});
  55. // cb is called 2 times with:
  56. // 1. root schema
  57. // 2. mySchema
  58. ```
  59. Without option `allKeys: true` callback will be called only with root schema.
  60. ## License
  61. [MIT](https://github.com/epoberezkin/json-schema-traverse/blob/master/LICENSE)