From 178f313a4cd3ba0513e07d94013aac4e1391e151 Mon Sep 17 00:00:00 2001 From: Adam Voss Date: Thu, 29 Jun 2017 14:53:27 -0500 Subject: [PATCH] Rebrand as '`pajv` and remove commands that will not be supported --- LICENSE | 22 +++ README.md | 115 +++------------ commands/compile.js | 71 --------- commands/help.js | 88 ++--------- commands/index.js | 2 - commands/migrate.js | 73 --------- package.json | 24 +-- test/compile.spec.js | 163 --------------------- test/help.spec.js | 22 +-- test/migrate.spec.js | 109 -------------- test/migrate/expected_migrated_schema.json | 18 --- test/migrate/schema.json | 22 --- test/migrate/schema_invalid.json | 3 - test/migrate/schema_no_changes.json | 3 - 14 files changed, 66 insertions(+), 669 deletions(-) create mode 100644 LICENSE delete mode 100644 commands/compile.js delete mode 100644 commands/migrate.js delete mode 100644 test/compile.spec.js delete mode 100644 test/migrate.spec.js delete mode 100644 test/migrate/expected_migrated_schema.json delete mode 100644 test/migrate/schema.json delete mode 100644 test/migrate/schema_invalid.json delete mode 100644 test/migrate/schema_no_changes.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69fd5b --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) Adam Voss +Copyright (c) Jesse Collis + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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 OR COPYRIGHT HOLDERS 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. diff --git a/README.md b/README.md index f2f546f..bec3b06 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,11 @@ -# ajv-cli +# Polyglottal [Another] JSON Schema Validator -Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the [fastest json schema validators](https://github.com/ebdrup/json-schema-benchmark). - -[![Build Status](https://travis-ci.org/jessedc/ajv-cli.svg?branch=master)](https://travis-ci.org/jessedc/ajv-cli) -[![npm version](https://badge.fury.io/js/ajv-cli.svg)](https://www.npmjs.com/package/ajv-cli) -[![Code Climate](https://codeclimate.com/github/epoberezkin/ajv-cli/badges/gpa.svg)](https://codeclimate.com/github/epoberezkin/ajv-cli) -[![Coverage Status](https://coveralls.io/repos/github/jessedc/ajv-cli/badge.svg?branch=master)](https://coveralls.io/github/jessedc/ajv-cli?branch=master) +Command line interface for [ajv](https://github.com/epoberezkin/ajv) that utilizes [any-json](https://github.com/laktak/any-json/) to provide validation against many data formats. +[![Build Status](https://travis-ci.org/json-schema-everywhere/pajv.svg?branch=master)](https://travis-ci.org/json-schema-everywhere/pajv) +[![npm version](https://badge.fury.io/js/pajv.svg)](https://www.npmjs.com/package/pajv) +[![Code Climate](https://codeclimate.com/github/json-schema-everywhere/pajv/badges/gpa.svg)](https://codeclimate.com/github/json-schema-everywhere/pajv) +[![Coverage Status](https://coveralls.io/repos/github/json-schema-everywhere/pajv/badge.svg?branch=master)](https://coveralls.io/github/json-schema-everywhere/pajv?branch=master) ## Contents @@ -14,7 +13,6 @@ Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the - Commands - [Help](#help) - [Validate data](#validate-data) - - [Migrate schema(s) to draft-06](#migrate-schemas-to-draft-06) - [Test validation result](#test-validation-result) - [Ajv options](#ajv-options) - [Version History, License](#version_history) @@ -23,18 +21,16 @@ Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the ## Installation ```sh -npm install -g ajv-cli +npm install -g pajv ``` ## Help ```sh -ajv help -ajv help validate -ajv help compile -ajv help migrate -ajv help test +pajv help +pajv help validate +pajv help test ``` @@ -43,8 +39,8 @@ ajv help test This command validates data files against JSON-schema ```sh -ajv validate -s test/schema.json -d test/valid_data.json -ajv -s test/schema.json -d test/valid_data.json +pajv validate -s test/schema.json -d test/valid_data.json +pajv -s test/schema.json -d test/valid_data.json ``` You can omit `validate` command name and `.json` from the [input file names](https://nodejs.org/api/modules.html#modules_file_modules). @@ -62,7 +58,7 @@ Only one schema can be passed in this parameter Multiple data files can be passed, as in `-r` parameter: ```sh -ajv -s test/schema.json -d "test/valid*.json" +pajv -s test/schema.json -d "test/valid*.json" ``` If some file is invalid exit code will be 1. @@ -103,90 +99,13 @@ For example, you can use `-c ajv-keywords` to add all keywords from [ajv-keyword Possible values are `js` (default), `json` and `line` (see `--errors` option). -## Compile schemas - -This command validates and compiles schema without validating any data. - -It can be used to check that the schema is valid and to create a standalone module exporting validation function (using [ajv-pack](https://github.com/epoberezkin/ajv-pack)). - -```sh -ajv compile -s schema - -# compile to module (BETA) -ajv compile -s schema -o validate.js -``` - -#### Parameters - -##### `-s` - file name(s) of JSON-schema(s) - -Multiple schemas can be passed both by using this parameter mupltiple times and with [glob patterns](https://github.com/isaacs/node-glob#glob-primer). - -```sh -ajv compile -s "test/schema*.json" -``` - - -##### `-o` - output file for compiled validation function module (BETA) - -Only a single schema can be compiled with this option. - -```sh -ajv compile -s "schema.json" -o "validate_schema.js" -``` - -This command also supports parameters `-r`, `-m` and `-c` as in [validate](#validate-data) command. - - -## Migrate schema(s) to draft-06 - -This command validates and migrates schema to draft-06 using [json-schema-migrate](https://github.com/epoberezkin/json-schema-migrate) package. - - -```sh -ajv migrate -s schema - -# compile to specific file name -ajv migrate -s schema -o migrated_schema.json -``` - -#### Parameters - -##### `-s` - file name(s) of JSON-schema(s) - -Multiple schemas can be passed both by using this parameter mupltiple times and with [glob patterns](https://github.com/isaacs/node-glob#glob-primer). - -```sh -ajv migrate -s "test/schema*.json" -``` - -If parameter `-o` is not specified the migrated schema is written to the same file and the original file is preserved with `.bak` extension. - -If migration doesn't change anything in the schema file no changes in files are made. - - -##### `-o` - output file for migrated schema - -Only a single schema can be migrated with this option. - -```sh -ajv compile -s "schema.json" -o migrated_schema.json -``` - -#### Options - -- `v5`: migrate schema as v5 if $schema is not specified -- `--indent=`: indentation in migrated schema JSON file, 4 by default -- `--validate-schema=false`: skip schema validation - - ## Test validation result This command asserts that the result of the validation is as expected. ```sh -ajv test -s test/schema.json -d test/valid_data.json --valid -ajv test -s test/schema.json -d test/invalid_data.json --invalid +pajv test -s test/schema.json -d test/valid_data.json --valid +pajv test -s test/schema.json -d test/invalid_data.json --invalid ``` If the option `--valid` (`--invalid`) is used for the `test` to pass (exit code 0) the data file(s) should be valid (invalid). @@ -196,7 +115,7 @@ This command supports the same options and parameters as [validate](#validate-da ## Ajv options -You can pass the following Ajv options (excluding `migrate` command): +You can pass the following Ajv options: |Option|Description| |---|---| @@ -226,7 +145,7 @@ See [Ajv Options](https://github.com/epoberezkin/ajv#options) for more informati ## Version History -See https://github.com/jessedc/ajv-cli/releases +See https://github.com/json-schema-everywhere/pajv/releases ## Licence diff --git a/commands/compile.js b/commands/compile.js deleted file mode 100644 index 7a1614c..0000000 --- a/commands/compile.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -var util = require('./util'); -var getAjv = require('./ajv'); -var ajvPack = require('ajv-pack'); -var fs = require('fs'); - -module.exports = { - execute: execute, - schema: { - type: 'object', - required: ['s'], - properties: { - s: { $ref: '#/definitions/stringOrArray' }, - r: { $ref: '#/definitions/stringOrArray' }, - m: { $ref: '#/definitions/stringOrArray' }, - c: { $ref: '#/definitions/stringOrArray' }, - o: { type: 'string' } - } - } -}; - - -function execute(argv) { - var ajv = getAjv(argv); - var allValid = true; - - var schemaFiles = util.getFiles(argv.s); - if (argv.o && schemaFiles.length > 1) { - console.error('multiple schemas cannot be compiled to a file'); - return false; - } - schemaFiles.forEach(compileSchema); - - return allValid; - - - function compileSchema(file) { - var schema = util.openFile(file, 'schema ' + file); - var validate; - try { - validate = ajv.compile(schema); - /* istanbul ignore else */ - if (typeof validate == 'function') { - console.log('schema', file, 'is valid'); - if (argv.o) { - try { - var moduleCode = ajvPack(ajv, validate); - try { - fs.writeFileSync(argv.o, moduleCode); - } catch(e) { - console.error('error saving file:', e); - allValid = false; - } - } catch(e) { - console.error('error preparing module:', e); - allValid = false; - } - } - } else { - allValid = false; - console.error('schema', file, 'failed to compile to a function'); - console.error(validate); - } - } catch (err) { - allValid = false; - console.error('schema', file, 'is invalid'); - console.error('error:', err.message); - } - } -} diff --git a/commands/help.js b/commands/help.js index 2511493..9deacc2 100644 --- a/commands/help.js +++ b/commands/help.js @@ -15,8 +15,6 @@ module.exports = { var commands = { validate: helpValidate, - compile: helpCompile, - migrate: helpMigrate, test: helpTest }; @@ -43,27 +41,21 @@ function execute(argv) { function usage() { console.error('\ usage:\n\ - validate: ajv [validate] -s schema[.json] -d data[.json]\n\ - compile: ajv compile -s schema[.json]\n\ - migrate: ajv migrate -s schema[.json] -o migrated_schema.json\n\ - test: ajv test -s schema[.json] -d data[.json] --[in]valid\n\ + validate: pajv [validate] -s schema_file -d data_file\n\ + test: pajv test -s schema_file -d data_file --[in]valid\n\ \n\ - help: ajv help\n\ - ajv help '); + help: pajv help\n\ + pajv help '); } function mainHelp() { _helpValidate(); - _helpCompile(); - _helpMigrate(); _helpTest(); console.log('\ More information:\n\ - ajv help validate\n\ - ajv help compile\n\ - ajv help migrate\n\ - ajv help test'); + pajv help validate\n\ + pajv help test'); } @@ -81,7 +73,6 @@ parameters\n\ glob should be enclosed in double quotes\n\ -c module(s) should export a function that accepts Ajv instance as parameter\n\ (file path should start with ".", otherwise used as require package)\n\ - .json extension can be omitted (but should be used in globs)\n\ \n\ options:\n\ --errors= error reporting format ("js" by deafult)\n\ @@ -98,66 +89,10 @@ options:\n\ function _helpValidate() { console.log('\ Validate data file(s) against schema\n\ - ajv [validate] -s schema[.json] -d data[.json]\n\ - ajv [validate] -s schema[.json] -d "data*.json"\n'); + ajv [validate] -s schema_file -d data_file\n\ + ajv [validate] -s schema_file -d "data*.ext"\n'); } - -function helpCompile() { - _helpCompile(); - console.log('\ -parameters\n\ - -s JSON schema to validate against (required)\n\ - -r referenced schema(s)\n\ - -m meta schema(s)\n\ - -c custom keywords/formats definitions\n\ - -o output file for compiled validation function\n\ -\n\ - -s, -r, -m, -c can be globs and can be used multiple times\n\ - If option -o is used only one schema can be compiled\n\ - glob should be enclosed in double quotes\n\ - -c module(s) should export a function that accepts Ajv instance as parameter\n\ - (file path should start with ".", otherwise used as require package)\n\ - .json extension can be omitted (but should be used in globs)\n'); - helpAjvOptions(); -} - - -function _helpCompile() { - console.log('\ -Compile schema(s)\n\ - ajv compile -s schema[.json]\n\ - ajv compile -s "schema*.json"\n'); -} - - -function helpMigrate() { - _helpMigrate(); - console.log('\ -parameters\n\ - -s JSON schema(s) to migrate to draft-06 (required)\n\ - -o output file for migrated schema (only allowed for a single schema)\n\ -\n\ - -s can be glob and can be used multiple times\n\ - If option -o is used only one schema can be migrated\n\ - glob should be enclosed in double quotes\n\ - .json extension can be omitted (but should be used in globs)\n\ -\n\ -options:\n\ - --v5 migrate schema as v5 if $schema is not specified\n\ - --indent= indentation in migrated schema JSON file, 4 by default\n\ - --validate-schema=false skip schema validation\n'); -} - - -function _helpMigrate() { - console.log('\ -Migrate schema(s) to draft-06\n\ - ajv migrate -s schema[.json] -o migrated_schema.json\n\ - ajv migrate -s "schema*.json"\n'); -} - - function helpTest() { _helpTest(); console.log('\ @@ -173,7 +108,6 @@ parameters\n\ glob should be enclosed in double quotes\n\ -c module(s) should export a function that accepts Ajv instance as parameter\n\ (file path should start with ".", otherwise used as require package)\n\ - .json extension can be omitted (but should be used in globs)\n\ --valid=false can be used instead of --invalid\n\ \n\ options:\n\ @@ -189,9 +123,9 @@ options:\n\ function _helpTest() { console.log('\ Test data validation result\n\ - ajv test -s schema[.json] -d data[.json] --valid\n\ - ajv test -s schema[.json] -d data[.json] --invalid\n\ - ajv test -s schema[.json] -d "data*.json" --valid \n'); + ajv test -s schema_file -d data_file --valid\n\ + ajv test -s schema_file -d data_file --invalid\n\ + ajv test -s schema_file -d "data*.ext" --valid \n'); } diff --git a/commands/index.js b/commands/index.js index 0833875..b24e4e9 100644 --- a/commands/index.js +++ b/commands/index.js @@ -3,7 +3,5 @@ module.exports = { help: require('./help'), validate: require('./validate'), - compile: require('./compile'), - migrate: require('./migrate'), test: require('./test') }; diff --git a/commands/migrate.js b/commands/migrate.js deleted file mode 100644 index c56e505..0000000 --- a/commands/migrate.js +++ /dev/null @@ -1,73 +0,0 @@ -'use strict'; - -var util = require('./util'); -var fs = require('fs'); -var migrate = require('json-schema-migrate'); -var jsonPatch = require('fast-json-patch'); - - -module.exports = { - execute: execute, - schema: { - type: 'object', - required: ['s'], - properties: { - s: { $ref: '#/definitions/stringOrArray' }, - o: { type: 'string' }, - v5: { type: 'boolean' }, - indent: { type: 'integer', minimum: 1 }, - 'validate-schema': { type: 'boolean' } - }, - _ajvOptions: false - } -}; - - -function execute(argv) { - var allValid = true; - var opts = { - v5: argv.v5, - validateSchema: argv['validate-schema'] - }; - - var schemaFiles = util.getFiles(argv.s); - if (argv.o && schemaFiles.length > 1) { - console.error('multiple schemas cannot be migrated to a named output file'); - return false; - } - schemaFiles.forEach(migrateSchema); - - return allValid; - - - function migrateSchema(file) { - var schema = util.openFile(file, 'schema ' + file); - var migratedSchema = JSON.parse(JSON.stringify(schema)); - - try { - migrate.draft6(migratedSchema, opts); - var patch = jsonPatch.compare(schema, migratedSchema); - if (patch.length > 0) { - if (argv.o) { - saveSchema(argv.o, migratedSchema); - } else { - var backupFile = file + '.bak'; - fs.writeFileSync(backupFile, fs.readFileSync(file, 'utf8')); - saveSchema(file, migratedSchema); - } - } else { - console.log('no changes in', file); - } - } catch (err) { - allValid = false; - console.error('schema', file, 'is invalid'); - console.error('error:', err.message); - } - } - - - function saveSchema(file, schema) { - fs.writeFileSync(file, JSON.stringify(schema, null, argv.indent || 4)); - console.log('saved migrated schema to', file); - } -} diff --git a/package.json b/package.json index cd6d60f..229f449 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,8 @@ { - "name": "ajv-cli", - "version": "2.1.0", - "description": "A command line interface for epoberezkin/ajv JSON schema validator", + "name": "pajv", + "displayName": "Polyglottal JSON Schema Validator", + "version": "1.0.0", + "description": " A command line JSON Schema validator that supports many file formats. Fork of jessedc/ajv-cli.", "scripts": { "eslint": "eslint index.js commands/*.js test/*.js test/**/*.js", "test-spec": "mocha test/*.spec.js -R spec", @@ -20,22 +21,27 @@ "cache": true }, "bin": { - "ajv": "index.js" + "pajv": "index.js" }, "preferGlobal": true, - "author": "Jesse Collis ", + "author": "Adam Voss", + "contributors": [ + { + "name": "Jesse Collis", + "email": "jesse@jcmultimedia.com.au" + } + ], "license": "MIT", "repository": { "type": "git", - "url": "https://github.com/jessedc/ajv-cli" + "url": "https://github.com/json-schema-everywhere/pajv" }, "dependencies": { "ajv": "^5.0.0", - "ajv-pack": "^0.3.0", "fast-json-patch": "^0.5.6", "glob": "^7.0.3", - "json-schema-migrate": "^0.2.0", - "minimist": "^1.2.0" + "minimist": "^1.2.0", + "util.promisify": "^1.0.0" }, "devDependencies": { "ajv-keywords": "^2.0.0", diff --git a/test/compile.spec.js b/test/compile.spec.js deleted file mode 100644 index b8ae031..0000000 --- a/test/compile.spec.js +++ /dev/null @@ -1,163 +0,0 @@ -'use strict'; - -var cli = require('./cli'); -var assert = require('assert'); -var fs = require('fs'); - - -describe('compile', function() { - this.timeout(10000); - - it('should compile valid schema', function (done) { - cli('compile -s test/schema', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - done(); - }); - }); - - it('should compile multiple schemas', function (done) { - cli('compile -s test/schema -s test/meta/schema -m test/meta/meta_schema', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 2); - assert.equal(stderr, ''); - done(); - }); - }); - - it('should compile schema to output file', function (done) { - cli('compile -s test/schema -o test/validate_schema.js', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - - var validate = require('./validate_schema.js'); - var validData = require('./valid_data.json'); - var invalidData = require('./invalid_data.json'); - assert.strictEqual(validate(validData), true); - assert.strictEqual(validate(invalidData), false); - - fs.unlinkSync('test/validate_schema.js'); - done(); - }); - }); - - it('should compile valid schema with a custom meta-schema', function (done) { - cli('compile -s test/meta/schema -m test/meta/meta_schema', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - done(); - }); - }); - - it('should compile schema with custom keyword', function(done) { - cli('compile -s test/custom/schema -c ./test/custom/typeof.js -o test/custom/validate_schema.js', function (error, stdout, stderr) { - assertCompiledCustom(error, stdout, stderr); - done(); - }); - }); - - it('should compile schema with custom keyword from npm package', function(done) { - cli('compile -s test/custom/schema -c ajv-keywords/keywords/typeof -o test/custom/validate_schema.js', function (error, stdout, stderr) { - assertCompiledCustom(error, stdout, stderr); - done(); - }); - }); - - it('should support draft-04 meta-schema', function(done) { - cli('compile -s test/migrate/schema', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - done(); - }); - }); - - it('should fail to compile invalid schema with a custom meta-schema', function (done) { - cli('compile -s test/meta/invalid_schema -m test/meta/meta_schema', function (error, stdout, stderr) { - assert(error instanceof Error); - assert.equal(stdout, ''); - var lines = assertError(stderr); - assert(/my_keyword\sshould\sbe\sboolean/.test(lines[1])); - done(); - }); - }); - - it('should fail to compile multiple schemas to output file', function (done) { - cli('compile -s test/schema -s test/meta/schema -m test/meta/meta_schema -o test/validate_schema.js', function (error, stdout, stderr) { - assert(error instanceof Error); - assert.equal(stdout, ''); - var lines = stderr.split('\n'); - assert.equal(lines.length, 2); - assert(/multiple\sschemas/.test(lines[0])); - done(); - }); - }); - - it('should fail to save compiled schemas when path does not exist', function (done) { - cli('compile -s test/schema -o no_folder/validate_schema.js', function (error, stdout, stderr) { - assert(error instanceof Error); - assertValid(stdout, 1); - var lines = stderr.split('\n'); - assert(lines.length > 1); - assert(/error\ssaving\sfile/.test(lines[0])); - done(); - }); - }); - - it('should fail to compile if referenced schema is invalid', function (done) { - cli('compile -s test/schema -r test/meta/invalid_schema2', function (error, stdout, stderr) { - assert(error instanceof Error); - assert.equal(stdout, ''); - var lines = assertError(stderr); - assert(/schema\sis\sinvalid/.test(lines[1])); - done(); - }); - }); - - it('should fail to compile if custom package does not export function', function (done) { - cli('compile -s test/custom/schema -c ./test/custom/invalid_custom.js', function (error, stdout, stderr) { - assert(error instanceof Error); - assert.equal(stdout, ''); - var lines = stderr.split('\n'); - assert(/module.*is\sinvalid/.test(lines[0])); - assert(/not\sa\sfunction/.test(lines[1])); - done(); - }); - }); -}); - - -function assertValid(stdout, count) { - var lines = stdout.split('\n'); - assert.equal(lines.length, count + 1); - for (var i=0; i