Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created, edited, published, released]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
README.md
testdata
.github
.idea
24 changes: 24 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
48 changes: 40 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
# JSON Dereference CLI
# JSON Dereference CLI v2

*Very* simple CLI tool that wraps the excellent [json-schema-ref-parser](https://github.com/BigstickCarpet/json-schema-ref-parser) library.
*Very* simple CLI tool that wraps the
excellent [json-schema-ref-parser](https://github.com/BigstickCarpet/json-schema-ref-parser) library.

[![Node.js Package](https://github.com/sedflix/json-dereference-cli-v2/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/sedflix/json-dereference-cli-v2/actions/workflows/npm-publish.yml)

## Usage

```bash
# Using npx:
npx json-dereference-cli-v2 -s <schema> [-i <spaces>] [-o <output>] [-t <type>]

# Installing globally:
npm install -g json-dereference-cli-v2
json-dereference-v2 -s <schema> [-i <spaces>] [-o <output>] [-t <type>]
```
npm install -g json-dereference-cli
json-dereference -s my-schema.json -o compiled-schema.yaml

### Options
- `-s <schema>`: Path to the input schema file (required).
- `-i <spaces>`: Number of spaces for indentation in the output (default: 2).
- `-o <output>`: Path to the output file (optional).
- `-t <type>`: Output type (`json` or `yaml`). If not specified, it is inferred from the output file extension.

### Examples

#### Dereference a JSON schema and output to a file:
```bash
json-dereference-v2 -s testdata/correct.schema.json -o testdata/output.json
```

_Note: The input file can either be `json`, or `yaml` / `yml`._
#### Dereference a JSON schema and print to stdout in YAML format:
```bash
json-dereference-v2 -s testdata/correct.schema.json -t yaml
```

*Note:* The input file can either be `json`, or `yaml` / `yml`.

_Note: The output file types are either `json` or `yaml` / `yml`. This is determined from the file extension for the output file path passed in._
*Note:* The output file types are either `json` or `yaml` / `yml`. This is determined from the file extension for the
output file path passed in or using `-t json|yaml` when writing to stdout.

### $ref: "s3://.."
### Meta-Validation

This CLI tool will also attempt to resolve S3 references using the `aws-sdk`. Take a look [here](/s3-resolver.js) to see the custom resolver code.
The CLI now supports validating JSON Schemas against their meta-schema to ensure correctness and adherence to best practices. This is automatically performed during dereferencing.

#### Example:
```bash
json-dereference-v2 -s testdata/correct.schema.json
```
If the schema is invalid, an error message will be displayed.
175 changes: 144 additions & 31 deletions dereference.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,155 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var util = require('util');
var $RefParser = require('json-schema-ref-parser');
var argv = require('minimist')(process.argv.slice(2));
var s3Resolver = require('./s3-resolver');

if (!argv.s || !argv.o) {
console.log('USAGE: ' + process.argv[1] + ' -s <schema> -o <output> [...]');
process.exit(1);
const fs = require('fs');
const path = require('path');
const $RefParser = require('@apidevtools/json-schema-ref-parser');
const argv = require('minimist')(process.argv.slice(2));
const yaml = require('js-yaml');
const Ajv = require('ajv');
const Ajv2020 = require('ajv/dist/2020');

// Add a new CLI option `--no-validate` to disable schema validation
const disableValidation = argv['no-validate'] || false;

/**
* Validates CLI arguments and returns parsed options.
* @returns {Object} Parsed CLI options.
*/
function validateArguments() {
if (!argv.s) {
console.error('Usage: ' + process.argv[1] + ' -s <schema> [-i <spaces>] [-o <output>] [-t <type>] [--no-validate]');
process.exit(1);
}

return {
input: path.resolve(argv.s),
output: argv.o ? path.resolve(argv.o) : undefined,
indent: argv.i !== undefined ? argv.i : 2,
type: argv.t
};
}

/**
* Detects the output format based on CLI arguments or file extension.
* @param {string} output - Output file path.
* @param {string} type - Output type specified by the user.
* @returns {string} Detected output type ('json' or 'yaml').
*/
function detectOutputFormat(output, type) {
if (type) {
return type;
} else if (output) {
const ext = path.parse(output).ext;
if (ext === '.json') {
return 'json';
} else if (ext.match(/^\.?(yaml|yml)$/)) {
return 'yaml';
} else {
console.error('ERROR: Cannot detect output file type from file name (please set -t <type>): ' + output);
process.exit(1);
}
}
return 'json';
}

if (argv.b) s3Resolver.bucket = argv.b;
/**
* Validates the input schema against the appropriate JSON Schema meta-schema.
* @param {Object} schema - The JSON schema to validate.
* @param {string} schemaVersion - The JSON Schema version (e.g., '2020-12', 'draft-07').
* @throws Will throw an error if the schema is invalid or the version is unsupported.
*/
function validateSchemaMeta(schema, schemaVersion) {
let ajv;

var input = path.resolve(argv.s);
if (schemaVersion === '2020-12') {
ajv = new Ajv2020();
} else if (schemaVersion === 'draft-07') {
ajv = new Ajv();
const draft07MetaSchema = require('ajv/lib/refs/json-schema-draft-07.json');
if (!ajv.getSchema(draft07MetaSchema.$id)) {
ajv.addMetaSchema(draft07MetaSchema);
}
} else {
throw new Error(`Unsupported schema version: ${schemaVersion}`);
}

var schema = fs.readFileSync(input, { encoding: 'utf8' });
const validate = ajv.compile(ajv.getSchema('$schema') || {});

console.log("Dereferencing schema: " + input);
if (!validate(schema)) {
throw new Error('Schema meta-validation failed: ' + JSON.stringify(validate.errors, null, 2));
}
}

$RefParser.dereference(input, { resolve: { s3: s3Resolver } }, function(err, schema) {
if (err) {
console.error(err);
} else {
var output = path.resolve(argv.o);
var ext = path.parse(output).ext;
/**
* Detects the JSON Schema version from the $schema property.
* @param {Object} schema - The JSON schema.
* @returns {string} The detected schema version (e.g., '2020-12', 'draft-07').
*/
function detectSchemaVersion(schema) {
const schemaUrl = schema.$schema;

if (ext == '.json') {
var data = JSON.stringify(schema);
fs.writeFileSync(output, data, { encoding: 'utf8', flag: 'w' });
} else if (ext.match(/^\.?(yaml|yml)$/)) {
var yaml = require('node-yaml');
yaml.writeSync(output, schema, { encoding: 'utf8' })
if (!schemaUrl) {
console.warn('No $schema property found. Defaulting to draft-07 schema.');
return 'draft-07';
}

if (schemaUrl.includes('draft/2020-12')) {
return '2020-12';
} else if (schemaUrl.includes('draft-07')) {
return 'draft-07';
} else {
console.error("Unrecognised output file type: " + output);
process.exit(1);
throw new Error('Unsupported $schema property in the JSON Schema: ' + schemaUrl);
}
console.log("Wrote file: " + output);
}
});
}

/**
* Writes the resolved schema to the specified output or stdout.
* @param {string} output - Output file path.
* @param {string} data - Resolved schema data.
*/
function writeOutput(output, data) {
if (output) {
fs.writeFileSync(output, data, { encoding: 'utf8', flag: 'w' });
console.warn('Wrote file: ' + output);
} else {
console.log(data);
}
}

/**
* Main function to dereference the schema.
*/
async function main() {
const { input, output, indent, type } = validateArguments();

console.warn('Dereferencing schema: ' + input);
try {
const schema = await $RefParser.dereference(input, { resolve: {} });

if (!disableValidation) {
const schemaVersion = detectSchemaVersion(schema);
validateSchemaMeta(schema, schemaVersion);
} else {
console.warn('Schema validation is disabled.');
}

const outputType = detectOutputFormat(output, type);
let data;

if (outputType === 'json') {
data = JSON.stringify(schema, null, indent);
} else if (outputType === 'yaml') {
data = yaml.dump(schema, { encoding: 'utf8', indent: indent, noRefs: true });
} else {
console.error('ERROR: Unsupported output file type: ' + outputType);
process.exit(1);
}

writeOutput(output, data);
} catch (err) {
console.error('ERROR: ' + err.message);
process.exit(1);
}
}

main();
Loading