all files / lib/waterline/query/ validate.js

86.11% Statements 31/36
72.22% Branches 13/18
100% Functions 12/12
96.77% Lines 30/31
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                    1308×     1308×     1308×       1308× 1314× 1314× 1314×       1308× 1308× 1308×           1308×   1308×               1308×           1302×           1302× 1308× 1308× 1308×       1302× 1302× 1302×         1308× 1302×          
/**
 * Validation
 *
 * Used in create and update methods validate a model
 * Can also be used independently
 */
 
var _ = require('lodash');
var WLValidationError = require('../error/WLValidationError');
var async = require('async');
 
module.exports = {
 
  validate: function(values, presentOnly, cb) {
    var self = this;
 
    // Handle optional second arg
    if (typeof presentOnly === 'function') {
      cb = presentOnly;
      presentOnly = false;
    }
 
    async.series([
 
      // Run Before Validate Lifecycle Callbacks
      function(cb) {
        var runner = function(item, callback) {
          item.call(self, values, function(err) {
            Iif (err) return callback(err);
            callback();
          });
        };
 
        async.eachSeries(self._callbacks.beforeValidate, runner, function(err) {
          Iif (err) return cb(err);
          cb();
        });
      },
 
      // Run Validation
      function(cb) {
        self._validator.validate(values, presentOnly, function _afterValidating(err, invalidAttributes) {
          // If fatal error occurred, handle it accordingly.
          Iif (err) {
            return cb(err);
          }
 
          // Otherwise, check out the invalid attributes that were sent back.
          //
          // Create validation error here
          // (pass in the invalid attributes as well as the collection's globalId)
          if (invalidAttributes) {
            return cb(new WLValidationError({
              invalidAttributes: invalidAttributes,
              model: self.globalId || self.adapter.identity
            }));
          }
 
          cb();
        });
      },
 
      // Run After Validate Lifecycle Callbacks
      function(cb) {
        var runner = function(item, callback) {
          item(values, function(err) {
            Iif (err) return callback(err);
            callback();
          });
        };
 
        async.eachSeries(self._callbacks.afterValidate, runner, function(err) {
          Iif (err) return cb(err);
          cb();
        });
      }
 
    ], function(err) {
      if (err) return cb(err);
      cb();
    });
  }
 
};