UNPKG

validatorjs2

Version:

Validation library inspired by Laravel's Validator

78 lines (66 loc) 1.7 kB
var Errors = function() { this.errors = {}; }; Errors.prototype = { constructor: Errors, /** * Add new error message for given attribute * * @param {string} attribute * @param {string} message * @return {void} */ add: function(attribute, message) { if (!this.has(attribute)) { this.errors[attribute] = []; } if (this.errors[attribute].indexOf(message) === -1) { this.errors[attribute].push(message); } }, /** * Returns an array of error messages for an attribute, or an empty array * * @param {string} attribute A key in the data object being validated * @return {array} An array of error messages */ get: function(attribute) { if (this.has(attribute)) { return this.errors[attribute]; } return []; }, /** * Returns the first error message for an attribute, false otherwise * * @param {string} attribute A key in the data object being validated * @return {string|false} First error message or false */ first: function(attribute) { if (this.has(attribute)) { return this.errors[attribute][0]; } return false; }, /** * Get all error messages from all failing attributes * * @return {Object} Failed attribute names for keys and an array of messages for values */ all: function() { return this.errors; }, /** * Determine if there are any error messages for an attribute * * @param {string} attribute A key in the data object being validated * @return {boolean} */ has: function(attribute) { if (this.errors.hasOwnProperty(attribute)) { return true; } return false; } }; module.exports = Errors;