From 5a6342066e37b1c967873fb2fd4798853c4e3b56 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Thu, 23 Mar 2017 22:43:02 +0000 Subject: [PATCH 01/13] feat: use draft-06, remove v5 option and tests --- commands/ajv.js | 2 +- commands/help.js | 2 - commands/options.js | 1 - package.json | 4 +- test/compile.spec.js | 9 ---- test/custom/schema.json | 2 +- test/meta/meta_schema.json | 84 +++++++++++++++++++------------------- test/schema.json | 7 ++-- test/v5/invalid_data.json | 3 -- test/v5/schema.json | 7 ---- test/v5/valid_data.json | 4 -- test/validate.spec.js | 26 ------------ 12 files changed, 49 insertions(+), 102 deletions(-) delete mode 100644 test/v5/invalid_data.json delete mode 100644 test/v5/schema.json delete mode 100644 test/v5/valid_data.json diff --git a/commands/ajv.js b/commands/ajv.js index aeb617a..51287e1 100644 --- a/commands/ajv.js +++ b/commands/ajv.js @@ -9,7 +9,7 @@ var path = require('path'); module.exports = function (argv) { var opts = options.get(argv); if (argv.o) opts.sourceCode = true; - var ajv = Ajv(opts); + var ajv = new Ajv(opts); var invalid; addSchemas(argv.m, 'addMetaSchema', 'meta-schema'); addSchemas(argv.r, 'addSchema', 'schema'); diff --git a/commands/help.js b/commands/help.js index 6749293..d4cb725 100644 --- a/commands/help.js +++ b/commands/help.js @@ -167,8 +167,6 @@ Test data validation result\n\ function helpAjvOptions() { console.log('\ Ajv options (see https://github.com/epoberezkin/ajv#options):\n\ - --v5 support validation keywords from v5 proposals\n\ -\n\ --all-errors collect all errors\n\ \n\ --json-pointers report data paths as JSON pointers\n\ diff --git a/commands/options.js b/commands/options.js index 2684d12..9e265dd 100644 --- a/commands/options.js +++ b/commands/options.js @@ -12,7 +12,6 @@ var ajv = Ajv({ }); var AJV_OPTIONS = { - 'v5': { type: 'boolean' }, 'all-errors': { type: 'boolean' }, 'verbose': { type: 'boolean' }, 'json-pointers': { type: 'boolean' }, diff --git a/package.json b/package.json index a8c5980..6d5abc5 100644 --- a/package.json +++ b/package.json @@ -30,14 +30,14 @@ "url": "https://github.com/jessedc/ajv-cli" }, "dependencies": { - "ajv": "^4.7.4", + "ajv": "^5.0.4-beta.1", "ajv-pack": "^0.2.3", "fast-json-patch": "^0.5.6", "glob": "^7.0.3", "minimist": "^1.2.0" }, "devDependencies": { - "ajv-keywords": "^1.1.1", + "ajv-keywords": "^2.0.1-beta.2", "coveralls": "^2.11.8", "eslint": "^2.4.0", "mocha": "^2.4.5", diff --git a/test/compile.spec.js b/test/compile.spec.js index 38ed936..2723393 100644 --- a/test/compile.spec.js +++ b/test/compile.spec.js @@ -43,15 +43,6 @@ describe('compile', function() { }); }); - it('should compile valid v5 schema', function (done) { - cli('compile -s test/v5/schema --v5', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - 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); diff --git a/test/custom/schema.json b/test/custom/schema.json index 7bee435..5cfea1e 100644 --- a/test/custom/schema.json +++ b/test/custom/schema.json @@ -1,5 +1,5 @@ { - "$schema": "http://json-schema.org/draft-04/schema#", + "$schema": "http://json-schema.org/draft-06/schema#", "id": "schema.json", "typeof": "number" } diff --git a/test/meta/meta_schema.json b/test/meta/meta_schema.json index fecfc8b..b6ef85e 100644 --- a/test/meta/meta_schema.json +++ b/test/meta/meta_schema.json @@ -1,40 +1,55 @@ { - "id": "http://example.com/my_meta_schema#", - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "Core schema meta-schema", + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "http://example.com/my_meta_schema#", + "title": "Core schema meta-schema", "definitions": { "schemaArray": { "type": "array", "minItems": 1, "items": { "$ref": "#" } }, - "positiveInteger": { + "nonNegativeInteger": { "type": "integer", "minimum": 0 }, - "positiveIntegerDefault0": { - "allOf": [ { "$ref": "#/definitions/positiveInteger" }, { "default": 0 } ] + "nonNegativeIntegerDefault0": { + "allOf": [ + { "$ref": "#/definitions/nonNegativeInteger" }, + { "default": 0 } + ] }, "simpleTypes": { - "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ] + "enum": [ + "array", + "boolean", + "integer", + "null", + "number", + "object", + "string" + ] }, "stringArray": { "type": "array", "items": { "type": "string" }, - "minItems": 1, - "uniqueItems": true + "uniqueItems": true, + "default": [] } }, - "type": "object", + "type": ["object", "boolean"], "properties": { - "id": { + "$id": { "type": "string", - "format": "uri" + "format": "uri-reference" }, "$schema": { "type": "string", "format": "uri" }, + "$ref": { + "type": "string", + "format": "uri-reference" + }, "title": { "type": "string" }, @@ -44,36 +59,27 @@ "default": {}, "multipleOf": { "type": "number", - "minimum": 0, - "exclusiveMinimum": true + "exclusiveMinimum": 0 }, "maximum": { "type": "number" }, "exclusiveMaximum": { - "type": "boolean", - "default": false + "type": "number" }, "minimum": { "type": "number" }, "exclusiveMinimum": { - "type": "boolean", - "default": false + "type": "number" }, - "maxLength": { "$ref": "#/definitions/positiveInteger" }, - "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "maxLength": { "$ref": "#/definitions/nonNegativeInteger" }, + "minLength": { "$ref": "#/definitions/nonNegativeIntegerDefault0" }, "pattern": { "type": "string", "format": "regex" }, - "additionalItems": { - "anyOf": [ - { "type": "boolean" }, - { "$ref": "#" } - ], - "default": {} - }, + "additionalItems": { "$ref": "#" }, "items": { "anyOf": [ { "$ref": "#" }, @@ -81,22 +87,17 @@ ], "default": {} }, - "maxItems": { "$ref": "#/definitions/positiveInteger" }, - "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "maxItems": { "$ref": "#/definitions/nonNegativeInteger" }, + "minItems": { "$ref": "#/definitions/nonNegativeIntegerDefault0" }, "uniqueItems": { "type": "boolean", "default": false }, - "maxProperties": { "$ref": "#/definitions/positiveInteger" }, - "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" }, + "contains": { "$ref": "#" }, + "maxProperties": { "$ref": "#/definitions/nonNegativeInteger" }, + "minProperties": { "$ref": "#/definitions/nonNegativeIntegerDefault0" }, "required": { "$ref": "#/definitions/stringArray" }, - "additionalProperties": { - "anyOf": [ - { "type": "boolean" }, - { "$ref": "#" } - ], - "default": {} - }, + "additionalProperties": { "$ref": "#" }, "definitions": { "type": "object", "additionalProperties": { "$ref": "#" }, @@ -121,6 +122,8 @@ ] } }, + "propertyNames": { "$ref": "#" }, + "const": {}, "enum": { "type": "array", "minItems": 1, @@ -137,15 +140,12 @@ } ] }, + "format": { "type": "string" }, "allOf": { "$ref": "#/definitions/schemaArray" }, "anyOf": { "$ref": "#/definitions/schemaArray" }, "oneOf": { "$ref": "#/definitions/schemaArray" }, "not": { "$ref": "#" }, "my_keyword": { "type": "boolean" } }, - "dependencies": { - "exclusiveMaximum": [ "maximum" ], - "exclusiveMinimum": [ "minimum" ] - }, "default": {} } diff --git a/test/schema.json b/test/schema.json index 3f51cb4..d1db81d 100644 --- a/test/schema.json +++ b/test/schema.json @@ -1,6 +1,6 @@ { - "$schema": "http://json-schema.org/draft-04/schema#", - "id": "schema.json", + "$schema": "http://json-schema.org/draft-06/schema#", + "$id": "schema.json", "description": "basic schema from z-schema benchmark (https://github.com/zaggino/z-schema)", "title": "Product set", "type": "array", @@ -18,8 +18,7 @@ }, "price": { "type": "number", - "minimum": 0, - "exclusiveMinimum": true + "exclusiveMinimum": 0 }, "tags": { "type": "array", diff --git a/test/v5/invalid_data.json b/test/v5/invalid_data.json deleted file mode 100644 index 050607f..0000000 --- a/test/v5/invalid_data.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "1": 1 -} \ No newline at end of file diff --git a/test/v5/schema.json b/test/v5/schema.json deleted file mode 100644 index e661333..0000000 --- a/test/v5/schema.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "patternProperties": { - "^[0-9]+$": { "constant": { "$data": "0#" } }, - "^[a-z]+$": { "constant": { "$data": "0#" } } - }, - "patternRequired": ["^[0-9]+$", "^[a-z]+$"] -} diff --git a/test/v5/valid_data.json b/test/v5/valid_data.json deleted file mode 100644 index 19aaaf8..0000000 --- a/test/v5/valid_data.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "1": "1", - "a": "a" -} \ No newline at end of file diff --git a/test/validate.spec.js b/test/validate.spec.js index 435f066..0655dac 100644 --- a/test/validate.spec.js +++ b/test/validate.spec.js @@ -112,32 +112,6 @@ describe('validate', function() { }); - describe('validate v5 schema', function() { - it('should validate valid data', function (done) { - cli('-s test/v5/schema -d test/v5/valid_data --v5', function (error, stdout, stderr) { - assert.strictEqual(error, null); - assertValid(stdout, 1); - assert.equal(stderr, ''); - done(); - }); - }); - - it('should validate invalid data', function (done) { - cli('-s test/v5/schema.json -d test/v5/invalid_data.json --v5 --errors=line', function (error, stdout, stderr) { - assert(error instanceof Error); - assert.equal(stdout, ''); - var results = assertErrors(stderr); - var errors = results[0]; - var err = errors[0]; - assert.equal(err.keyword, 'constant'); - assert.equal(err.dataPath, "['1']"); - assert.equal(err.schemaPath, '#/patternProperties/%5E%5B0-9%5D%2B%24/constant'); - done(); - }); - }); - }); - - describe('validate with schema using added meta-schema', function() { it('should validate valid data', function (done) { cli('-s test/meta/schema -d test/meta/valid_data -m test/meta/meta_schema', function (error, stdout, stderr) { From cce95e6dfc475cb3d51bd2a2e6c0131ae950fdde Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Fri, 24 Mar 2017 21:24:37 +0000 Subject: [PATCH 02/13] feat: options supported by Ajv 5.0 --- commands/help.js | 15 +++++++++++++++ commands/options.js | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/commands/help.js b/commands/help.js index d4cb725..b2134e1 100644 --- a/commands/help.js +++ b/commands/help.js @@ -167,7 +167,13 @@ Test data validation result\n\ function helpAjvOptions() { console.log('\ Ajv options (see https://github.com/epoberezkin/ajv#options):\n\ + --data use $data references\n\ +\n\ --all-errors collect all errors\n\ +\n\ + --unknown-formats= handling of unknown formats\n\ + true throw exception during schema compilation (default)\n\ + allowed unknown format name, multiple names can be used\n\ \n\ --json-pointers report data paths as JSON pointers\n\ \n\ @@ -178,6 +184,15 @@ Ajv options (see https://github.com/epoberezkin/ajv#options):\n\ --format= format validation mode\n\ fast using regex (default)\n\ full using functions\n\ +\n\ + --schema-id= (by default both IDs will be used)\n\ + $id use $id\n\ + id use id\n\ +\n\ + --extend-refs= validation of other keywords when $ref is present in the schema\n\ + ignore ignore other keywords (default)\n\ + fail throw exception (recommended)\n\ + true validate all keywords\n\ \n\ --missing-refs= handling missing referenced schemas\n\ true fail schema compilation (default)\n\ diff --git a/commands/options.js b/commands/options.js index 9e265dd..f30d833 100644 --- a/commands/options.js +++ b/commands/options.js @@ -4,7 +4,7 @@ var Ajv = require('ajv'); var glob = require('glob'); var ajv = Ajv({ allErrors: true, - coerceTypes: true, + coerceTypes: 'array', jsonPointers: true, formats: { notGlob: function(s) { return !glob.hasMagic(s); } @@ -12,6 +12,7 @@ var ajv = Ajv({ }); var AJV_OPTIONS = { + 'data': { type: 'boolean' }, 'all-errors': { type: 'boolean' }, 'verbose': { type: 'boolean' }, 'json-pointers': { type: 'boolean' }, @@ -21,6 +22,16 @@ var AJV_OPTIONS = { { type: 'boolean' }, { enum: ['fast', 'full'] } ] }, + 'unknown-formats': { anyOf: [ + { type: 'boolean' }, + { const: 'ignore' }, + { type: 'array', items: { type: 'string' } } + ] }, + 'schema-id': { enum: ['$id', 'id'] }, + 'extend-refs': { anyOf: [ + { type: 'boolean' }, + { enum: ['ignore', 'fail'] } + ] }, 'missing-refs': { anyOf: [ { type: 'boolean' }, { enum: ['ignore', 'fail'] } From 55558b8f9cf5d2de44676a3769f71b5a984a0740 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Fri, 24 Mar 2017 21:29:30 +0000 Subject: [PATCH 03/13] docs: options supported by Ajv v5.0.x --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8400b63..dab1e2f 100644 --- a/README.md +++ b/README.md @@ -144,13 +144,16 @@ You can pass the following Ajv options: |Option|Description| |---|---| -|`--v5`|support v5 proposals| +|`--data`|use [$data references](https://github.com/epoberezkin/ajv#data-reference)| |`--all-errors`|collect all errors| +|`--unknown-formats=`|handling of unknown formats| |`--verbose`|include schema and data in errors| |`--json-pointers`|report data paths in errors using JSON-pointers| |`--unique-items=false`|do not validate uniqueItems keyword| |`--unicode=false`|count unicode pairs as 2 characters| |`--format=full`|format mode| +|`--schema-id=`|keyword(s) to use as schema ID| +|`--extend-refs=`|validation of other keywords when $ref is present in the schema| |`--missing-refs=`|handle missing referenced schemas (true/ignore/fail)| |`--inline-refs=`|referenced schemas compilation mode (true/false/\)| |`--remove-additional`|remove additional properties (true/all/failing)| From b3986a8f681c97e8205cde04ce39c1b486022623 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Fri, 24 Mar 2017 21:31:54 +0000 Subject: [PATCH 04/13] test: typo in description --- test/validate.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/validate.spec.js b/test/validate.spec.js index 0655dac..cb72419 100644 --- a/test/validate.spec.js +++ b/test/validate.spec.js @@ -153,7 +153,7 @@ describe('validate', function() { describe('option "changes"', function() { - it('should log changes ni the object after validation', function (done) { + it('should log changes in the object after validation', function (done) { cli('-s test/schema -d test/data_with_additional --remove-additional --changes=line', function (error, stdout, stderr) { assert.strictEqual(error, null); var lines = assertValid(stdout, 1, 2); From 74c61bcbfb42c1e47026ca264dc14503b32f56eb Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 21:21:42 +0000 Subject: [PATCH 05/13] feat: migrate schema to draft-06 --- commands/index.js | 1 + commands/migrate.js | 73 ++++++++++++++ commands/options.js | 2 +- package.json | 1 + test/.eslintrc.yml | 1 + 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 + 10 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 commands/migrate.js create mode 100644 test/migrate.spec.js create mode 100644 test/migrate/expected_migrated_schema.json create mode 100644 test/migrate/schema.json create mode 100644 test/migrate/schema_invalid.json create mode 100644 test/migrate/schema_no_changes.json diff --git a/commands/index.js b/commands/index.js index c292850..c41e832 100644 --- a/commands/index.js +++ b/commands/index.js @@ -2,6 +2,7 @@ module.exports = { help: require('./help'), + migrate: require('./migrate'), validate: require('./validate'), compile: require('./compile'), test: require('./test') diff --git a/commands/migrate.js b/commands/migrate.js new file mode 100644 index 0000000..c56e505 --- /dev/null +++ b/commands/migrate.js @@ -0,0 +1,73 @@ +'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/commands/options.js b/commands/options.js index f30d833..dfa4d46 100644 --- a/commands/options.js +++ b/commands/options.js @@ -2,7 +2,7 @@ var Ajv = require('ajv'); var glob = require('glob'); -var ajv = Ajv({ +var ajv = new Ajv({ allErrors: true, coerceTypes: 'array', jsonPointers: true, diff --git a/package.json b/package.json index 6d5abc5..14c39cc 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "ajv-pack": "^0.2.3", "fast-json-patch": "^0.5.6", "glob": "^7.0.3", + "json-schema-migrate": "^0.1.0", "minimist": "^1.2.0" }, "devDependencies": { diff --git a/test/.eslintrc.yml b/test/.eslintrc.yml index 1231df2..bd27b67 100644 --- a/test/.eslintrc.yml +++ b/test/.eslintrc.yml @@ -2,6 +2,7 @@ rules: indent: [2, 2, { SwitchCase : 1 } ] no-invalid-this: 0 + no-empty: [2, allowEmptyCatch: true] globals: describe: false diff --git a/test/migrate.spec.js b/test/migrate.spec.js new file mode 100644 index 0000000..8749ec1 --- /dev/null +++ b/test/migrate.spec.js @@ -0,0 +1,109 @@ +'use strict'; + +var cli = require('./cli'); +var assert = require('assert'); +var fs = require('fs'); +var path = require('path'); + + +describe('migrate', function() { + this.timeout(10000); + + it('should migrate schema to draft-06', function (done) { + try { deleteSchema('migrated_schema.json'); } catch(e) {} + + cli('migrate -s test/migrate/schema.json -o test/migrate/migrated_schema.json', function (error, stdout, stderr) { + try { + assert.strictEqual(error, null); + assertMigrated(stdout, 1); + assert.equal(stderr, ''); + var migratedSchema = readSchema('migrated_schema.json'); + var expectedMigratedSchema = require('./migrate/expected_migrated_schema.json'); + assert.deepStrictEqual(migratedSchema, expectedMigratedSchema); + } finally { + deleteSchema('migrated_schema.json'); + } + done(); + }); + }); + + it('should migrate schema to draft-06 to the same file and create backup', function (done) { + var backup = fs.readFileSync(path.join(__dirname, 'migrate', 'schema.json'), 'utf8'); + + cli('migrate -s test/migrate/schema.json', function (error, stdout, stderr) { + try { + assert.strictEqual(error, null); + assertMigrated(stdout, 1); + assert.equal(stderr, ''); + var backupSchema = readSchema('schema.json.bak'); + assert.deepStrictEqual(backupSchema, JSON.parse(backup)); + + var migratedSchema = readSchema('schema.json'); + var expectedMigratedSchema = require('./migrate/expected_migrated_schema.json'); + assert.deepStrictEqual(migratedSchema, expectedMigratedSchema); + } finally { + fs.writeFileSync(path.join(__dirname, 'migrate', 'schema.json'), backup); + deleteSchema('schema.json.bak'); + } + done(); + }); + }); + + it('should not save schema id schema is draft-06 compatible', function (done) { + cli('migrate -s test/migrate/schema_no_changes.json -o test/migrate/migrated_schema.json', function (error, stdout, stderr) { + assert.strictEqual(error, null); + assert.equal(stderr, ''); + var lines = stdout.split('\n'); + assert.equal(lines.length, 2); + assert(/no\schanges/.test(lines[0])); + var err; + try { readSchema('migrated_schema.json'); } + catch(e) { err = e; } + assert(err instanceof Error); + done(); + }); + }); + + it('should fail on invalid schema', function (done) { + cli('migrate -s test/migrate/schema_invalid.json', function (error, stdout, stderr) { + assert(error instanceof Error); + assert.equal(stdout, ''); + assertError(stderr); + done(); + }); + }); + + it('should fail if multiple schemas passed with -o option', function (done) { + cli('migrate -s "test/migrate/schema*.json" -o test/migrate/migrated_schema.json', function (error, stdout, stderr) { + assert(error instanceof Error); + assert.equal(stdout, ''); + assert(/multiple\sschemas/.test(stderr)); + done(); + }); + }); +}); + + +function assertMigrated(stdout, count) { + var lines = stdout.split('\n'); + assert.equal(lines.length, count + 1); + for (var i=0; i Date: Sat, 25 Mar 2017 21:34:30 +0000 Subject: [PATCH 06/13] feat: help for "migrate" command --- commands/help.js | 33 ++++++++++++++++++++++++++++++++- commands/index.js | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/commands/help.js b/commands/help.js index b2134e1..2511493 100644 --- a/commands/help.js +++ b/commands/help.js @@ -16,6 +16,7 @@ module.exports = { var commands = { validate: helpValidate, compile: helpCompile, + migrate: helpMigrate, test: helpTest }; @@ -44,6 +45,7 @@ function usage() { 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\ \n\ help: ajv help\n\ @@ -54,11 +56,13 @@ usage:\n\ 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'); } @@ -110,7 +114,7 @@ parameters\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 (-c option)\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\ @@ -127,6 +131,33 @@ Compile schema(s)\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('\ diff --git a/commands/index.js b/commands/index.js index c41e832..0833875 100644 --- a/commands/index.js +++ b/commands/index.js @@ -2,8 +2,8 @@ module.exports = { help: require('./help'), - migrate: require('./migrate'), validate: require('./validate'), compile: require('./compile'), + migrate: require('./migrate'), test: require('./test') }; From 4d81252ca31a147d5a9317655586e18c4c4e685a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 21:39:13 +0000 Subject: [PATCH 07/13] test: help for "migrate", help for unknown command --- test/help.spec.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/test/help.spec.js b/test/help.spec.js index ff65d83..43482cc 100644 --- a/test/help.spec.js +++ b/test/help.spec.js @@ -37,6 +37,16 @@ describe('help', function() { }); }); + it('should print help for migrate', function (done) { + cli('help migrate', function (error, stdout, stderr) { + assert.strictEqual(error, null); + assert(/Migrate/.test(stdout)); + assert(/options/.test(stdout)); + assert.equal(stderr, ''); + done(); + }); + }); + it('should print help for test', function (done) { cli('help test', function (error, stdout, stderr) { assert.strictEqual(error, null); @@ -47,7 +57,18 @@ describe('help', function() { }); }); - it('should print usage if command in unknown', function (done) { + it('should print usage if unknown command is used', function (done) { + cli('unknown', function (error, stdout, stderr) { + assert(error instanceof Error); + assert.equal(stdout, ''); + assert(/command/.test(stderr)); + assert(/unknown/.test(stderr)); + assert(/usage/.test(stderr)); + done(); + }); + }); + + it('should print usage if help command is unknown', function (done) { cli('help unknown', function (error, stdout, stderr) { assert(error instanceof Error); assert.equal(stdout, ''); From 9e643d85052b5299c7c094b0f954ec989ce0b02c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 21:50:11 +0000 Subject: [PATCH 08/13] docs: "migrate" command --- README.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/README.md b/README.md index dab1e2f..3c22330 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,14 @@ Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the npm install -g ajv-cli ``` + ## Help ```sh ajv help ajv help validate ajv help compile +ajv help migrate ajv help test ``` @@ -124,6 +126,48 @@ 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. From e6e552483441c6fbec51217f2d7d9a881920c2dc Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 22:02:02 +0000 Subject: [PATCH 09/13] doc: TOC --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3c22330..f2f546f 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,18 @@ Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the [![Coverage Status](https://coveralls.io/repos/github/jessedc/ajv-cli/badge.svg?branch=master)](https://coveralls.io/github/jessedc/ajv-cli?branch=master) +## Contents + +- [Installation](#installation) +- 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) + + ## Installation ```sh @@ -184,7 +196,7 @@ This command supports the same options and parameters as [validate](#validate-da ## Ajv options -You can pass the following Ajv options: +You can pass the following Ajv options (excluding `migrate` command): |Option|Description| |---|---| From dc2ffcc4df4d05a588533dca4c33faf0cfa65ebd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 22:03:09 +0000 Subject: [PATCH 10/13] test: node versions in travis --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 850a09f..7877440 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,10 +2,9 @@ language: node_js before_script: - npm install -g codeclimate-test-reporter node_js: - - "0.10" - - "0.12" - "4" - - "5" + - "6" + - "7" after_script: - codeclimate-test-reporter < coverage/lcov.info - coveralls < coverage/lcov.info From 093df034b3031e8bc16afd50ef6983b0075be67c Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 25 Mar 2017 22:04:59 +0000 Subject: [PATCH 11/13] version 2.0.0-beta.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 14c39cc..88ce04f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv-cli", - "version": "1.1.1", + "version": "2.0.0-beta.0", "description": "A command line interface for epoberezkin/ajv JSON schema validator", "scripts": { "eslint": "eslint index.js commands/*.js test/*.js test/**/*.js", From e8a161792e6bc441d5b3f8944776d64ca9880481 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 17 Apr 2017 18:26:25 +0100 Subject: [PATCH 12/13] update for Ajv version 5.0.0 --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 88ce04f..3014cce 100644 --- a/package.json +++ b/package.json @@ -30,15 +30,15 @@ "url": "https://github.com/jessedc/ajv-cli" }, "dependencies": { - "ajv": "^5.0.4-beta.1", - "ajv-pack": "^0.2.3", + "ajv": "^5.0.0", + "ajv-pack": "^0.3.0", "fast-json-patch": "^0.5.6", "glob": "^7.0.3", - "json-schema-migrate": "^0.1.0", + "json-schema-migrate": "^0.2.0", "minimist": "^1.2.0" }, "devDependencies": { - "ajv-keywords": "^2.0.1-beta.2", + "ajv-keywords": "^2.0.0", "coveralls": "^2.11.8", "eslint": "^2.4.0", "mocha": "^2.4.5", From 147b7e52ef25a8ac704726091f2d1cc353659716 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 17 Apr 2017 18:27:20 +0100 Subject: [PATCH 13/13] version 2.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3014cce..9674464 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv-cli", - "version": "2.0.0-beta.0", + "version": "2.0.0", "description": "A command line interface for epoberezkin/ajv JSON schema validator", "scripts": { "eslint": "eslint index.js commands/*.js test/*.js test/**/*.js",