UNPKG

verificator

Version:

Client and server-side validation JavaScript library

79 lines (58 loc) 2.06 kB
'use strict'; exports.__esModule = true; var _actions = require('./actions'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ErrorBag = function () { function ErrorBag(store) { _classCallCheck(this, ErrorBag); this._store = store; } ErrorBag.prototype.add = function add(key, message) { this._store.dispatch((0, _actions.addError)(key, message)); return this; }; ErrorBag.prototype.remove = function remove(key) { this._store.dispatch((0, _actions.removeError)(key)); return this; }; ErrorBag.prototype.clear = function clear() { this._store.dispatch((0, _actions.clearErrors)()); return this; }; ErrorBag.prototype.first = function first(key) { var _get = this.get(key), messages = _get[0]; return messages; }; ErrorBag.prototype.has = function has(key) { return this.get(key).length > 0; }; ErrorBag.prototype.get = function get(key) { var _store$getState = this._store.getState(), errors = _store$getState.errors; if (key in errors) { return errors[key]; } return []; }; ErrorBag.prototype.all = function all() { var _this = this; var _store$getState2 = this._store.getState(), errors = _store$getState2.errors; return Object.keys(errors).reduce(function (value, key) { return value.concat(_this.get(key)); }, []); }; ErrorBag.prototype.any = function any() { return this.count() > 0; }; ErrorBag.prototype.count = function count() { var _store$getState3 = this._store.getState(), errors = _store$getState3.errors; return Object.keys(errors).reduce(function (value, key) { return value + errors[key].length; }, 0); }; return ErrorBag; }(); exports['default'] = ErrorBag;