UNPKG

laravel-form-validation

Version:
105 lines (104 loc) 2.98 kB
var Errors = /** @class */ (function () { /** * Create a new Errors instance. * * @param {Object} errors */ function Errors(errors) { if (errors === void 0) { errors = {}; } this.errors = errors; } /** * Get all of the messages in the bag. * * @return {object} */ Errors.prototype.all = function () { return this.errors; }; /** * Determine if messages exist for the given key. * * @param {String} field * @return {Boolean} */ Errors.prototype.has = function (field) { var hasError = this.errors.hasOwnProperty(field); if (!hasError) { hasError = Object.keys(this.errors).some(function (f) { return f.startsWith("".concat(field, ".")) || f.startsWith("".concat(field, "[")); }); } return hasError; }; /** * Get the first message from the bag for a given key. * * @param {String} field * @returns {String|null} */ Errors.prototype.first = function (field) { return this.get(field)[0]; }; /** * Get the first error from matching key * * @param fieldPattern */ Errors.prototype.firstWhere = function (fieldPattern) { var foundKey = Object.keys(this.all()).find(function (f) { return f.startsWith("".concat(fieldPattern, ".")) || f.startsWith("".concat(fieldPattern, "[")); }); if (foundKey) { return this.first(foundKey); } return null; }; /** * Get all of the messages from the bag for a given key. * * @param {String} field * @returns {Array} */ Errors.prototype.get = function (field) { return this.errors[field] || []; }; /** * Determine if we have any errors. * * @return {Boolean} */ Errors.prototype.any = function () { return Object.keys(this.errors).length > 0; }; /** * Get all the errors in a flat array. * * @return {Array} */ Errors.prototype.flatten = function () { return Object.values(this.errors).reduce(function (a, b) { return a.concat(b); }, []); }; /** * Record the new errors. * * @param {Object} errors */ Errors.prototype.record = function (errors) { this.errors = errors; }; /** * Clear a specific field, object or all error fields. * * @param {String?} field */ Errors.prototype.clear = function (field) { var errors = {}; if (field) { // Prevent reactivity issues errors = Object.assign({}, this.errors); Object.keys(errors) .filter(function (f) { return f === field || f.startsWith("".concat(field, ".")) || f.startsWith("".concat(field, "[")); }) .map(function (f) { return delete errors[f]; }); } this.record(errors); }; return Errors; }()); export default Errors;