├── .github ├── FUNDING.yml └── workflows │ ├── build.yml │ └── publish.yml ├── spec ├── .eslintrc.yml ├── fixtures │ └── schema.js └── index.spec.js ├── .eslintrc.yml ├── index.d.ts ├── LICENSE ├── .gitignore ├── package.json ├── index.js └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: epoberezkin 2 | tidelift: "npm/json-schema-traverse" 3 | -------------------------------------------------------------------------------- /spec/.eslintrc.yml: -------------------------------------------------------------------------------- 1 | parserOptions: 2 | ecmaVersion: 6 3 | globals: 4 | beforeEach: false 5 | describe: false 6 | it: false 7 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [master] 6 | pull_request: 7 | branches: ["*"] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | node-version: [10.x, 12.x, 14.x] 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v1 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm install 24 | - run: npm test 25 | - name: Coveralls 26 | uses: coverallsapp/github-action@master 27 | with: 28 | github-token: ${{ secrets.GITHUB_TOKEN }} 29 | -------------------------------------------------------------------------------- /.eslintrc.yml: -------------------------------------------------------------------------------- 1 | extends: eslint:recommended 2 | env: 3 | node: true 4 | browser: true 5 | rules: 6 | block-scoped-var: 2 7 | complexity: [2, 15] 8 | curly: [2, multi-or-nest, consistent] 9 | dot-location: [2, property] 10 | dot-notation: 2 11 | indent: [2, 2, SwitchCase: 1] 12 | linebreak-style: [2, unix] 13 | new-cap: 2 14 | no-console: [2, allow: [warn, error]] 15 | no-else-return: 2 16 | no-eq-null: 2 17 | no-fallthrough: 2 18 | no-invalid-this: 2 19 | no-return-assign: 2 20 | no-shadow: 1 21 | no-trailing-spaces: 2 22 | no-use-before-define: [2, nofunc] 23 | quotes: [2, single, avoid-escape] 24 | semi: [2, always] 25 | strict: [2, global] 26 | valid-jsdoc: [2, requireReturn: false] 27 | no-control-regex: 0 28 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: publish 2 | 3 | on: 4 | release: 5 | types: [published] 6 | 7 | jobs: 8 | publish-npm: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | - uses: actions/setup-node@v1 13 | with: 14 | node-version: 14 15 | registry-url: https://registry.npmjs.org/ 16 | - run: npm install 17 | - run: npm test 18 | - name: Publish beta version to npm 19 | if: "github.event.release.prerelease" 20 | run: npm publish --tag beta 21 | env: 22 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 23 | - name: Publish to npm 24 | if: "!github.event.release.prerelease" 25 | run: npm publish 26 | env: 27 | NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} 28 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare function traverse( 2 | schema: traverse.SchemaObject, 3 | opts: traverse.Options, 4 | cb?: traverse.Callback 5 | ): void; 6 | 7 | declare function traverse( 8 | schema: traverse.SchemaObject, 9 | cb: traverse.Callback 10 | ): void; 11 | 12 | declare namespace traverse { 13 | interface SchemaObject { 14 | $id?: string; 15 | $schema?: string; 16 | [x: string]: any; 17 | } 18 | 19 | type Callback = ( 20 | schema: SchemaObject, 21 | jsonPtr: string, 22 | rootSchema: SchemaObject, 23 | parentJsonPtr?: string, 24 | parentKeyword?: string, 25 | parentSchema?: SchemaObject, 26 | keyIndex?: string | number 27 | ) => void; 28 | 29 | interface Options { 30 | allKeys?: boolean; 31 | cb?: 32 | | Callback 33 | | { 34 | pre?: Callback; 35 | post?: Callback; 36 | }; 37 | } 38 | } 39 | 40 | export = traverse; 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Evgeny Poberezkin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | .DS_Store 61 | 62 | package-lock.json 63 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-schema-traverse", 3 | "version": "1.0.0", 4 | "description": "Traverse JSON Schema passing each schema object to callback", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "files":[ 8 | "index.d.ts" 9 | ], 10 | "scripts": { 11 | "eslint": "eslint index.js spec", 12 | "test-spec": "mocha spec -R spec", 13 | "test": "npm run eslint && nyc npm run test-spec" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git+https://github.com/epoberezkin/json-schema-traverse.git" 18 | }, 19 | "keywords": [ 20 | "JSON-Schema", 21 | "traverse", 22 | "iterate" 23 | ], 24 | "author": "Evgeny Poberezkin", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/epoberezkin/json-schema-traverse/issues" 28 | }, 29 | "homepage": "https://github.com/epoberezkin/json-schema-traverse#readme", 30 | "devDependencies": { 31 | "eslint": "^7.3.1", 32 | "mocha": "^8.0.1", 33 | "nyc": "^15.0.0", 34 | "pre-commit": "^1.2.2" 35 | }, 36 | "nyc": { 37 | "exclude": [ 38 | "**/spec/**", 39 | "node_modules" 40 | ], 41 | "reporter": [ 42 | "lcov", 43 | "text-summary" 44 | ] 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var traverse = module.exports = function (schema, opts, cb) { 4 | // Legacy support for v0.3.1 and earlier. 5 | if (typeof opts == 'function') { 6 | cb = opts; 7 | opts = {}; 8 | } 9 | 10 | cb = opts.cb || cb; 11 | var pre = (typeof cb == 'function') ? cb : cb.pre || function() {}; 12 | var post = cb.post || function() {}; 13 | 14 | _traverse(opts, pre, post, schema, '', schema); 15 | }; 16 | 17 | 18 | traverse.keywords = { 19 | additionalItems: true, 20 | items: true, 21 | contains: true, 22 | additionalProperties: true, 23 | propertyNames: true, 24 | not: true, 25 | if: true, 26 | then: true, 27 | else: true 28 | }; 29 | 30 | traverse.arrayKeywords = { 31 | items: true, 32 | allOf: true, 33 | anyOf: true, 34 | oneOf: true 35 | }; 36 | 37 | traverse.propsKeywords = { 38 | $defs: true, 39 | definitions: true, 40 | properties: true, 41 | patternProperties: true, 42 | dependencies: true 43 | }; 44 | 45 | traverse.skipKeywords = { 46 | default: true, 47 | enum: true, 48 | const: true, 49 | required: true, 50 | maximum: true, 51 | minimum: true, 52 | exclusiveMaximum: true, 53 | exclusiveMinimum: true, 54 | multipleOf: true, 55 | maxLength: true, 56 | minLength: true, 57 | pattern: true, 58 | format: true, 59 | maxItems: true, 60 | minItems: true, 61 | uniqueItems: true, 62 | maxProperties: true, 63 | minProperties: true 64 | }; 65 | 66 | 67 | function _traverse(opts, pre, post, schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex) { 68 | if (schema && typeof schema == 'object' && !Array.isArray(schema)) { 69 | pre(schema, jsonPtr, rootSchema, parentJsonPtr, parentKeyword, parentSchema, keyIndex); 70 | for (var key in schema) { 71 | var sch = schema[key]; 72 | if (Array.isArray(sch)) { 73 | if (key in traverse.arrayKeywords) { 74 | for (var i=0; i