├── .gitignore ├── package.json ├── .github └── workflows │ └── semgrep.yml ├── lib └── loader.js ├── LICENSE ├── README.md └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-schema-loader", 3 | "version": "1.0.0", 4 | "description": "Webpack loader that resolves Json Schema references.", 5 | "license": "BSD-3-Clause", 6 | "homepage": "https://github.com/cloudflare/json-schema-loader#readme", 7 | "author": "Vojtech Miksu ", 8 | "main": "./lib/loader.js", 9 | "repository": { 10 | "url": "git+https://github.com/cloudflare/json-schema-loader.git", 11 | "type": "git" 12 | }, 13 | "dependencies": { 14 | "json-schema-ref-parser": "^3.0.2", 15 | "loader-utils": "^0.2.16", 16 | "lodash": "^4.10.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /.github/workflows/semgrep.yml: -------------------------------------------------------------------------------- 1 | 2 | on: 3 | pull_request: {} 4 | workflow_dispatch: {} 5 | push: 6 | branches: 7 | - main 8 | - master 9 | schedule: 10 | - cron: '0 0 * * *' 11 | name: Semgrep config 12 | jobs: 13 | semgrep: 14 | name: semgrep/ci 15 | runs-on: ubuntu-20.04 16 | env: 17 | SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} 18 | SEMGREP_URL: https://cloudflare.semgrep.dev 19 | SEMGREP_APP_URL: https://cloudflare.semgrep.dev 20 | SEMGREP_VERSION_CHECK_URL: https://cloudflare.semgrep.dev/api/check-version 21 | container: 22 | image: returntocorp/semgrep 23 | steps: 24 | - uses: actions/checkout@v3 25 | - run: semgrep ci 26 | -------------------------------------------------------------------------------- /lib/loader.js: -------------------------------------------------------------------------------- 1 | var loaderUtils = require('loader-utils'); 2 | var _ = require('lodash'); 3 | var RefParser = require('json-schema-ref-parser'); 4 | 5 | module.exports = function(source) { 6 | var query = loaderUtils.parseQuery(this.query); 7 | 8 | var callback = this.async(); 9 | var parser = new RefParser(); 10 | this.cacheable && this.cacheable(); 11 | 12 | parser.dereference(query.useSource ? JSON.parse(source) : this.resourcePath) 13 | .then(handleResolveSuccess.bind(this)) 14 | .catch(callback); 15 | 16 | function handleResolveSuccess(schema) { 17 | this.value = [schema]; 18 | var json = JSON.stringify(schema, null, 2); 19 | _.map(parser.$refs.paths(), this.addDependency); 20 | callback(null, 'module.exports = ' + json + ';', schema); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, CloudFlare. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | 8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | 10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Json Schema Loader 2 | 3 | While still used by the likewise-deprecated `doca` package, for the new `@cloudflare/doca` 4 | package, this package has been replaced by `@cloudflare/json-schema-ref-loader` in 5 | the [json-schema-tools](https://github.com/cloudflare/json-schema-tools) repository. 6 | It relies on several general-purpose JSON Schema utility packages that are also present 7 | in that repository. 8 | 9 | Depending on your use case, either this package or the new one may be more suitable. 10 | 11 | ---- 12 | 13 | This package is part of the [doca](https://github.com/cloudflare/doca) suite. Please file any issues at the [doca repository](https://github.com/cloudflare/doca/issues) 14 | 15 | ## Installation 16 | 17 | ``` 18 | npm install json-schema-loader --save 19 | ``` 20 | 21 | ## Description 22 | 23 | Webpack loader that resolves both internal and external [json schema](json-schema.org) references (`$ref` properties). The loader uses [json-schema-ref-parser](https://github.com/BigstickCarpet/json-schema-ref-parser) to resolve references. It supports both JSON and YAML. Based on [@pr0da/json-schema-loader](https://github.com/pr0da/json-schema-loader). 24 | 25 | ## Usage 26 | 27 | ```js 28 | var resolvedSchema = require('json-schema-loader!./schema.json'); 29 | ``` 30 | 31 | Or define it in your `webpack.config.js` 32 | 33 | ```js 34 | module: { 35 | loaders: [{ 36 | test: /\.json$/, 37 | exclude: /node_modules/, 38 | loader: 'json-schema-loader' 39 | }] 40 | } 41 | ``` 42 | ```js 43 | var resolvedSchema = require('./schema.json'); 44 | ``` 45 | 46 | ### Options 47 | 48 | #### `useSource: boolean` 49 | 50 | This loader normally passes the RefParser the `resourcePath`, not the source. If you want to chain loaders, make sure to set `useSource=true`. 51 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | argparse@^1.0.7: 6 | version "1.0.9" 7 | resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.9.tgz#73d83bc263f86e97f8cc4f6bae1b0e90a7d22c86" 8 | dependencies: 9 | sprintf-js "~1.0.2" 10 | 11 | big.js@^3.1.3: 12 | version "3.1.3" 13 | resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.1.3.tgz#4cada2193652eb3ca9ec8e55c9015669c9806978" 14 | 15 | call-me-maybe@^1.0.1: 16 | version "1.0.1" 17 | resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b" 18 | 19 | commander@^2.7.1: 20 | version "2.9.0" 21 | resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4" 22 | dependencies: 23 | graceful-readlink ">= 1.0.0" 24 | 25 | debug@^2.2.0: 26 | version "2.6.0" 27 | resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.0.tgz#bc596bcabe7617f11d9fa15361eded5608b8499b" 28 | dependencies: 29 | ms "0.7.2" 30 | 31 | emojis-list@^2.0.0: 32 | version "2.1.0" 33 | resolved "https://registry.yarnpkg.com/emojis-list/-/emojis-list-2.1.0.tgz#4daa4d9db00f9819880c79fa457ae5b09a1fd389" 34 | 35 | es6-promise@^3.1.2: 36 | version "3.3.1" 37 | resolved "https://registry.yarnpkg.com/es6-promise/-/es6-promise-3.3.1.tgz#a08cdde84ccdbf34d027a1451bc91d4bcd28a613" 38 | 39 | esprima@^2.6.0: 40 | version "2.7.3" 41 | resolved "https://registry.yarnpkg.com/esprima/-/esprima-2.7.3.tgz#96e3b70d5779f6ad49cd032673d1c312767ba581" 42 | 43 | "graceful-readlink@>= 1.0.0": 44 | version "1.0.1" 45 | resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" 46 | 47 | js-yaml@^3.6.0: 48 | version "3.7.0" 49 | resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.7.0.tgz#5c967ddd837a9bfdca5f2de84253abe8a1c03b80" 50 | dependencies: 51 | argparse "^1.0.7" 52 | esprima "^2.6.0" 53 | 54 | json-schema-ref-parser@^3.0.2: 55 | version "3.1.2" 56 | resolved "https://registry.yarnpkg.com/json-schema-ref-parser/-/json-schema-ref-parser-3.1.2.tgz#a38ecb7774f87f32e7eb9723d5921390e76a9a42" 57 | dependencies: 58 | call-me-maybe "^1.0.1" 59 | debug "^2.2.0" 60 | es6-promise "^3.1.2" 61 | js-yaml "^3.6.0" 62 | ono "^2.2.1" 63 | z-schema "^3.17.0" 64 | 65 | json5@^0.5.0: 66 | version "0.5.1" 67 | resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821" 68 | 69 | loader-utils@^0.2.16: 70 | version "0.2.16" 71 | resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-0.2.16.tgz#f08632066ed8282835dff88dfb52704765adee6d" 72 | dependencies: 73 | big.js "^3.1.3" 74 | emojis-list "^2.0.0" 75 | json5 "^0.5.0" 76 | object-assign "^4.0.1" 77 | 78 | lodash.get@^4.1.2: 79 | version "4.4.2" 80 | resolved "https://registry.yarnpkg.com/lodash.get/-/lodash.get-4.4.2.tgz#2d177f652fa31e939b4438d5341499dfa3825e99" 81 | 82 | lodash.isequal@^4.4.0: 83 | version "4.5.0" 84 | resolved "https://registry.yarnpkg.com/lodash.isequal/-/lodash.isequal-4.5.0.tgz#415c4478f2bcc30120c22ce10ed3226f7d3e18e0" 85 | 86 | lodash@^4.10.0: 87 | version "4.17.4" 88 | resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.4.tgz#78203a4d1c328ae1d86dca6460e369b57f4055ae" 89 | 90 | ms@0.7.2: 91 | version "0.7.2" 92 | resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765" 93 | 94 | object-assign@^4.0.1: 95 | version "4.1.1" 96 | resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" 97 | 98 | ono@^2.2.1: 99 | version "2.2.4" 100 | resolved "https://registry.yarnpkg.com/ono/-/ono-2.2.4.tgz#f6c1d9ea64da07a54863986535da3de67e502696" 101 | 102 | sprintf-js@~1.0.2: 103 | version "1.0.3" 104 | resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" 105 | 106 | validator@^6.0.0: 107 | version "6.2.1" 108 | resolved "https://registry.yarnpkg.com/validator/-/validator-6.2.1.tgz#bc575b78d15beb2e338a665ba9530c7f409ef667" 109 | 110 | z-schema@^3.17.0: 111 | version "3.18.2" 112 | resolved "https://registry.yarnpkg.com/z-schema/-/z-schema-3.18.2.tgz#e422196b5efe60b46adef3c3f2aef2deaa911161" 113 | dependencies: 114 | lodash.get "^4.1.2" 115 | lodash.isequal "^4.4.0" 116 | validator "^6.0.0" 117 | optionalDependencies: 118 | commander "^2.7.1" 119 | --------------------------------------------------------------------------------