verificator
Version:
Client and server-side validation JavaScript library
180 lines (147 loc) • 6.8 kB
JavaScript
'use strict';
exports.__esModule = true;
var _constants = require('./constants');
var _utils = require('./utils');
var utils = _interopRequireWildcard(_utils);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Translator = function () {
function Translator(locale, validator) {
_classCallCheck(this, Translator);
this._validator = validator;
this.setLocale(locale);
this.setCustomMessages(locale.customMessages || {});
this.setCustomAttributes(locale.customAttributes || {});
}
Translator.prototype.setLocale = function setLocale(locale) {
this._messages = utils.flatten(locale.messages);
this._attributes = locale.attributes || {};
return this;
};
Translator.prototype.getAttribute = function getAttribute(attribute) {
var _attributes = this._attributes,
_customAttributes = this._customAttributes;
if (attribute in _customAttributes) {
return _customAttributes[attribute];
}
if (attribute in _attributes) {
return _attributes[attribute];
}
return null;
};
Translator.prototype.getMessage = function getMessage(rule, attribute, value, parameters) {
var _messages = this._messages,
_customMessages = this._customMessages;
var rawAttribute = attribute;
attribute = this._getDisplayableAttribute(attribute);
parameters = this._getDisplayableParameters(rule, parameters);
var _arr = [_customMessages, _messages];
for (var _i = 0; _i < _arr.length; _i++) {
var source = _arr[_i];
var message = this._findMessage(source, rawAttribute, { rule: rule, attribute: attribute, value: value, parameters: parameters });
if (message !== null) {
return message;
}
}
return 'Invalid value for field "' + attribute + '"';
};
Translator.prototype.setCustomMessages = function setCustomMessages(messages) {
this._customMessages = utils.flatten(messages || {});
return this;
};
Translator.prototype.addCustomMessages = function addCustomMessages(messages) {
this._customMessages = Object.assign({}, this._customMessages, utils.flatten(messages || {}));
return this;
};
Translator.prototype.setCustomAttributes = function setCustomAttributes(attributes) {
this._customAttributes = attributes || {};
return this;
};
Translator.prototype.addCustomAttributes = function addCustomAttributes(attributes) {
this._customAttributes = Object.assign({}, this._customAttributes, attributes || {});
return this;
};
Translator.prototype._findMessage = function _findMessage(source, attribute, parameters) {
var type = this._getAttributeType(attribute);
var keys = [attribute + '.' + parameters.rule + ':' + type, attribute + '.' + parameters.rule, parameters.rule + ':' + type, parameters.rule];
for (var _iterator = keys, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i2 >= _iterator.length) break;
_ref = _iterator[_i2++];
} else {
_i2 = _iterator.next();
if (_i2.done) break;
_ref = _i2.value;
}
var key = _ref;
for (var _iterator2 = Object.keys(source), _isArray2 = Array.isArray(_iterator2), _i3 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
var _ref2;
if (_isArray2) {
if (_i3 >= _iterator2.length) break;
_ref2 = _iterator2[_i3++];
} else {
_i3 = _iterator2.next();
if (_i3.done) break;
_ref2 = _i3.value;
}
var sourceKey = _ref2;
if (utils.is(sourceKey, key)) {
var message = source[sourceKey];
if (typeof message === 'string') {
return message;
}
if (typeof message === 'function') {
return message(parameters);
}
return null;
}
}
}
return null;
};
Translator.prototype._getDisplayableAttribute = function _getDisplayableAttribute(attribute) {
var implicitAttributes = this._validator.getImplicitAttributes();
var primaryAttribute = this._validator.getPrimaryAttribute(attribute);
var expectedAttributes = attribute !== primaryAttribute ? [attribute, primaryAttribute] : [attribute];
for (var _iterator3 = expectedAttributes, _isArray3 = Array.isArray(_iterator3), _i4 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
var _ref3;
if (_isArray3) {
if (_i4 >= _iterator3.length) break;
_ref3 = _iterator3[_i4++];
} else {
_i4 = _iterator3.next();
if (_i4.done) break;
_ref3 = _i4.value;
}
var name = _ref3;
var line = this.getAttribute(name);
if (line !== null) {
return line;
}
}
if (primaryAttribute in implicitAttributes) {
return attribute;
}
return attribute.toLowerCase().replace(/_/g, ' ');
};
Translator.prototype._getDisplayableParameters = function _getDisplayableParameters(rule, parameters) {
var _this = this;
if (_constants.DEPENDENT_RULES.indexOf(rule) > -1) {
return parameters.map(function (parameter) {
return _this._getDisplayableAttribute(parameter);
});
}
return parameters;
};
Translator.prototype._getAttributeType = function _getAttributeType(attribute) {
if (this._validator.hasRule(attribute, ['numeric', 'integer'])) {
return 'numeric';
} else if (this._validator.hasRule(attribute, ['array'])) {
return 'array';
}
return 'string';
};
return Translator;
}();
exports['default'] = Translator;