diff --git a/lib/model-patch.js b/lib/model-patch.js index 7f1c99b..c2b251a 100644 --- a/lib/model-patch.js +++ b/lib/model-patch.js @@ -4,6 +4,19 @@ var async = require('async'); var utils = require('prana').utils; +var validator = require('validator/lib/validators'); + +// Modify notEmpty validator method. +validator.notEmpty = function (data) { + if (typeof data === 'undefined' || data === null) return false; + if (typeof data === 'number' || typeof data === 'boolean') return true; + if (Object.prototype.toString.call(data) === '[object Function]') + if (typeof data.length !== 'undefined' && data.length === 0) return false; + if (typeof data === 'string' && data.match(/^[\s\t\r\n]*$/)) return false; + if (JSON.stringify(data) === '{}') return false; + + return true; +}; var modelPatch = module.exports = { @@ -171,7 +184,7 @@ var modelPatch = module.exports = { var fieldFullName = (prefix ? prefix + ' - ' : '') + fieldSettings.title; // If field is required check if a value was supplied for it. - if (fieldSettings.required && !(fieldName in item)) { + if (fieldSettings.required && !(fieldName in item && validator.notEmpty(item[fieldName]))) { errors.push(fieldFullName + ' is required.'); return next(null, [fieldFullName + ' is required.']); }