GithubHelp home page GithubHelp logo

json-schema-traverse's Introduction

json-schema-traverse

Traverse JSON Schema passing each schema object to callback

build npm coverage

Install

npm install json-schema-traverse

Usage

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

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.

Callback is passed these parameters:

  • schema: the current schema object
  • JSON pointer: from the root schema to the current schema object
  • root schema: the schema passed to traverse object
  • parent JSON pointer: from the root schema to the parent schema object (see below)
  • parent keyword: the keyword inside which this schema appears (e.g. properties, anyOf, etc.)
  • parent schema: not necessarily parent object/array; in the example above the parent schema for {type: 'string'} is the root schema
  • 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'

Traverse objects in all unknown keywords

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.

Enterprise support

json-schema-traverse package is a part of Tidelift enterprise subscription - it provides a centralised commercial support to open-source software users, in addition to the support provided by software maintainers.

Security contact

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure. Please do NOT report security vulnerability via GitHub issues.

License

MIT

json-schema-traverse's People

Contributors

blackdynamo avatar epoberezkin avatar fogine avatar sondrele avatar willfarrell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

json-schema-traverse's Issues

TypeScript conversion

I'm going to fork and convert to TypeScript - would you like me to:

  1. Send a pull request for the conversion
  2. Send a pull request that leaves the package alone except adding index.d.ts and updating the typings property in the package.json
  3. None of the above, you're happy with the package as is

Reduce the size of the npm package by limiting the included files

Looks like the files property (https://docs.npmjs.com/files/package.json#files) is not used in package.json to specify the included files, nor is the .npmignore file (https://docs.npmjs.com/misc/developers#keeping-files-out-of-your-package) is being used for blacklisting unwanted files, for the package published to npm.

Would you consider adding either the files property or the .npmignore file, so that the resulting package file would have smaller size?

The current size can be seen when executing the command npm pack (https://docs.npmjs.com/cli/pack).

This issue was create via tawata

Pass dataPath in callback

I'm wondering why where is no dataPath along with schemaPath (the second argument in the callback)?

Exception has occurred: RangeError: Maximum call stack size exceeded

If an OpenAPI schema definition contains cycles, then the traversal fails with the error and call stackbelow:

Exception has occurred: RangeError: Maximum call stack size exceeded
  at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:63:19]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:79:9]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:76:13]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:79:9]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:76:13]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:79:9]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:76:13]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:79:9]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:76:13]())
    at _traverse ([/*****/node_modules/ajv/node_modules/json-schema-traverse/index.js:79:9]())

post-order traversal

Hi! I'm working on a PR to support both pre- and post-order traversal (i.e. calling cb before calling into children like today, and calling into it afterwards, or perhaps calling one function before and a different function afterwards.)

So I guess the first question is, are you interested in such a PR? :)

Second; at first, I thought, "I'll just make it so you can all traverse(schema, cb) and it will do pre-order like today, and I'll make it so if you call traverse(schema, {pre, post}) then it will call pre and/or post depending on which are provided."

But then I realized the optional opts makes this slightly more complicated. If you provide opts, should you call traverse(schema, {allKeys: true}, {pre, post})? Or should it be traverse(schema, {allKeys: true, pre, post}) and not provide a cb at all? Or maybe I'm going about this wrong and it should really be traverse(schema, pre, post) or traverse(schema, opts, pre, post).

Since this is your baby, I figured I'd check in and see if you had a preference before I submitted a PR. :)

Allow callback to return false to stop traversal

When traversing a schema to find whether a keyword is defined or not, it is helpful to be able to stop the traversal once a keyword is found.

Happy to submit a patch if this is ok with you.

doesn't traverse inside arrays

Example:

I'm trying to harvest all field properties that appear in a JSON object.

image
Using allKeys: true it works as long as the fields don't appear inside arrays, but it doesn't traverse inside layer: [{ field: "test" }] to capture test, as it should ?

npm ERR 429 - too many requests

Not sure if this is an error with NPM or you... (somehow?) but i'm receiving an NPM ERR 429 on my CI/CD pipeline which i assume is coming from my install of AJV or AJV-Keywords.

npm WARN tar ENOENT: no such file or directory, open '/usr/src/app/node_modules/.staging/fast-json-stable-stringify-dd7d8d34/benchmark/test.json'
 npm ERR! code E429
 npm ERR! 429 Too Many Requests - GET https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2020-02-17T12_46_57_764Z-debug.log

Cannot run basic example

Hi Evgeny,

I am trying to run your library, but upon fixing basic missing errors (with let cb,pre,post;), I still get errors when I run the basic example provided under "Usage". The error I get is:

`json-schema-traverse/index.js:11
  var pre = (typeof cb == 'function') ? cb : cb.pre || function() {};
                                                ^
TypeError: Cannot read properties of undefined (reading 'pre')`

The following is the code I'm using:

const traverse = require('json-schema-traverse');
const schema = {
  properties: {
    foo: {type: 'string'},
    bar: {type: 'integer'}
  }
};

let cb:any;
let pre:any;
let post:any;
traverse(schema, {cb});

What would be a suggestion to solve this issue?

In addition, I am interested in using the library in order to traverse arbitrary json schemas? Is this library able to deal with json-schemas of arbitrary shapes?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.