Comparing version 1.4.1 to 1.5.0
#!/usr/bin/env node | ||
var fs = require("fs"); | ||
var path = require("path"); | ||
var parser = require("./jsonlint").parser; | ||
var JSV = require("JSV").JSV; | ||
var options = require("nomnom") | ||
.scriptName("jsonlint") | ||
.opts({ | ||
.script("jsonlint") | ||
.options({ | ||
file: { | ||
@@ -17,3 +20,3 @@ position: 0, | ||
callback: function() { | ||
return JSON.parse(fs.readFileSync(__dirname + "/../package.json", "utf8")).version; | ||
return require("../package").version; | ||
} | ||
@@ -40,12 +43,20 @@ }, | ||
help : 'compact error display' | ||
}, | ||
validate : { | ||
string: '-V, --validate', | ||
help : 'a JSON schema to use for validation' | ||
}, | ||
env : { | ||
string: '-e, --environment', | ||
"default": "json-schema-draft-03", | ||
help: 'which specification of JSON Schema the validation file uses' | ||
} | ||
}) | ||
.parseArgs(); | ||
}).parse(); | ||
if (options.compact) { | ||
var fileName = options.file? options.file + ': ' : ''; | ||
parser.parseError = parser.lexer.parseError = function(str, hash) { | ||
console.error(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.'); | ||
throw new Error(str); | ||
}; | ||
var fileName = options.file? options.file + ': ' : ''; | ||
parser.parseError = parser.lexer.parseError = function(str, hash) { | ||
console.error(fileName + 'line '+ hash.loc.first_line +', col '+ hash.loc.last_column +', found: \''+ hash.token +'\' - expected: '+ hash.expected.join(', ') +'.'); | ||
throw new Error(str); | ||
}; | ||
} | ||
@@ -56,8 +67,18 @@ | ||
var parsed = options.sort ? | ||
sortObject(parser.parse(source)) : | ||
parser.parse(source); | ||
sortObject(parser.parse(source)) : | ||
parser.parse(source); | ||
if (options.validate) { | ||
var env = JSV.createEnvironment(options.env); | ||
var schema = JSON.parse(fs.readFileSync(path.normalize(options.validate), "utf8")); | ||
var report = env.validate(parsed, schema); | ||
if (report.errors.length) { | ||
throw report.errors.reduce(schemaError, 'Validation Errors:'); | ||
} | ||
} | ||
return JSON.stringify(parsed,null,options.indent); | ||
} catch (e) { | ||
if (! options.compact) { | ||
console.log(e); | ||
console.error(e); | ||
} | ||
@@ -68,7 +89,16 @@ process.exit(1); | ||
function schemaError (str, err) { | ||
return str + | ||
"\n\n"+err.message + | ||
"\nuri: " + err.uri + | ||
"\nschemaUri: " + err.schemaUri + | ||
"\nattribute: " + err.attribute + | ||
"\ndetails: " + JSON.stringify(err.details); | ||
} | ||
function main (args) { | ||
var source = ''; | ||
if (options.file) { | ||
var path = require('path').normalize(options.file); | ||
source = parse(fs.readFileSync(path, "utf8")); | ||
var json = path.normalize(options.file); | ||
source = parse(fs.readFileSync(json, "utf8")); | ||
if (options.inplace) { | ||
@@ -75,0 +105,0 @@ fs.writeSync(fs.openSync(path,'w+'), source, 0, "utf8"); |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"preferGlobal": true, | ||
@@ -29,3 +29,4 @@ "repository": { | ||
"dependencies": { | ||
"nomnom": ">= 1.x.x" | ||
"nomnom": ">= 1.5.x", | ||
"JSV": ">= 4.0.x" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
58309
1038
2
+ AddedJSV@>= 4.0.x
+ AddedJSV@4.0.2(transitive)
Updatednomnom@>= 1.5.x