Skip to content

Commit

Permalink
Merge pull request #27 from jessedc/v2.0.0
Browse files Browse the repository at this point in the history
version 2.0.0
  • Loading branch information
Evgeny Poberezkin authored Apr 17, 2017
2 parents 236b0f0 + 147b7e5 commit 4185a71
Show file tree
Hide file tree
Showing 23 changed files with 427 additions and 113 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
63 changes: 61 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,32 @@ 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
npm install -g ajv-cli
```


## Help

```sh
ajv help
ajv help validate
ajv help compile
ajv help migrate
ajv help test
```

Expand Down Expand Up @@ -124,6 +138,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.
Expand All @@ -140,17 +196,20 @@ 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|
|---|---|
|`--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/\<number\>)|
|`--remove-additional`|remove additional properties (true/all/failing)|
Expand Down
2 changes: 1 addition & 1 deletion commands/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
48 changes: 46 additions & 2 deletions commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
var commands = {
validate: helpValidate,
compile: helpCompile,
migrate: helpMigrate,
test: helpTest
};

Expand Down Expand Up @@ -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\
Expand All @@ -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');
}

Expand Down Expand Up @@ -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\
Expand All @@ -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=<N> 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('\
Expand Down Expand Up @@ -167,9 +198,13 @@ 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\
--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\
<string> allowed unknown format name, multiple names can be used\n\
\n\
--json-pointers report data paths as JSON pointers\n\
\n\
Expand All @@ -180,6 +215,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\
Expand Down
1 change: 1 addition & 0 deletions commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
help: require('./help'),
validate: require('./validate'),
compile: require('./compile'),
migrate: require('./migrate'),
test: require('./test')
};
73 changes: 73 additions & 0 deletions commands/migrate.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 13 additions & 3 deletions commands/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

var Ajv = require('ajv');
var glob = require('glob');
var ajv = Ajv({
var ajv = new Ajv({
allErrors: true,
coerceTypes: true,
coerceTypes: 'array',
jsonPointers: true,
formats: {
notGlob: function(s) { return !glob.hasMagic(s); }
}
});

var AJV_OPTIONS = {
'v5': { type: 'boolean' },
'data': { type: 'boolean' },
'all-errors': { type: 'boolean' },
'verbose': { type: 'boolean' },
'json-pointers': { type: 'boolean' },
Expand All @@ -22,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'] }
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv-cli",
"version": "1.1.2",
"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",
Expand Down Expand Up @@ -30,14 +30,15 @@
"url": "https://github.com/jessedc/ajv-cli"
},
"dependencies": {
"ajv": "^4.7.4",
"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.2.0",
"minimist": "^1.2.0"
},
"devDependencies": {
"ajv-keywords": "^1.1.1",
"ajv-keywords": "^2.0.0",
"coveralls": "^2.11.8",
"eslint": "^2.4.0",
"mocha": "^2.4.5",
Expand Down
1 change: 1 addition & 0 deletions test/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
rules:
indent: [2, 2, { SwitchCase : 1 } ]
no-invalid-this: 0
no-empty: [2, allowEmptyCatch: true]

globals:
describe: false
Expand Down
Loading

0 comments on commit 4185a71

Please sign in to comment.