Skip to content

Commit

Permalink
Merge a23edcb into d9de674
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed May 9, 2020
2 parents d9de674 + a23edcb commit 8ee220f
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ajv-cli

Command line interface for [ajv](https://github.com/epoberezkin/ajv), one of the [fastest json schema validators](https://github.com/ebdrup/json-schema-benchmark).
Supports [JSON](http://json.org/), [JSON5](http://json5.org/), and [YAML](http://yaml.org/).

[![Build Status](https://travis-ci.org/jessedc/ajv-cli.svg?branch=master)](https://travis-ci.org/jessedc/ajv-cli)
[![npm](https://img.shields.io/npm/v/ajv-cli.svg)](https://www.npmjs.com/package/ajv-cli)
Expand Down
25 changes: 24 additions & 1 deletion commands/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
var glob = require('glob');
var path = require('path');
var fs = require('fs');
var yaml = require('js-yaml');
var JSON5 = require('json5');


module.exports = {
Expand Down Expand Up @@ -30,12 +32,33 @@ function getFiles(args) {
}


function getFormatFromFileName(filename) {
return path.extname(filename).substr(1).toLowerCase();
}


function decodeFile(contents, format) {
switch(format) {
case 'json':
return JSON.parse(contents);
case 'json5':
return JSON5.parse(contents);
case 'yml':
case 'yaml':
return yaml.safeLoad(contents);
default:
throw new Error('unsupported format ' + format);
}
}


function openFile(filename, suffix){
var json = null;
var file = path.resolve(process.cwd(), filename);
try {
try {
json = JSON.parse(fs.readFileSync(file).toString());
var format = getFormatFromFileName(filename);
json = decodeFile(fs.readFileSync(file).toString(), format);
} catch(JSONerr) {
json = require(file);
}
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
"ajv-pack": "^0.3.0",
"fast-json-patch": "^2.0.0",
"glob": "^7.1.0",
"js-yaml": "^3.13.1",
"json-schema-migrate": "^0.2.0",
"json5": "^2.1.3",
"minimist": "^1.2.0"
},
"devDependencies": {
Expand Down
32 changes: 32 additions & 0 deletions test/valid_data.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
// todo: we're missing item 1
{
id: 2,
name: 'An ice sculpture',
price: 12.50,
tags: ['cold', 'ice'],
dimensions: {
length: 7.0,
width: 12.0,
height: 9.5
},
warehouseLocation: {
latitude: -78.75,
longitude: 20.4
}
},
{
id: 3,
name: 'A blue mouse',
price: 25.50,
dimensions: {
length: 3.1,
width: 1.0,
height: 1.0
},
warehouseLocation: {
latitude: 54.4,
longitude: -32.7
}
}
]
23 changes: 23 additions & 0 deletions test/valid_data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- id: 2
name: An ice sculpture
price: 12.5
tags:
- cold
- ice
dimensions:
length: 7
width: 12
height: 9.5
warehouseLocation:
latitude: -78.75
longitude: 20.4
- id: 3
name: A blue mouse
price: 25.5
dimensions:
length: 3.1
width: 1
height: 1
warehouseLocation:
latitude: 54.4
longitude: -32.7
23 changes: 23 additions & 0 deletions test/valid_data.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- id: 2
name: An ice sculpture
price: 12.5
tags:
- cold
- ice
dimensions:
length: 7
width: 12
height: 9.5
warehouseLocation:
latitude: -78.75
longitude: 20.4
- id: 3
name: A blue mouse
price: 25.5
dimensions:
length: 3.1
width: 1
height: 1
warehouseLocation:
latitude: 54.4
longitude: -32.7
27 changes: 27 additions & 0 deletions test/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ describe('validate', function() {
});
});

it('should validate valid data with the "yml" extension', function(done) {
cli('-s test/schema -d test/valid_data.yml', function(error, stdout, stderr) {
assert.strictEqual(error, null);
assertValid(stdout, 1);
assert.equal(stderr, '');
done();
});
});

it('should validate valid data with the "yaml" extension', function(done) {
cli('-s test/schema -d test/valid_data.yaml', function(error, stdout, stderr) {
assert.strictEqual(error, null);
assertValid(stdout, 1);
assert.equal(stderr, '');
done();
});
});

it('should validate valid data with the "json5" extension', function(done) {
cli('-s test/schema -d test/valid_data.json5', 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/schema.json -d test/invalid_data.json --errors=line', function (error, stdout, stderr) {
assert(error instanceof Error);
Expand Down

0 comments on commit 8ee220f

Please sign in to comment.