Code coverage report for core/validation.js

Statements: 93.33% (14 / 15)      Branches: 62.5% (5 / 8)      Functions: 100% (4 / 4)      Lines: 92.31% (12 / 13)      Ignored: none     

All files » core/ » validation.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                        1         6 1 18               1   171 171 171 171   171 15 156        
/*
 * Copyright 2014-2016, Sébastien Piquemal <sebpiq@gmail.com>
 *
 * rhizome is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * rhizome is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with rhizome.  If not, see <http://www.gnu.org/licenses/>.
 */
"use strict";
 
// !!! This file must not use ES6 features, as it is browserified
 
var _ = require('underscore')
  , chai = require('chai')
  , vpod = require('validation-pod')
  , ValidationError = require('./errors').ValidationError
 
var ChaiValidator = exports.ChaiValidator = function() { vpod.Validator.apply(this, arguments) }
_.extend(ChaiValidator.prototype, vpod.Validator.prototype, {
  handleError: function(err) { Eif (err instanceof chai.AssertionError) return err.message }
})
 
// Mixin for classes that have a config to validate.
// Expects attributes :
//    - `_config` a reference to the config object
//    - `configDefaults` default values for the config
//    - `configValidator` an instance of `ChaiValidator` with tests for each config field
exports.ValidateConfigMixin = {
  validateConfig: function(done) {
    this._config = this._config || {}
    _.defaults(this._config, this.configDefaults)
    this.configValidator.run(this._config, function(err, validationErrors) {
      Iif (err)
        done(err)
      else if (_.keys(validationErrors).length)
        done(new ValidationError(validationErrors))
      else done()
    })
  }
}