redux-form-validators
Version:
Simple validations with redux-form / react-final-form
192 lines (155 loc) • 5.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isFileList = isFileList;
exports["default"] = void 0;
var _validators = _interopRequireDefault(require("./validators"));
var _helpers = require("./helpers");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var ACCEPT_SEP_REG = ',';
var file = (0, _helpers.memoize)(function () {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
message = _ref.message,
msg = _ref.msg,
accept = _ref.accept,
minSize = _ref.minSize,
maxSize = _ref.maxSize,
minFiles = _ref.minFiles,
maxFiles = _ref.maxFiles,
ifCond = _ref["if"],
unless = _ref.unless,
allowBlank = _ref.allowBlank;
msg = msg || message;
minFiles = (0, _helpers.selectNum)(minFiles);
maxFiles = (0, _helpers.selectNum)(maxFiles);
if (maxFiles < 0) {
maxFiles = null;
}
if (minFiles === null) {
minFiles = 1;
}
if (typeof accept === 'string' && accept.trim()) {
accept = accept.trim().toLowerCase().split(ACCEPT_SEP_REG).map(function (type) {
type = type.trim();
return type.charAt(0) === '.' || type.indexOf('*') < 0 ? type : (0, _helpers.stringToReg)(type);
});
} else {
accept = null;
}
var min = minSize != null ? sizeToInt(minSize) : null;
var max = maxSize != null ? sizeToInt(maxSize) : null;
return (0, _helpers.prepare)(ifCond, unless, false, function (value) {
var isAFileList = isFileList(value); // special blank check
if ((allowBlank != null ? allowBlank : _validators["default"].defaultOptions.allowBlank) && (typeof value === 'string' ? value.trim() === '' : isAFileList && value.length === 0)) {
return;
}
if (!isAFileList) {
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'file'));
}
if (isNaN(value.length)) {
value = [value];
}
if (value.length < minFiles) {
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'fileTooFew', 'tooFew', 'minFiles', {
count: minFiles
}));
}
if (maxFiles !== null && value.length > maxFiles) {
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'fileTooMany', 'tooMany', 'maxFiles', {
count: maxFiles
}));
}
var acceptError = [];
var tooSmallError = [];
var tooBigError = [];
var _loop = function _loop(i, len, _val, _ftype, _fext) {
_val = value[i];
if (accept) {
_ftype = _val.type ||
/* istanbul ignore next */
'';
_fext = fileExt(_val.name ||
/* istanbul ignore next */
'');
if (!accept.some(function (type) {
val = _val;
ftype = _ftype;
fext = _fext;
return typeof type === 'string' ? type === (type.charAt(0) === '.' ? _fext : _ftype) : type.test(_ftype);
})) {
acceptError.push(_val);
}
}
if (min != null && _val.size < min) {
tooSmallError.push(_val);
}
if (max != null && _val.size > max) {
tooBigError.push(_val);
}
val = _val;
ftype = _ftype;
fext = _fext;
};
for (var i = 0, len = value.length, val, ftype, fext; i < len; ++i) {
_loop(i, len, val, ftype, fext);
}
if (acceptError.length) {
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'fileAccept', 'accept', {
files: acceptError,
count: acceptError.length
}));
}
if (tooSmallError.length) {
var pair = parse(minSize);
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'fileTooSmall', 'tooSmall', 'minSize', {
files: tooSmallError,
count: tooSmallError.length,
size: _validators["default"].formatSize(pair[1], pair[2] || 'B')
}));
}
if (tooBigError.length) {
var _pair = parse(maxSize);
return _validators["default"].formatMessage((0, _helpers.prepareMsg)(msg, 'fileTooBig', 'tooBig', 'maxSize', {
files: tooBigError,
count: tooBigError.length,
size: _validators["default"].formatSize(_pair[1], _pair[2] || 'B')
}));
}
});
});
var _default = file;
exports["default"] = _default;
function isFileList(value) {
if (typeof FileList !== 'undefined' && value instanceof FileList || typeof File !== 'undefined' && (value instanceof File || value[0] instanceof File)) {
return true;
}
var str = _helpers.TO_STRING.call(value);
return str === '[object FileList]' || str === '[object File]' || _helpers.TO_STRING.call(value[0]) === '[object File]';
} // private
// eslint-disable-next-line no-useless-escape
var SIZE_REG = /^([\d\.]+)\s*([KMGTPE]?B)?$/;
var SIZE_UNITS = {
B: 1,
KB: 1024,
MB: 1048576,
GB: 1073741824,
TB: 1099511627776,
PB: 1125899906842624,
EB: 1152921504606847000
};
function parse(size) {
return SIZE_REG.exec(('' + size).trim());
}
function sizeToInt(size) {
var pair = parse(size);
if (pair) return pair[1] * (SIZE_UNITS[pair[2]] || 1);
/* istanbul ignore else */
if (typeof console !== 'undefined') {
console.error("file validator: size \"".concat(size, "\" unknown"));
}
return null;
}
function fileExt(filename) {
return filename.slice((filename.lastIndexOf('.') - 1 >>> 0) + 1).toLowerCase();
}
;