grunt-yaml-validator
Advanced tools
Comparing version 0.7.1 to 0.7.2
{ | ||
"name": "grunt-yaml-validator", | ||
"description": "Validate Yaml files and enforce a given structure", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"homepage": "https://github.com/paazmaya/grunt-yaml-validator", | ||
@@ -35,3 +35,3 @@ "author": { | ||
"scripts": { | ||
"test": "grunt test" | ||
"test": "grunt eslint" | ||
}, | ||
@@ -38,0 +38,0 @@ "dependencies": { |
@@ -247,2 +247,3 @@ # grunt-yaml-validator | ||
* v0.7.1 (2014-11-03) Object properties in array are correctly matched | ||
* v0.7.2 (2014-11-03) Undefined key crashes if used | ||
@@ -249,0 +250,0 @@ ## License |
@@ -12,10 +12,10 @@ /** | ||
var yaml = require('js-yaml'), | ||
check = require('check-type').init(), | ||
grunt = require('grunt'); | ||
check = require('check-type').init(); | ||
var YamlValidatore = function(options) { | ||
var YamlValidatore = function YamlValidatore(options, grunt) { | ||
this.options = options; | ||
this.logs = []; | ||
this.nonValidPaths = []; | ||
this.inValidFiles = []; // list of filepaths | ||
this.nonValidPaths = []; // list of property paths | ||
this.inValidFilesCount = 0; | ||
this.grunt = grunt; // need to have reference on the original instance | ||
}; | ||
@@ -31,3 +31,3 @@ | ||
this.logs.push(msg); | ||
grunt.log.error(msg); | ||
this.grunt.log.error(msg); | ||
}; | ||
@@ -63,4 +63,4 @@ | ||
if (item instanceof Array) { | ||
if (check(doc[key]).is('Array')) { | ||
doc[key].forEach(function (child, index) { | ||
if (check(doc).has(key) && check(doc[key]).is('Array')) { | ||
doc[key].forEach(function eachArray(child, index) { | ||
notValid = validateStructure([child], item, current + '[' + index + ']'); | ||
@@ -97,12 +97,11 @@ notFound = notFound.concat(notValid); | ||
YamlValidatore.prototype.checkFile = function checkFile(filepath) { | ||
// Verbose output will tell which file is being read | ||
var data = grunt.file.read(filepath), | ||
hadError = 0, | ||
var data = this.grunt.file.read(filepath), | ||
hadWarning = 0, | ||
_self = this; | ||
var doc = yaml.safeLoad(data, { | ||
onWarning: function (error) { | ||
hadError = 1; | ||
_self.errored(error); | ||
onWarning: function onWarning(error) { | ||
hadWarning = 1; | ||
_self.errored(filepath + ' > ' + error); | ||
if (_self.options.yaml && | ||
@@ -117,3 +116,3 @@ typeof _self.options.yaml.onWarning === 'function') { | ||
var json = JSON.stringify(doc, null, ' '); | ||
grunt.file.write(filepath.replace(/yml$/, 'json'), json); | ||
this.grunt.file.write(filepath.replace(/yml$/, 'json'), json); | ||
} | ||
@@ -125,5 +124,4 @@ | ||
if (nonValidPaths.length > 0) { | ||
hadError = 1; | ||
this.errored(filepath + ' is not following the correct structure, missing:'); | ||
this.errored(grunt.log.wordlist(nonValidPaths, {color: 'grey'})); | ||
this.errored(this.grunt.log.wordlist(nonValidPaths, {color: 'grey'})); | ||
this.nonValidPaths = this.nonValidPaths.concat(nonValidPaths); | ||
@@ -133,3 +131,3 @@ } | ||
return hadError; | ||
return hadWarning; | ||
}; | ||
@@ -142,5 +140,5 @@ | ||
var _self = this; | ||
this.inValidFiles = files.map(function (filepath) { | ||
this.inValidFilesCount = files.map(function mapFiles(filepath) { | ||
return _self.checkFile(filepath); | ||
}).reduce(function (prev, curr) { | ||
}).reduce(function reduceFiles(prev, curr) { | ||
return prev + curr; | ||
@@ -155,11 +153,11 @@ }); | ||
if (this.inValidFiles.length > 0) { | ||
this.errored('Structure mismatching found in total of ' + this.inValidFiles.length + ' files'); | ||
if (this.inValidFilesCount > 0) { | ||
this.errored('Yaml format related errors in ' + this.inValidFilesCount + ' files'); | ||
} | ||
var len = this.nonValidPaths.length; | ||
grunt.log.writeln('Total of ' + len + ' validation error' + grunt.util.pluralize(len, '/s')); | ||
this.errored('Total of ' + len + ' structure validation error' + this.grunt.util.pluralize(len, '/s')); | ||
if (typeof this.options.log === 'string') { | ||
grunt.file.write(this.options.log, grunt.log.uncolor(this.logs.join('\n'))); | ||
this.grunt.file.write(this.options.log, this.grunt.log.uncolor(this.logs.join('\n'))); | ||
} | ||
@@ -180,3 +178,3 @@ }; | ||
var files = this.filesSrc.filter(function(filepath) { | ||
var files = this.filesSrc.filter(function filterFiles(filepath) { | ||
if (!grunt.file.exists(filepath)) { | ||
@@ -189,3 +187,3 @@ grunt.log.warn('Source file "' + filepath + '" not found.'); | ||
var validator = new YamlValidatore(options); | ||
var validator = new YamlValidatore(options, grunt); | ||
validator.validate(files); | ||
@@ -192,0 +190,0 @@ validator.report(); |
14372
252
151