208 lines
7.1 KiB
JavaScript
208 lines
7.1 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
exports.__esModule = true;
|
|
exports.default = StringSchema;
|
|
|
|
var _inherits = _interopRequireDefault(require("./util/inherits"));
|
|
|
|
var _mixed = _interopRequireDefault(require("./mixed"));
|
|
|
|
var _locale = require("./locale");
|
|
|
|
var _isAbsent = _interopRequireDefault(require("./util/isAbsent"));
|
|
|
|
// eslint-disable-next-line
|
|
var rEmail = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i; // eslint-disable-next-line
|
|
|
|
var rUrl = /^((https?|ftp):)?\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
|
|
|
|
var hasLength = function hasLength(value) {
|
|
return (0, _isAbsent.default)(value) || value.length > 0;
|
|
};
|
|
|
|
var isTrimmed = function isTrimmed(value) {
|
|
return (0, _isAbsent.default)(value) || value === value.trim();
|
|
};
|
|
|
|
function StringSchema() {
|
|
var _this = this;
|
|
|
|
if (!(this instanceof StringSchema)) return new StringSchema();
|
|
|
|
_mixed.default.call(this, {
|
|
type: 'string'
|
|
});
|
|
|
|
this.withMutation(function () {
|
|
_this.transform(function (value) {
|
|
if (this.isType(value)) return value;
|
|
return value != null && value.toString ? value.toString() : value;
|
|
});
|
|
});
|
|
}
|
|
|
|
(0, _inherits.default)(StringSchema, _mixed.default, {
|
|
_typeCheck: function _typeCheck(value) {
|
|
if (value instanceof String) value = value.valueOf();
|
|
return typeof value === 'string';
|
|
},
|
|
required: function required(message) {
|
|
if (message === void 0) {
|
|
message = _locale.mixed.required;
|
|
}
|
|
|
|
var next = _mixed.default.prototype.required.call(this, message);
|
|
|
|
return next.test({
|
|
message: message,
|
|
name: 'required',
|
|
test: hasLength
|
|
});
|
|
},
|
|
length: function length(_length, message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.length;
|
|
}
|
|
|
|
return this.test({
|
|
message: message,
|
|
name: 'length',
|
|
exclusive: true,
|
|
params: {
|
|
length: _length
|
|
},
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value.length === this.resolve(_length);
|
|
}
|
|
});
|
|
},
|
|
min: function min(_min, message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.min;
|
|
}
|
|
|
|
return this.test({
|
|
message: message,
|
|
name: 'min',
|
|
exclusive: true,
|
|
params: {
|
|
min: _min
|
|
},
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value.length >= this.resolve(_min);
|
|
}
|
|
});
|
|
},
|
|
max: function max(_max, message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.max;
|
|
}
|
|
|
|
return this.test({
|
|
name: 'max',
|
|
exclusive: true,
|
|
message: message,
|
|
params: {
|
|
max: _max
|
|
},
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value.length <= this.resolve(_max);
|
|
}
|
|
});
|
|
},
|
|
matches: function matches(regex, options) {
|
|
var excludeEmptyString = false;
|
|
var message;
|
|
|
|
if (options) {
|
|
if (options.message || options.hasOwnProperty('excludeEmptyString')) {
|
|
excludeEmptyString = options.excludeEmptyString;
|
|
message = options.message;
|
|
} else message = options;
|
|
}
|
|
|
|
return this.test({
|
|
message: message || _locale.string.matches,
|
|
params: {
|
|
regex: regex
|
|
},
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value === '' && excludeEmptyString || regex.test(value);
|
|
}
|
|
});
|
|
},
|
|
email: function email(message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.email;
|
|
}
|
|
|
|
return this.matches(rEmail, {
|
|
message: message,
|
|
excludeEmptyString: true
|
|
});
|
|
},
|
|
url: function url(message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.url;
|
|
}
|
|
|
|
return this.matches(rUrl, {
|
|
message: message,
|
|
excludeEmptyString: true
|
|
});
|
|
},
|
|
//-- transforms --
|
|
ensure: function ensure() {
|
|
return this.default('').transform(function (val) {
|
|
return val === null ? '' : val;
|
|
});
|
|
},
|
|
trim: function trim(message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.trim;
|
|
}
|
|
|
|
return this.transform(function (val) {
|
|
return val != null ? val.trim() : val;
|
|
}).test({
|
|
message: message,
|
|
name: 'trim',
|
|
test: isTrimmed
|
|
});
|
|
},
|
|
lowercase: function lowercase(message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.lowercase;
|
|
}
|
|
|
|
return this.transform(function (value) {
|
|
return !(0, _isAbsent.default)(value) ? value.toLowerCase() : value;
|
|
}).test({
|
|
message: message,
|
|
name: 'string_case',
|
|
exclusive: true,
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value === value.toLowerCase();
|
|
}
|
|
});
|
|
},
|
|
uppercase: function uppercase(message) {
|
|
if (message === void 0) {
|
|
message = _locale.string.uppercase;
|
|
}
|
|
|
|
return this.transform(function (value) {
|
|
return !(0, _isAbsent.default)(value) ? value.toUpperCase() : value;
|
|
}).test({
|
|
message: message,
|
|
name: 'string_case',
|
|
exclusive: true,
|
|
test: function test(value) {
|
|
return (0, _isAbsent.default)(value) || value === value.toUpperCase();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
module.exports = exports["default"]; |